#include<bits/stdc++.h>
using namespace std;
#define maxn 2010
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);
#pragma GCC optimize(2)
#define ll long long
#define makefa(n) for(int i=1;i<=(n);i++)f[i]=i;
#define hh puts("");
#define eps 1e-10
#define db double
#define AC 2333
#define REP(i,n) for((i)=0;(i)<(n);(i)++)
bool ab[maxn+20000],ac[maxn+20000],bc[maxn+20000];
bool match(char c,char d){return c==d||c=='?'||d=='?';}
int main(){
   IOS
   string a,b,c;
   cin>>a>>b>>c;
   int i,j;
   int la=a.size(),lb=b.size(),lc=c.size();
   REP(i,la)REP(j,lb)if(!match(a[i],b[j]))ab[i-j+10000]=true;
    REP(i,la)REP(j,lc)if(!match(a[i],c[j]))ac[i-j+10000]=true;
    REP(i,lb)REP(j,lc)if(!match(b[i],c[j]))bc[i-j+10000]=true;
    int ans=3*maxn;
    for (i = -2*maxn; i <=2*maxn ; ++i) {
        for (j = -2*maxn;  j<=2*maxn ; j++) {
            if(!ab[i+10000] && !ac[j+10000] && !bc[j-i+10000]){
                int l=min(0,min(i,j));
                int r=max(la,max(lb+i,j+lc));
                ans=min(ans,r-l);
            }
        }
    }
    cout<<ans;
    return 0;
}
