#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define int long long
#define ll long long
#define P pair<int,int>
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define all(V) V.begin(),V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = 1e-9;
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
using namespace std;
inline ll gcd(ll a, ll b) {
	while (b) {
		ll c = a;
		a = b; b = c % b;
	}
	return a;
}
inline ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
bool isprime(ll n) {
	if (n == 1)return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0)return false;
	}
	return true;
}
ll mypow(ll a, ll b) {
	if (!b)return 1;
	if (b & 1)return mypow(a, b - 1) * a;
	int memo = mypow(a, b >> 1);
	return memo * memo;
}
string a, b, c;
bool can[3][6010];
signed main() {
	cin >> a >> b >> c;
	rep(i, a.size() + b.size() + 1) {
		rep(j, a.size()) {
			if (b.size() - i + j < 0 || b.size() <= b.size() - i + j)continue;
			if (a[j] != b[b.size() - i + j] && a[j] != '?' && b[b.size() - i + j] != '?') {
				goto end1;
			}
		}
		rep(j, b.size()) {
			if (i - b.size() + j < 0 || a.size() <= i - b.size() + j)continue;
			if (a[i - b.size() + j] != b[j] && a[i - b.size() + j] != '?' && b[j] != '?') {
				goto end1;
			}
		}
		can[0][i] = true;
	end1:;
	}
	string d = a;
	a = b; b = c; c = d;
	rep(i, a.size() + b.size() + 1) {
		rep(j, a.size()) {
			if (b.size() - i + j < 0 || b.size() <= b.size() - i + j)continue;
			if (a[j] != b[b.size() - i + j] && a[j] != '?' && b[b.size() - i + j] != '?') {
				goto end2;
			}
		}
		rep(j, b.size()) {
			if (i - b.size() + j < 0 || a.size() <= i - b.size() + j)continue;
			if (a[i - b.size() + j] != b[j] && a[i - b.size() + j] != '?' && b[j] != '?') {
				goto end2;
			}
		}
		can[1][i] = true;
	end2:;
	}
	d = a;
	a = b; b = c; c = d;
	rep(i, a.size() + b.size() + 1) {
		rep(j, a.size()) {
			if (b.size() - i + j < 0 || b.size() <= b.size() - i + j)continue;
			if (a[j] != b[b.size() - i + j] && a[j] != '?' && b[b.size() - i + j] != '?') {
				goto end3;
			}
		}
		rep(j, b.size()) {
			if (i - b.size() + j < 0 || a.size() <= i - b.size() + j)continue;
			if (a[i - b.size() + j] != b[j] && a[i - b.size() + j] != '?' && b[j] != '?') {
				goto end3;
			}
		}
		can[2][i] = true;
	end3:;
	}
	d = a;
	a = b; b = c; c = d;
	int ans = INF;
	for (int i = b.size(); i <= a.size() + b.size() + c.size(); i++) {
		for (int j = c.size(); j <= a.size() + b.size() + c.size(); j++) {
			if ((a.size() + b.size() <= i || can[0][i]) && (a.size() + c.size() <= j || can[2][a.size() - j + c.size()]) && (j + b.size() - i <= 0 || b.size() + c.size() <= j + b.size() - i || can[1][j + b.size() - i]))chmin(ans, max({ (int)a.size(),i, j }));
		}
	}
	d = a;
	a = b; b = c; c = d;
	for (int i = b.size(); i <= a.size() + b.size() + c.size(); i++) {
		for (int j = c.size(); j <= a.size() + b.size() + c.size(); j++) {
			if ((a.size() + b.size() <= i || can[1][i]) && (a.size() + c.size() <= j || can[0][a.size() - j + c.size()]) && (j + b.size() - i <= 0 || b.size() + c.size() <= j + b.size() - i || can[2][j + b.size() - i]))chmin(ans, max({ (int)a.size(),i, j }));
		}
	}
	d = a;
	a = b; b = c; c = d;
	for (int i = b.size(); i <= a.size() + b.size() + c.size(); i++) {
		for (int j = c.size(); j <= a.size() + b.size() + c.size(); j++) {
			if ((a.size() + b.size() <= i || can[2][i]) && (a.size() + c.size() <= j || can[1][a.size() - j + c.size()]) && (j + b.size() - i <= 0 || b.size() + c.size() <= j + b.size() - i || can[0][j + b.size() - i]))chmin(ans, max({ (int)a.size(),i, j }));
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256