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(),input(),input()]
def checkEqual(l,r,k):
    i = len(l)-k
    j = 0
    for a in range(k):
        x, y = i+a, j+a
        if l[x] != r[y]:
            if l[x]=='?' or r[y]=='?':
                continue
            return False
    return True

overLaps = [[[] for i in range(3)] for j in range(3)]
ij = [(0,1),(1,0),(1,2),(2,1),(0,2),(2,0)]
for i,j in ij:
    l = a[i]
    r = a[j]
    m = min(len(l),len(r))
    for k in range(m+1):
        if checkEqual(l,r,k):
            overLaps[i][j].append(k)

maxRe = 0
combs = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0)]
for x,y,z in combs:
    full = len(a[y])
    o1 = overLaps[x][y]
    o2 = overLaps[y][z]
    midOl = set(overLaps[x][z])
    for i in range(len(o1)-1,-1,-1):
        for j in range(len(o2)-1,-1,-1):
            re = o1[i] + o2[j]
            if re > full:
                if re-full not in midOl:
                    continue
            maxRe = max(re,maxRe)
    # i = len(o1)-1
    # j = len(o2)-1
    # if o1[i] + o2[j] <= full:
    #     re = o1[i] + o2[j]
    #     maxRe = max(re,maxRe)
    #     continue
    #
    # bar = full - o1[i]
    # while o2[j] > bar:
    #     j -= 1
    #     if j == 0:
    #         break
    # while j < len(o2):
    #     bar = full - o2[j]
    #     while o1[i] > bar:
    #         i -= 1
    #         if i<0:
    #             break
    #     if i<0:
    #         break
    #     re = o1[i] + o2[j]
    #     maxRe = max(re,maxRe)
    #     j += 1

ans = len(a[0])+len(a[1])+len(a[2])-maxRe
print(ans)