← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func computePi(pat string) []int {
	n := len(pat)
	pi := make([]int, n)
	j := 0
	for i := 1; i < n; i++ {
		for j > 0 && pat[i] != pat[j] {
			j = pi[j-1]
		}
		if pat[i] == pat[j] {
			j++
		}
		pi[i] = j
	}
	return pi
}

func maxOverlap(text, pat string) int {
	if len(pat) == 0 {
		return 0
	}
	pi := computePi(pat)
	q := 0
	n := len(text)
	for i := 0; i < n; i++ {
		for q > 0 && text[i] != pat[q] {
			q = pi[q-1]
		}
		if text[i] == pat[q] {
			q++
		}
		if q == len(pat) {
			if i < n-1 {
				q = pi[q-1]
			}
		}
	}
	return q
}

func containsSubstring(text, pat string) bool {
	if len(pat) == 0 {
		return true
	}
	if len(pat) > len(text) {
		return false
	}
	pi := computePi(pat)
	q := 0
	for i := 0; i < len(text); i++ {
		for q > 0 && text[i] != pat[q] {
			q = pi[q-1]
		}
		if text[i] == pat[q] {
			q++
		}
		if q == len(pat) {
			return true
		}
	}
	return false
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var strs []string
	for i := 0; i < 3; i++ {
		line, _ := reader.ReadString('\n')
		if len(line) > 0 && line[len(line)-1] == '\n' {
			line = line[:len(line)-1]
		}
		strs = append(strs, line)
	}
	minLen := 300010
	for i := 0; i < 3; i++ {
		T := strs[i]
		ok := true
		for j := 0; j < 3; j++ {
			if i == j {
				continue
			}
			if !containsSubstring(T, strs[j]) {
				ok = false
				break
			}
		}
		if ok && len(T) < minLen {
			minLen = len(T)
		}
	}
	pairs := [][]int{{0, 1, 2}, {0, 2, 1}, {1, 2, 0}}
	for _, pr := range pairs {
		idx1, idx2, third := pr[0], pr[1], pr[2]
		A, B := strs[idx1], strs[idx2]
		type cand struct {
			l int
			t string
		}
		var cands []cand
		k := maxOverlap(A, B)
		t1 := A + B[k:]
		l1 := len(A) + len(B) - k
		cands = append(cands, cand{l1, t1})
		k = maxOverlap(B, A)
		t2 := B + A[k:]
		l2 := len(B) + len(A) - k
		cands = append(cands, cand{l2, t2})
		if containsSubstring(A, B) {
			cands = append(cands, cand{len(A), A})
		}
		if containsSubstring(B, A) {
			cands = append(cands, cand{len(B), B})
		}
		for _, cd := range cands {
			if containsSubstring(cd.t, strs[third]) {
				if cd.l < minLen {
					minLen = cd.l
				}
			}
		}
	}
	perms := [][]int{
		{0, 1, 2}, {0, 2, 1},
		{1, 0, 2}, {1, 2, 0},
		{2, 0, 1}, {2, 1, 0},
	}
	for _, p := range perms {
		curT := strs[p[0]]
		curL := len(curT)
		k := maxOverlap(curT, strs[p[1]])
		curT += strs[p[1]][k:]
		curL += len(strs[p[1]]) - k
		m := maxOverlap(curT, strs[p[2]])
		curT += strs[p[2]][m:]
		curL += len(strs[p[2]]) - m
		if curL < minLen {
			minLen = curL
		}
	}
	fmt.Println(minLen)
}
```