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)

// #pragma GCC optimize("O3")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T1, class T2>
ostream& operator << (ostream& out, pair <T1, T2> p)
{
	out << '(' << p.first << ',' << p.second << ')';
	return out;
}

template <class T1, class T2>
istream& operator >> (istream& in, pair<T1, T2> &p)
{
	in >> p.first >> p.second;
	return in;
}

template <class T>
istream& operator >> (istream& in, vector<T> &v)
{
	for (T &x : v)
		in >> x;
	return in;
}

template <class T>
ostream& operator << (ostream& out, vector<vector<T>> &v)
{
	for (vector<T> &x : v)
		out << x << '\n';
	return out;
}

template <class T>
ostream& operator << (ostream& out, vector<T> &v)
{
	for (T &x : v)
		out << x << ' ';
	return out;
}

long long gcd (long long a, long long b)
{
	if (b > a)
		swap(a, b);
	return (b ? gcd(b, a % b) : a);
}

using ll   = long long;
using pii  = pair<int, int>;
using pll  = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi   = vector<int>;
using vl   = vector<long long>;
using vvi  = vector<vector<int>>;
using vvl  = vector<vector<long long>>;

#define F          first
#define S          second
#define First      first.first
#define Second     first.second
#define Third      second
#define mp         make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)
#define per(i,b,a) for (int i = b; i > a; i--)
#define all(x)     x.begin(), x.end()
#define ret(x)     return cout << x, 0;
#define throwex    throw runtime_error ("Found the error.");

const int h = 1000000007;

set<int> match(string a, string b)
{
	vvi dp(a.size()+1, vi(b.size()+1, true));
	rep(i,0,a.size())
		rep(j,0,b.size())
			dp[i+1][j+1] = (a[i] == b[j] or a[i] == '?' or b[j] == '?') and dp[i][j];
	// cerr<<dp<<'\n';
	set<int> ans = {0};
	rep(i,0,b.size())
		if(dp[a.size()][i+1])
			ans.insert(i+1);
	return ans;
}

int f(string a, string b, string c)
{
	// cerr << a << ' ' << b << ' ' << c << '\n';
	int ans = b.size() + a.size() + c.size();
	auto sa = match(a, b);
	reverse(all(b));
	reverse(all(c));
	auto sb = match(c, b);
	reverse(all(b));
	reverse(all(c));
	// cerr << ans << '\n';
	rep(i,0,b.size() + 1)
	{
		if(i and b[i-1] == '?')
			sb.erase(b.size() + 1 - i);
		if(sa.count(i))
			ans = min(ans, int(b.size() + a.size() - min(int(a.size()), i) + c.size() - min(int(c.size()), *sb.rbegin())));
	}
	return ans;
}

signed main()
{
	ios::sync_with_stdio(false);
	#ifdef ONLINE_JUDGE
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.setstate(ios::failbit);
	#endif

	int ans = 6000;
	// string a = string(2000, 'a'),b = string(2000, '?'),c = string(2000, 'c');
	string a, b, c;
	cin >> a >> b >> c;
	ans = min(ans, f(a, b, c));
	ans = min(ans, f(c, b, a));
	ans = min(ans, f(b, a, c));
	ans = min(ans, f(c, a, b));
	ans = min(ans, f(a, c, b));
	ans = min(ans, f(b, c, a));
	cout << ans;
}