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)

s = {}
s[0] = input()
s[1] = input()
s[2] =input()

def compe(a,b):
    a,b = list(a),list(b)
    L = len(a) if len(a) < len(b) else len(b)
    c = 0
    for i in range(L):
        flg = 0
        for j,(x,y) in enumerate(zip(a[len(a) - i -1:len(a)],b[0:i+1])):
            if x == '?' or y == '?':
                continue
            elif x == y:
                continue
            else:
                flg = 1
                break
        if flg == 0:
            c = i+1
    ab_1 = a + b[c:]
    ab_2 = a[0:len(a)-c] + b
    for i ,(x, y) in enumerate(zip(ab_1,ab_2)):
        if x == '?':
            ab_1[i] = y
    return c,ab_1

junban = [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]
min_s = 100000
for j in junban:
    p,obj = compe(s[j[0]],s[j[1]])
    q,obj2 = compe(obj,s[j[2]])
    L = len(s[0])+len(s[1])+len(s[2]) - p - q
    if min_s > L:
        min_s = L

print(min_s)