R=range

def F(a,b):A=len(a);return[all([len(set([a[i+j],b[j],'?']))<3for j in R(min(A-i,len(b)))])for i in R(A)]+[1]

def Z(X):

 i,j,k=X;A,B,C=[len(S[t])for t in X];q=1e5

 for l in R(A+1):

  if M[i][j][l]:

   for r in R(l,A+B+1):

    if(B<r-l or M[j][k][r-l])*M[i][k][min(A,r)]:q=min(q,max(A,l+B,r+C))

 return q

S=[eval(input())for i in R(3)]

M=[[F(i,j)for j in S]for i in S]

from itertools import*;print((min([Z(i)for i in permutations(R(3))])))
K=4000

S=[eval(input())for i in range(3)]

A,B,C=S

a,b,c=len(A),len(B),len(C)

def F(S,T):

 R=[1]*(K*2+1)

 for i in range(-len(T),len(S)):

  for j in range(max(i,0),min(len(S),len(T)+i)):

   if S[j]!=T[j-i]and S[j]!="?"and T[j-i]!="?":

    R[i]=0

    break

 return R

S=F(A,B)

T=F(A,C)

U=F(B,C)

m=K*3

for p in range(-K,K+1):

 if S[p]!=0:

  for q in range(-K,K+1):

   if T[q]!=0 and U[q-p]!=0:

    m=min(m,max(p+b,q+c,a)-min(p,q,0))

print(m)
R = range


def F(a, b):
    A = len(a)
    return [
        all([len(set([a[i + j], b[j], "?"])) < 3 for j in R(min(A - i, len(b)))])
        for i in R(A)
    ] + [1]


def Z(X):
    i, j, k = X
    A, B, C = [len(S[t]) for t in X]
    q = 1e5
    for l in R(A + 1):
        if M[i][j][l]:
            for r in R(l, A + B + 1):
                if (B < r - l or M[j][k][r - l]) * M[i][k][min(A, r)]:
                    q = min(q, max(A, l + B, r + C))
    return q


S = [eval(input()) for i in R(3)]
M = [[F(i, j) for j in S] for i in S]
from itertools import *

print((min([Z(i) for i in permutations(R(3))])))

K = 4000
S = [eval(input()) for i in range(3)]
A, B, C = S
a, b, c = len(A), len(B), len(C)


def F(S, T):
    R = [1] * (K * 2 + 1)
    for i in range(-len(T), len(S)):
        for j in range(max(i, 0), min(len(S), len(T) + i)):
            if S[j] != T[j - i] and S[j] != "?" and T[j - i] != "?":
                R[i] = 0
                break
    return R


S = F(A, B)
T = F(A, C)
U = F(B, C)
m = K * 3
for p in range(-K, K + 1):
    if S[p] != 0:
        for q in range(-K, K + 1):
            if T[q] != 0 and U[q - p] != 0:
                m = min(m, max(p + b, q + c, a) - min(p, q, 0))
print(m)

-R = range +K = 4000 +S = [eval(input()) for i in range(3)] +A, B, C = S +a, b, c = len(A), len(B), len(C) -def F(a, b): -    A = len(a) -    return [ -        all([len(set([a[i + j], b[j], "?"])) < 3 for j in R(min(A - i, len(b)))]) -        for i in R(A) -    ] + [1] +def F(S, T): +    R = [1] * (K * 2 + 1) +    for i in range(-len(T), len(S)): +        for j in range(max(i, 0), min(len(S), len(T) + i)): +            if S[j] != T[j - i] and S[j] != "?" and T[j - i] != "?": +                R[i] = 0 +                break +    return R -def Z(X): -    i, j, k = X -    A, B, C = [len(S[t]) for t in X] -    q = 1e5 -    for l in R(A + 1): -        if M[i][j][l]: -            for r in R(l, A + B + 1): -                if (B < r - l or M[j][k][r - l]) * M[i][k][min(A, r)]: -                    q = min(q, max(A, l + B, r + C)) -    return q - - -S = [eval(input()) for i in R(3)] -M = [[F(i, j) for j in S] for i in S] -from itertools import * - -print((min([Z(i) for i in permutations(R(3))]))) +S = F(A, B) +T = F(A, C) +U = F(B, C) +m = K * 3 +for p in range(-K, K + 1): +    if S[p] != 0: +        for q in range(-K, K + 1): +            if T[q] != 0 and U[q - p] != 0: +                m = min(m, max(p + b, q + c, a) - min(p, q, 0)) +print(m)