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, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (int)v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
ll MOD = 1e9 + 7;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }

string merge(string x, string y){

    ll is = -1;

    ll mx = 0;
    
    for(ll i = 1; i <= min(len(x),len(y)); ++i){
        bool flag = true;
        rep(j,i){
            if(x[len(x)-i+j] != y[j] && y[j] != '?' && x[len(x)-i+j] != '?') flag = false;
        }
        if(flag) mx = i;
    }

    string t = "";
    for (ll i = mx; i < len(y); ++i) t += y[i];

    return x + t;
}

string solve(string x, string y, string z){
    return merge(merge(x, y), z);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);

    string a, b, c;
    cin >> a >> b >> c;

    vector<string> S = {solve(a, b, c), solve(a, c, b), solve(b, a, c), solve(b, c, a), solve(c, a, b), solve(c, b, a)};

    ll ans = 1e18;

    for (auto s : S) ans = min(ans,ll(len(s)));

    print(ans);
    return 0;
}