#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 4079, mlg = 17, mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ans = 1<<30, mab[maxn], mac[maxn], mbc[maxn];
bool good(int *dp, string &a, string &b, int s) {
	if(dp[s] >= 0) return dp[s];
	dp[s] = 1;
	for(int j = 0, i = s; dp[s] && i < a.size() && j < b.size(); i++, j++) {
		dp[s] &= a[i] == b[j] || a[i] == '?' || b[j] == '?';
	}
	return dp[s];
}
void solve(string a, string b, string c) {
	memset(mab, -1, sizeof mab);
	memset(mbc, -1, sizeof mbc);
	memset(mac, -1, sizeof mac);
	for(int i = 0; i <= a.size(); i++) {//shift for b
		for(int j = 0; j <= max(b.size(), a.size()); j++) {//shift for c
			if(good(mab, a, b, i) && good(mbc, b, c, j) && good(mac, a, c, i+j)) ans = min(ans, max({(int)a.size(), (int)b.size()+i, (int)c.size()+i+j}));
		}
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	string a, b, c;
	cin >> a >> b >> c;
	solve(a, b, c);
	solve(a, c, b);
	solve(b, a, c);
	solve(b, c, a);
	solve(c, a, b);
	solve(c, b, a);
	cout << ans << '\n';
}
