Problem:
Score : 500 points

Problem Statement

Snuke has a string s.
From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows:

Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with ?s.

For example, if s is mississippi, we can choose the substring ssissip and replace its 1-st and 3-rd characters with ? to obtain ?s?ssip.

You are given the strings a, b, and c.
Find the minimum possible length of s.

Constraints

1 \leq |a|, |b|, |c| \leq 2000

a, b, and c consists of lowercase English letters and ?s.

Input

Input is given from Standard Input in the following format:

a
b
c

Output

Print the minimum possible length of s.

Sample Input 1

a?c
der
cod

Sample Output 1

7

For example, s could be atcoder.

Sample Input 2

atcoder
atcoder
???????

Sample Output 2

7

a, b, and c may not be distinct.

Solution in Go (1.6) (judge verdict: Wrong Answer)

package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string { sc.Scan(); return sc.Text() }
func scanRunes() []rune { return []rune(scanString()) }
func scanInt() int { a,_ := strconv.Atoi(scanString()); return a }
func scanInt64() int64 { a,_ := strconv.ParseInt(scanString(),10,64); return a }
func scanFloat64() float64 { a,_ := strconv.ParseFloat(scanString(),64); return a }

func scanInts(n int) []int {
	res := make([]int, n); for i := 0; i < n; i++ { res[i] = scanInt() }; return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int { if a<0 { return -a }; return a }
func min(a,b int) int { if a<b { return a }; return b }
func max(a,b int) int { if a>b { return a }; return b }

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	abc := make([]string,3)
	for i := 0; i < 3; i++ {
		abc[i] = scanString()
	}

	ans := len(abc[0])+len(abc[1])+len(abc[2])
	permutationSearch(3, func(a []int) {
		s1 := abc[a[0]]
		s2 := abc[a[1]]
		s3 := abc[a[2]]
		// debug(s3, s1,s2)

		ma := -1
		L: for i := 0; i < len(s2); i++ {
			for j := 0; j < min(i+1,len(s1)); j++ {
				if s2[i-j] != s1[len(s1)-1-j] && s2[i-j] != '?' && s1[len(s1)-1-j] != '?' {
					continue L
				}
			}

			ma = max(ma, i)
		}

		var s string
		if ma == -1 {
			s = s1+s2
		} else {
			if len(s1)>ma {
				s = s1+s2[ma+1:]
			}
		}

		ma = -1
		L1: for i := 0; i < len(s); i++ {
			for j := 0; j < min(i+1, len(s3)); j++ {
				if len(s3)-1-j < 0 {
				}
				if s[i-j] != s3[len(s3)-1-j] && s[i-j] != '?' && s3[len(s3)-1-j] != '?' {
					continue L1
				}
			}

			ma = max(ma, i)
		}

		if ma == -1 {
			s = s3+s
		} else {
			if len(s3)>ma {
				s = s3+s[ma+1:]
			}
		}

		// debug(s, ma)
		ans = min(ans, len(s))
	})

	fmt.Fprintln(wr, ans)

}

func permutationSearch(n int, fn func(a []int)) {
	a := make([]int, n)
	for i := 0; i < n; i++ { a[i] = i }
	swap := func(i,j int) { a[i],a[j] = a[j],a[i] }

	fn(a)
	for i := n-2; i >= 0; i-- {
		if a[i] > a[i+1] { continue }
		for j := n-1; j >= 0; j-- {
			if a[i] > a[j] { continue }
			swap(i,j)
			for k := i+1; k < (n+i+1)/2; k++ { swap(k,n-(k-i)) }
			fn(a)
			i = n-1
			break
		}
	}
}