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)


from collections import deque
from itertools import permutations

def match(s, t):
    # s -> tのマッチ
    res1 = list(s) + ['*'] * len(t)
    res2 = list(t) + ['*'] * len(s)
    
    while True:
        check = True
        for i in range(len(res1)):
            if res1[i] == res2[i]:
                continue

            if res1[i] == '?' or res1[i] == '*':
                continue

            if res2[i] == '?' or res2[i] == '*':
                continue

            check = False
            break

        if check:
            break
        else:
            temp = [res2[-1]]
            temp.extend(res2[:-1])
            res2 = temp


    res = []
    for r1, r2 in zip(res1, res2):
        if r1 == r2 == "*":
            continue

        if r1 == "*":
            res.append(r2)
        elif r1 == "?" and r2 == "*":
            res.append(r1)
        elif r1 == "?" and r2 != "*":
            res.append(r2)
        elif r1 != '?' and r2 == '?':
            res.append(r2)
        else:
            res.append(r1)

    return res


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

    min_len = float('inf')
    res = None
    for s1, s2, s3 in permutations([a, b, c]):
        m = match(s1, s2)
        t = match(m, s3)
        if min_len > len(t):
            min_len = len(t)
            res = t

    print(min_len)



if __name__ == "__main__":
    submit()