#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>pi;
const int MAXN=100005;
const int MOD=1000000007;

#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define DEC(i,a,b) for(int i=(a);i>=(b);i--)
#define fst first
#define snd second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define db(x) cerr<<#x<<" = "<<x<<"\n"
#define LOW(v,x) (lower_bound(all(v),(x))-(v).begin())
#define UP(v,x) (upper_bound(all(v),(x))-(v).begin())

bool match(char x, char y){
	return (x=='?'||y=='?'||x==y);
}

int ab[MAXN], bc[MAXN], ac[MAXN];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	string a,b,c; cin>>a>>b>>c;
	
	//int ans=1e9;
	int A=a.length(), B=b.length(), C=c.length();
	FOR(i,0,A-1) FOR(j,0,B-1) if(!match(a[i],b[j])) ab[i-j+50000]=1;
	FOR(i,0,A-1) FOR(j,0,C-1) if(!match(a[i],c[j])) ac[i-j+50000]=1;
	FOR(i,0,B-1) FOR(j,0,C-1) if(!match(b[i],c[j])) bc[i-j+50000]=1;
	
	int ans = 1e9;
	FOR(i,-4000,4000) FOR(j,-4000,4000) {
		int x=ab[50000+i]+ac[50000+j]+bc[-i+j+50000];
		if(x==0){
			int L = min({0,i,j});
			int R = max({A,B+i,C+j});
			ans = min(ans, R - L);
		}
	}
	cout<<ans;
}
