#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int As;
int Bs;
int Cs;
char A[2002];
char B[2002];
char C[2002];
int *okAB;
int *okBC;
int *okCA;
void chk(int As, char A[], int Bs, char B[], int ok[]){
  int i;
  for(i=(-8005);i<(8006);i++){
    int j;
    ok[i] = 1;
    for(j=(0);j<(Bs);j++){
      if(0 <= i+j  &&  i+j < As && A[i+j] != B[j] && A[i+j] != '?' && B[j] != '?'){
        ok[i] = 0;
        break;
      }
    }
  }
}
int main(){
  int i;
  wmem = memarr;
  int mn;
  int mx;
  int res = 1073709056;
  As = rd(A);
  Bs = rd(B);
  Cs = rd(C);
  walloc1d(&okAB, 100000);
  okAB = okAB + 40000;
  walloc1d(&okBC, 100000);
  okBC = okBC + 40000;
  walloc1d(&okCA, 100000);
  okCA = okCA + 40000;
  chk(As, A, Bs, B, okAB);
  chk(Bs, B, Cs, C, okBC);
  chk(Cs, C, As, A, okCA);
  for(i=(-4001);i<(4002);i++){
    int j;
    for(j=(-4001);j<(4002);j++){
      if(!okAB[i]){
        continue;
      }
      if(!okBC[j-i]){
        continue;
      }
      if(!okCA[-j]){
        continue;
      }
      chmin(res, max_L(max_L(As, i+Bs), j+Cs)-min_L(min_L(0, i), j));
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200314-1

// --- original code ---
// int As, Bs, Cs;
// char A[2002], B[2002], C[2002];
// int *okAB, *okBC, *okCA;
// 
// void chk(int As, char A[], int Bs, char B[], int ok[]){
//   rep(i,-8005,8006){
//     ok[i] = 1;
//     rep(j,Bs){
//       if(0 <= i+j < As && A[i+j] != B[j] && A[i+j] != '?' && B[j] != '?') ok[i] = 0, break;
//     }
//   }
// }
// 
// {
//   int mn, mx, res = int_inf;
//   rd(A@As, B@Bs, C@Cs);
// 
//   walloc1d(&okAB, 1d5); okAB = okAB + 4d4;
//   walloc1d(&okBC, 1d5); okBC = okBC + 4d4;
//   walloc1d(&okCA, 1d5); okCA = okCA + 4d4;
// 
//   chk(As, A, Bs, B, okAB);
//   chk(Bs, B, Cs, C, okBC);
//   chk(Cs, C, As, A, okCA);
// 
//   rep(i,-4001,4002) rep(j,-4001,4002){
//     if(!okAB[i]) continue;
//     if(!okBC[j-i]) continue;
//     if(!okCA[-j]) continue;
//     res <?= max(As, i+Bs, j+Cs) - min(0, i, j);
//   }
// 
//   wt(res);
// }


Measured memory_bytes: 512