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 Python (3.4.3) (judge verdict: Wrong Answer)

from itertools import permutations
"""
どの順番に出てくるかをすべて（6通り）ためす
そこからa,b,cのずれ幅の組み合わせ（2000**2）をためす
判定をO(1)ですると
2000*2*6 = 2.4*10**7
x: aとbのずれ幅
y: aとcのずれ幅
"""
def main():
    s = sorted([input() for _ in range(3)])
    n = len(max(s, key=len))
    d = [[[None]*n for _ in range(3)] for _ in range(3)]
    ans = n*3
    for i in range(3):
        li = len(s[i])
        di = d[i]
        for j in range(3):
            if i == j:
                continue
            lj = len(s[j])
            dij = di[j]
            for k in range(li):
                notok = False
                for ni in range(k, li):
                    nj = ni - k
                    if nj >= lj:
                        break
                    if s[i][ni] == "?" or s[j][nj] == "?":
                        continue
                    if s[i][ni] != s[j][nj]:
                        notok = True
                dij[k] = notok
    f = lambda i, j, k: False if k>=len(s[i]) else d[i][j][k]
    for x in range(n):
        for y in range(n):
            if f(0, 1, x):
                continue
            if f(1, 2, y):
                continue
            if f(0, 2, x+y):
                continue
            now = max([0, len(s[0]), x+len(s[1]), x+y+len(s[2])])
            if ans > now:
                ans = now
    print(ans)
                
if __name__ == "__main__":
    main()