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: Runtime Error)

from itertools import permutations
import collections
abc = [input() for i in range(3)]


ans = 10000

for x in permutations(range(3)):
    a = abc[x[0]]
    b = abc[x[1]]
    c = abc[x[2]]

    id = 0
    ab = len(a)
    bc = len(b)
    for i in range(len(a)):
        if a[i] == b[0] or a[i] == "?" or b[0] == "?":
            count = 1
            for j in range(1, min(len(a) - i, len(b))):
                if a[i+j] == b[j] or a[i+j] == "?" or b[j] == "?":
                    count += 1
                else:
                    break
            if count + i == len(a):
                ab = i
                break
            elif count == len(b):
                ab = -1
                break
            else:
                continue
    if ab >= 0:
        for i in range(len(b)):
            if b[i] == c[0] or b[i] == "?" or c[0] == "?":
                count = 1
                for j in range(1, min(len(a) - i, len(c))):
                    if b[i+j] == c[j] or b[i+j] == "?" or c[j] == "?":
                        count += 1
                    else:
                        break
                if count + i == len(b):
                    bc = i
                    break
                elif count == len(b):
                    bc = -1
                    break
                else:
                    continue
    else:
        for i in range(len(a)):
            if a[i] == c[0] or a[i] == "?" or c[0] == "?":
                count = 1
                for j in range(1, min(len(a) - i, len(c))):
                    if a[i+j] == c[j] or a[i+j] == "?" or c[j] == "?":
                        count += 1
                    else:
                        break
                if count + i == len(a):
                    bc = i
                    break
                elif count == len(c):
                    bc = -1
                    break
                else:
                    continue
    tmp = 0
    if ab >= 0 and bc >= 0:
        tmp = ab + bc + len(c)
    elif ab >= 0 and bc < 0:
        tmp = len(b) + ab
    elif ab < 0 and bc >= 0:
        tmp = bc + len(c)
    else:
        tmp = len(a)
    ans = min(ans, tmp)

print(ans)