Problem:
Score : 500 points

Problem Statement

Snuke has a string s.
From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows:

Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with ?s.

For example, if s is mississippi, we can choose the substring ssissip and replace its 1-st and 3-rd characters with ? to obtain ?s?ssip.

You are given the strings a, b, and c.
Find the minimum possible length of s.

Constraints

1 \leq |a|, |b|, |c| \leq 2000

a, b, and c consists of lowercase English letters and ?s.

Input

Input is given from Standard Input in the following format:

a
b
c

Output

Print the minimum possible length of s.

Sample Input 1

a?c
der
cod

Sample Output 1

7

For example, s could be atcoder.

Sample Input 2

atcoder
atcoder
???????

Sample Output 2

7

a, b, and c may not be distinct.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     @hamayanhamayan0
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/















bool ok_ab[2010]; // [a]
bool ok_bc[2010]; // [b]
bool ok_ac[2010]; // [a]
int solve(string A, string B, string C) {
	int na = A.size();
	int nb = B.size();
	int nc = C.size();

	rep(a, 0, na) {
		ok_ab[a] = true;
		for (int i = 0; a + i < na && i < nb; i++) {
			if (A[a + i] == '?') continue;
			if (B[i] == '?') continue;
			if (A[a + i] != B[i]) ok_ab[a] = false;
		}
	}
	ok_ab[na] = true;

	rep(b, 0, nb) {
		ok_bc[b] = true;
		for (int i = 0; b + i < nb && i < nc; i++) {
			if (B[b + i] == '?') continue;
			if (C[i] == '?') continue;
			if (B[b + i] != C[i]) ok_bc[b] = false;
		}
	}
	ok_bc[nb] = true;

	rep(a, 0, na) {
		ok_ac[a] = true;
		for (int i = 0; a + i < na && i < nc; i++) {
			if (A[a + i] == '?') continue;
			if (C[i] == '?') continue;
			if (A[a + i] != C[i]) ok_ac[a] = false;
		}
	}
	ok_ac[na] = true;

	int ans = inf;
	rep(a, 0, na + 1) rep(b, 0, nb) if(ok_ab[a] && ok_bc[b]) {
		if (a + b < na && !ok_ac[a + b]) continue;
		chmin(ans, max({ na, a + nb, a + b + nc }));
	}
	return ans;

	/*
	rep(a, 0, na) if (okb[a]) {
		string AB = A;
		int i = 0;
		for (; a + i < na && i < nb; i++) {
			if (A[a + i] == '?') AB += B[i];
			else AB += A[a + i];
		}
		while (a + i < na) AB += A[a + i], i++;
		while (i < nb) AB += B[i], i++;

		int nab = AB.size();
		bool ok = true;
		rep(ab, 0, nab) {
			for (int i = 0; a + i < na && i < nb; i++) {
				if (A[a + i] == '?') continue;
				if (B[i] == '?') continue;
		}
	}*/

	/*rep(a, 0, na + 1) rep(b, 0, nb + 1) dp1[a][b] = inf;
	dp1[0][0] = 0;

	rep(a, 0, na + 1) rep(b, 0, nb + 1) if(dp1[a][b] != inf) {
		if (a == 0) {
			if (b != 0) continue;

			chmin(dp1[a + 1][b], dp1[a][b] + 1);
		}

		if(a == )
	}

	rep(a, 0, na + 1) rep(b, 0, nb + 1) dp2[a][b] = inf;
	dp2[0][0] = 0;*/

}
//---------------------------------------------------------------------------------------------------
void _main() {
	vector<string> v;
	rep(i, 0, 3) {
		string s; cin >> s;
		v.push_back(s);
	}
	sort(all(v));

	int ans = inf;
	do {
		chmin(ans, solve(v[0], v[1], v[2]));
	} while (next_permutation(all(v)));
	cout << ans << endl;
}