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)

import sys
sys.setrecursionlimit(1000000)


def js(s1, s2):

    for i in range(len(s1)):
        flag = True
        for j in range(0, min(len(s1) - i, len(s2))):
            c1 = s1[i + j]
            c2 = s2[j]

            if c1 != '?' and c2 != '?' and c1 != c2:
                flag = False
                break

        if flag:
            if len(s1) >= len(s2) + i:
                l1 = list(s1)
                for j in range(len(s2)):
                    if s1[i + j] == '?':
                        l1[i+j] = s2[j]
                return "".join(l1)
            else:
                l2 = list(s2)
                for j in range(i, len(s1)):
                    if s2[j - i] == '?':
                        l2[j - i] = s1[j]
                return s1[:i] + "".join(l2)
    return s1 + s2


def main():
    a = input()
    b = input()
    c = input()

    ac = js(a, c)
    ab = js(a, b)
    ba = js(b, a)
    bc = js(b, c)
    ca = js(c, a)
    cb = js(c, b)

    abc = js(ab, c)
    acb = js(ac, b)
    bac = js(ba, c)
    bca = js(bc, a)
    cab = js(ca, b)
    cba = js(cb, a)

    print(min(len(s) for s in (abc, acb, bac, bca, cab, cba)))


main()