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. You are given the strings "a", "b", and "c". Find the minimum possible length of "s".

Constraints:
1 ≤ |a|, |b|, |c| ≤ 2000
"a", "b", and "c" consist 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.

# Buggy version:
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=map(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=[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))]))

# Fixed version:
R=range
def F(a,b):A=len(a);r=[all([len(set([a[i+j],b[j],'?']))<3for j in R(min(A-i,len(b)))])for i in R(A)];return r+[1]
def Z(X):
 i,j,k=X;A,B,C=map(len,[S[i]for i 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
T=R(3)
S=[input()for i in T]
M=[[F(S[i],S[j])for j in T]for i in T]
from itertools import *;print(min([Z(i)for i in permutations(T)]))

# Buggy version:
def check(a,b):
    na=len(a)
    nb=len(b)
    rt=[0]*na
    for i in range(na):
        fl=True
        for j in range(min(na-i,nb)):
            if a[i+j]!=b[j] and a[i+j]!='?' and b[j]!='?':
                fl=False
                break
        if fl:
            rt[i]=1
    return rt+[1]

def tri_check(idx):
    id1,id2,id3=idx
    a,b,c=[strs[i] for i in idx]
    ab,bc,ac=chMat[id1][id2],chMat[id2][id3],chMat[id1][id3]
    na,nb,nc=map(len,(a,b,c))
    rt=na+nb+nc
    al,ar=na,nb
    for l in range(na+1):
        for r in range(nb+1):
            if ab[l]==1 and bc[r]==1 and ac[min(na,l+r)]==1:
                alt=max(na,l+nb,l+r+nc)
                if alt<rt:
                    rt=alt

        if ab[l]==1:
            for r in range(l+nb,na+1):
                if ac[r]==1:
                    alt=max(na,r+nc)
                    if alt<rt:
                        rt=alt
    return rt
    
a=input()
b=input()
c=input()
na,nb,nc=map(len,(a,b,c))
ans=10**5
strs=[a,b,c]
chMat=[[check(strs[i],strs[j]) if i!=j else [] for j in range(3)]for i in range(3)]

for i in permutations(range(3)):
    x=tri_check(i)
    ans=min(ans,x)

print(ans)


# Fixed version:
from itertools import permutations
def check(a,b):
    na=len(a)
    nb=len(b)
    rt=[0]*na
    for i in range(na):
        fl=True
        for j in range(min(na-i,nb)):
            if a[i+j]!=b[j] and a[i+j]!='?' and b[j]!='?':
                fl=False
                break
        if fl:
            rt[i]=1
    return rt+[1]

def tri_check(idx):
    id1,id2,id3=idx
    a,b,c=[strs[i] for i in idx]
    ab,bc,ac=chMat[id1][id2],chMat[id2][id3],chMat[id1][id3]
    na,nb,nc=map(len,(a,b,c))
    rt=na+nb+nc
    al,ar=na,nb
    for l in range(na+1):
        for r in range(nb+1):
            if ab[l]==1 and bc[r]==1 and ac[min(na,l+r)]==1:
                alt=max(na,l+nb,l+r+nc)
                if alt<rt:
                    rt=alt

        if ab[l]==1:
            for r in range(l+nb,na+1):
                if ac[r]==1:
                    alt=max(na,r+nc)
                    if alt<rt:
                        rt=alt
    return rt
    
a=input()
b=input()
c=input()
na,nb,nc=map(len,(a,b,c))
ans=10**5
strs=[a,b,c]
chMat=[[check(strs[i],strs[j]) if i!=j else [] for j in range(3)]for i in range(3)]

for i in permutations(range(3)):
    x=tri_check(i)
    ans=min(ans,x)

print(ans)

# Buggy version:
def main():
  a = input()
  b = input()
  c = input()
  ab = [None]*20000
  ac = [None]*20000
  bc = [None]*20000
  def match(c1,c2):
    return (c1=='?')or(c2=='?')or(c1==c2)

  A = len(a)
  B = len(b)
  C = len(c)
  def solve(d1,d2,n,m,l):
    for i in range(n):
      for j in range(m):
        if not(match(d1[i],d2[j])):
          l[i-j+10000]=True
          continue
    return l
  ab = solve(a,b,A,B,ab)
  ac = solve(a,c,A,C,ac)
  bc = solve(b,c,B,C,bc)
  ans = 6000
  for i in range(-4000,4001,1):
    for j in range(-4000,4001,1):
      if (not(ab[i+10000]))and(not(ac[j+10000]))and(not(bc[j-i+10000])):
        L = min(0,min(i,j))
        R = max(A,max(B+i,C+j))
        ans = min(ans,R-L)
  print(ans)
  
main()

# Fixed version:
def main():
  a = input()
  b = input()
  c = input()
  ab = [None]*20000
  ac = [None]*20000
  bc = [None]*20000
  def match(c1,c2):
    return (c1=='?')or(c2=='?')or(c1==c2)

  A = len(a)
  B = len(b)
  C = len(c)
  def solve(d1,d2,n,m,l):
    for i in range(n):
      for j in range(m):
        if not(match(d1[i],d2[j])):
          l[i-j+10000]=True
    return l
  ab = solve(a,b,A,B,ab)
  ac = solve(a,c,A,C,ac)
  bc = solve(b,c,B,C,bc)
  ans = 6000
  AB = A+B
  AC = A+C
  for i in range(-AC,AC,1):
    for j in range(-AB,AB,1):
      if (not(ab[i+10000]))and(not(ac[j+10000]))and(not(bc[j-i+10000])):
        L = min(0,min(i,j))
        R = max(A,max(B+i,C+j))
        ans = min(ans,R-L)
  print(ans)
  
main()

# Buggy version:
def main():
  a = input()
  b = input()
  c = input()
  ab = [False]*20000
  ac = [False]*20000
  bc = [False]*20000
  def match(c1,c2):
    return (c1=='?')or(c2=='?')or(c1==c2)

  A = len(a)
  B = len(b)
  C = len(c)
  def solve(d1,d2,n,m,l):
    for i in range(n):
      for j in range(m):
        if not(match(d1[i],d2[j])):
          l[i-j+10000]=True
          continue
    return l
  ab = solve(a,b,A,B,ab)
  ac = solve(a,c,A,C,ac)
  bc = solve(b,c,B,C,bc)
  ans = 6000
  for i in range(-4000,4001,1):
    for j in range(-4000,4001,1):
      if (not(ab[i+10000]))and(not(ac[j+10000]))and(not(bc[j-i+10000])):
        L = min(0,min(i,j))
        R = max(A,max(B+i,C+j))
        ans = min(ans,R-L)
  print(ans)
  
  
main()

# Fixed version:
def main():
  a = input()
  b = input()
  c = input()
  ab = [None]*20000
  ac = [None]*20000
  bc = [None]*20000
  def match(c1,c2):
    return (c1=='?')or(c2=='?')or(c1==c2)

  A = len(a)
  B = len(b)
  C = len(c)
  def solve(d1,d2,n,m,l):
    for i in range(n):
      for j in range(m):
        if not(match(d1[i],d2[j])):
          l[i-j+10000]=True
          continue
    return l
  ab = solve(a,b,A,B,ab)
  ac = solve(a,c,A,C,ac)
  bc = solve(b,c,B,C,bc)
  ans = 6000
  AB = A+B
  AC = A+C
  for i in range(-AC,AC,1):
    for j in range(-AB,AB,1):
      if (not(ab[i+10000]))and(not(ac[j+10000]))and(not(bc[j-i+10000])):
        L = min(0,min(i,j))
        R = max(A,max(B+i,C+j))
        ans = min(ans,R-L)
  print(ans)
  
main()

# Buggy version:
import sys
input=lambda: sys.stdin.readline().rstrip()
A=input()
B=input()
C=input()

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

def f(X,Y):
  XY=[True]*(10**4)
  x,y=len(X),len(Y)
  for i in range(5000-y+1,5000+x):
    ret=True
    if i<=5000:
      for j in range(min(x,y+i-5000)):
        if X[j]=="?" or Y[j-i+5000]=="?" or X[j]==Y[j-i+5000]:
          continue
        else:
          ret=False
    else:
      for j in range(min(y,x-i+5000)):
        if Y[j]=="?" or X[j+i-5000]=="?" or Y[j]==X[j+i-5000]:
          continue
        else:
          ret=False
    XY[i]=ret
  return XY
AB=f(A,B)
BC=f(B,C)
AC=f(A,C)
ans=a+b+c
for i in range(5000-b-c,5000+a+c):
  if AB[i]:
    for j in range(5000-b-c,5000+a+b):
      if -4000<=j-i<=4000:
        if AC[j] and BC[j-i+5000]:
          l=min(5000,i,j)
         r=max(i+b,j+c,5000+a)
          ans=min(ans,r-l)
      else:
        if AC[j]:
          l=min(5000,i,j)
          r=max(i+b,j+c,5000+a)
          ans=min(ans,r-l)
print(ans)


# Fixed version:
import sys
input=lambda: sys.stdin.readline().rstrip()
A=input()
B=input()
C=input()

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

def f(X,Y):
  XY=[True]*(10**4)
  x,y=len(X),len(Y)
  for i in range(5000-y+1,5000+x):
    ret=True
    if i<=5000:
      for j in range(min(x,y+i-5000)):
        if X[j]=="?" or Y[j-i+5000]=="?" or X[j]==Y[j-i+5000]:
          continue
        else:
          ret=False
    else:
      for j in range(min(y,x-i+5000)):
        if Y[j]=="?" or X[j+i-5000]=="?" or Y[j]==X[j+i-5000]:
          continue
        else:
          ret=False
    XY[i]=ret
  return XY
AB=f(A,B)
BC=f(B,C)
AC=f(A,C)
ans=a+b+c
for i in range(5000-b-c,5000+a+c):
  for j in range(5000-b-c,5000+a+b):
    if -4000<=j-i<=4000:
      if AB[i] and AC[j] and BC[j-i+5000]:
        l=min(5000,i,j)
        r=max(i+b,j+c,5000+a)
        ans=min(ans,r-l)
    else:
      if AB[i] and AC[j]:
        l=min(5000,i,j)
        r=max(i+b,j+c,5000+a)
        ans=min(ans,r-l)
print(ans)

# Buggy version:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools

A,B,C = read().decode().split()

A = list(A)
B = list(B)
C = list(C)

def can_match(x,y):
    return x == '?' or y == '?' or x == y

M = 2020

def solve(A,B,C,LA,LB,LC):
    B_from_A = [False] * (M+M+1)
    for i in range(M+M+1):
        B_from_A[i] = all(can_match(x,y) for x,y in zip(A[i:],B))
    C_from_A = [False] * (M+M+1)
    for i in range(M+M+1):
        C_from_A[i] = all(can_match(x,y) for x,y in zip(A[i:],C))
    C_from_B = [False] * (M+M+1)
    for i in range(M+M+1):
        C_from_B[i] = all(can_match(x,y) for x,y in zip(B[i:],C))
    for i in range(M):
        if not B_from_A[i]:
            continue
        for j in range(i,i+M):
            if not C_from_A[j]:
                continue
            if not C_from_B[j-i]:
                continue
            yield max(LA, LB+i, LC+j)

answer = 10 ** 9
for A,B,C in itertools.permutations([A,B,C]):
    LA = len(A)
    LB = len(B)
    LC = len(C)
    x = min(solve(A,B,C,LA,LB,LC))
    if answer > x:
        answer = x
print(answer)



# Fixed version:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools

A,B,C = read().decode().split()

A = list(A)
B = list(B)
C = list(C)

def can_match(x,y):
    return x == '?' or y == '?' or x == y

M = 2020

def solve(A,B,C,LA,LB,LC):
    B_from_A = [True] * (M+1)
    for i in range(M+1):
        B_from_A[i] = all(can_match(x,y) for x,y in zip(A[i:],B))
    C_from_A = [True] * (M+M+1)
    for i in range(M+M+1):
        C_from_A[i] = all(can_match(x,y) for x,y in zip(A[i:],C))
    C_from_B = [True] * (M+1)
    for i in range(M+1):
        C_from_B[i] = all(can_match(x,y) for x,y in zip(B[i:],C))
    for i in range(M):
        if not B_from_A[i]:
            continue
        for j in range(M):
            if not C_from_B[j]:
                continue
            if not C_from_A[i+j]:
                continue
            yield max(LA, LB+i, LC+i+j)
            break

answer = 10 ** 9
for A,B,C in itertools.permutations([A,B,C]):
    LA = len(A)
    LB = len(B)
    LC = len(C)
    x = min(solve(A,B,C,LA,LB,LC))
    if answer > x:
        answer = x
print(answer)