#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;

const int N = 3;
const int M = 2000;
string strs[N];
bool can[N][N][2*M+1];

bool cmp(char a, char b) {
	return a == '?' || b == '?' || a == b;
}

void calc(int a, int b) {
	for (int i = 0; i <= 2*M; ++i) {
		can[a][b][i] = 1;
		for (int j = 0; j < strs[b].size() && i + j < strs[a].size(); ++j) {
			if (! cmp(strs[a][i+j], strs[b][j])) can[a][b][i] = 0;
		}
	}
}

int solve(int a, int b, int c) {
	// a starts first, then b, then c
	// Loop how much they overlap.
	int res = 3 * M;
	for (int d01 = 0; d01 <= strs[a].size(); ++d01) {
		if (! can[a][b][d01]) continue;

		for (int d12 = 0; d12 <= max(strs[a].size() - d01, strs[b].size()); ++d12) {
			if (! can[b][c][d12]) continue;

			int d02 = d01 + d12;
			if (! can[a][c][d02]) continue;

			int off = max((int)strs[a].size(), (int)max(d01 + strs[b].size(), d02 + strs[c].size()));
			res = min(res, off);
		}
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> strs[0] >> strs[1] >> strs[2];
	for (int a = 0; a < 3; ++a) {
		for (int b = 0; b < 3; ++b) {
			if (a != b) calc(a, b);
		}
	}

	int res = 3 * M;
	vector<int> perm = {0, 1, 2};

	do {
		res = min(res, solve(perm[0], perm[1], perm[2]));
	} while (next_permutation(perm.begin(), perm.end()));

	cout << res << '\n';
}
