#include <bits/stdc++.h>

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

int mem[2005][2005][3][3];

string s[3];
int n[3];

bool equal(char c1, char c2) {
	if (c1 == '?' || c2 == '?')
		return true;
	return c1 == c2;
}

int dp(int i, int j, int s1, int s2) {
	if (i == n[s1] || j == n[s2])
		return 0;
	int& ret = mem[i][j][s1][s2];
	if (ret != -1)
		return ret;
	ret = OO;
	if (equal(s[s1][i], s[s2][j]))
		ret = dp(i + 1, j + 1, s1, s2);
	return ret;
}

int main() {
	#ifdef LOCAL
	auto stTime = clock();
//	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	for (int i = 0; i < 3; ++i) {
		cin >>  s[i];
		n[i] = sz(s[i]);
		db(i, n[i]);
	}


	for (int i = 0; i < 2005; ++i) {
		for (int j = 0; j < 2005; ++j) {
			for (int k = 0; k < 3; ++k) {
				for (int l = 0; l < 3; ++l) {
					mem[i][j][k][l] = -1;
				}
			}
		}
	}

	int perm[] = {0, 1, 2};

	int ans = n[0] + n[1] + n[2];

	do {
		int na = n[perm[0]];
		int nb = n[perm[1]];
		int nc = n[perm[2]];

		db(perm[0], perm[1], perm[2]);

		for (int ib = 0; ib <= na; ++ib) {
			for (int ic = ib; ic <= max(ib + nb, na); ++ic) {
				bool goodab = (ib >= na) || (!dp(ib , 0, perm[0], perm[1]));
				bool goodac = (ic >= na) || (!dp(ic , 0, perm[0], perm[2]));
				bool goodbc;
				if (ib <= ic)
					goodbc = (ic >= ib + nb) || (!dp(ic - ib, 0, perm[1], perm[2]));
				else
					goodbc = (ib >= ic + nc) || (!dp(ib - ic, 0, perm[2], perm[1]));

				if (goodab && goodac && goodbc)
					ans = min(ans, max(ic + nc, max(na, ib + nb)));
			}
		}


	} while (next_permutation(perm, perm + 3));


	cout << ans << '\n';


	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
    return 0;
}


Measured memory_bytes: 141824