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)

a = input()
b = input()
c = input()

# 二つの文字列の場合を考える。
# 二つの文字列を生み出せるような最小の文字列を求める関数。
# print(a,b,c)

def sub_2(a0,b0):
    
    Y0 = []
    
    # a>=bとする。
    if len(a0) >= len(b0):
        a,b=a0,b0
    else:
        a,b=b0,a0
    
    # aがbを含む場合。
    for i in range(len(a)-len(b)+1):
        # print(a[i:i+len(b)])
        if a[i:i+len(b)] == b:
            return [a]
            break
    
    # aの右がbの左と重なる場合。
    for i in range(len(b)-1):
        # print(a[-len(b)+i+1:],b[:-i-1])
        if a[-len(b)+i+1:] == b[:-i-1]:
            Y0.append(a+b[-i-1:])
            break
    
    # aの左がbの右と重なる場合。
    for i in range(len(b)-1):
        # print(b[i+1:],a[:len(b)-i-1])
        if b[i+1:] == a[:len(b)-i-1]:
            Y0.append(b+a[len(b)-i-1:])
            break
    
    if not Y0:
        return [a+b,b+a]
    else:
        return Y0

Y = []
for i in sub_2(a,b):
    for j in sub_2(i,c):
        Y.append(len(j))
for i in sub_2(b,c):
    for j in sub_2(i,a):
        Y.append(len(j))
for i in sub_2(c,a):
    for j in sub_2(i,b):
        Y.append(len(j))

# print(Y)
print(min(Y))