#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
bool ab[100000],bc[100000],ac[100000];

bool match(char p, char q)
{
	return (p == '?' || q == '?' || p == q);
}

int main()
{
	string a,b,c;
	cin >> a >> b >> c;

	int A = a.size(), B = b.size(), C = c.size();
	rep(i,A)
	{
		rep(j,B)
		{
			if (!match(a[i],b[j])) ab[i-j+50000] = true;
		}
	}
	rep(i,B)
	{
		rep(j,C)
		{
			if (!match(b[i],c[j])) bc[i-j+50000] = true;
		}
	}
	rep(i,A)
	{
		rep(j,C)
		{
			if (!match(a[i],c[j])) ac[i-j+50000] = true;
		}
	}
	int ans = 6000;
	for (int i = -4000; i <= 4000; ++i)
	{
		for (int j = -4000; j <= 4000; ++j)
		{
			if (!ab[i+50000] && !ac[j+50000] && !bc[j-i+50000])
			{
				int L = min(0,min(i,j));
				int R = max(A,max(B+i,C+j));
				ans = min(ans,R-L);
			}
		}
	}
	cout << ans << endl;
	return 0;
}