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 PyPy3 (2.4.0) (judge verdict: Wrong Answer)

import copy
#Z[i]:length of the longest list starting from S[i] which is also a prefix of S
#O(|S|)
def Z_algorithm(s,L):
    N = len(s)
    Z_alg = [0]*N

    Z_alg[0] = 0
    i = L
    j = 0
    while i < N:
        while i+j < N and (s[j] == s[i+j] or s[j] == "?" or s[i+j] == "?"):
            j += 1
        Z_alg[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k < N and k + Z_alg[k]<j:
            Z_alg[i+k] = Z_alg[k]
            k += 1
        i += k
        j -= k
    return Z_alg

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


shaaa = ["$"]*2001
def de(x):
    n = len(x)
    for i in range(n):
        if x[i] == "$":
            return x[:i] 
    return x

def unite(x,y):
    y = y + shaaa
    X,Y = len(x),len(y)
    z = Z_algorithm(y+x,Y)

    tmp = -1
    for i in range(Y,X+Y):
        if z[i]+i == X+Y or z[i] == Y:
            tmp = i
            break


    if tmp == -1:
        return de(x+y)
    else:
        if z[tmp] == Y:
            res = x
            for i in range(tmp-Y,tmp):
                if res[i+Y-tmp] == "?":
                    res[i] = y[i-(tmp-Y)]
            return de(res)
        else:
            res = x
            for i in range(tmp-Y,X):
                if res[i] == "?":
                    res[i] = y[i-(tmp-Y)]
            for i in range(X-tmp+Y,Y):
                res.append(y[i])
            return de(res)

print(min(
len(unite(unite(a,b),c)),
len(unite(unite(a,c),b)),
len(unite(unite(b,a),c)),
len(unite(unite(b,c),a)),
len(unite(unite(c,a),b)),
len(unite(unite(c,b),a))))