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)

/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 110;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

string a, b, c, s;

bool solve(int len, string &A, string &B, string &C) {
    s.resize(len);
    FOR(i,0,len-1) s[i] = '?';
    FOR(i,0,SZ(A)-1) s[i] = A[i];
    
    bool co2 = false;
    FOR(i,0,len-1) {
        bool co = true;
        FOR(j,0,SZ(B)-1) {
            if (i + j >= len) {
                co = false;
                break;
            }
            char ch = B[j];
            if (s[i+j] != '?') {
                if (ch == '?') continue;
                if (s[i+j] == ch) continue;
                co = false;
                break;
            }
        }
        if (co) {
            FOR(j,0,SZ(B)-1) {
                char ch = B[j];
                if (s[i+j] != '?') {
                    if (ch == '?') continue;
                    if (s[i+j] == ch) continue;
                    return false;
                }
                s[i+j] = ch;
            }
            co2 = true;
            break;
        }
    }
    if (!co2) return false;

    // FOR(i,0,len-1) cout << s[i];
    // cout << endl;

    FOR(i,0,len-1) {
        bool co = true;
        FOR(j,0,SZ(C)-1) {
            if (i + j >= len) {
                co = false;
                break;
            }
            char ch = C[j];
            if (s[i+j] != '?') {
                if (ch == '?') continue;
                if (s[i+j] == ch) continue;
                co = false;
                break;
            }
        }
        if (co) return true;
    }
    return false;
}

bool check(int len) {
    if (solve(len, a, b, c)) return true;
    if (solve(len, a, c, b)) return true;
    if (solve(len, b, a, c)) return true;
    if (solve(len, b, c, a)) return true;
    if (solve(len, c, a, b)) return true;
    if (solve(len, c, b, a)) return true;
    return false;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> a >> b >> c;
    int dau = max(SZ(a), max(SZ(b), SZ(c))), cuoi = SZ(a) + SZ(b) + SZ(c);
    // cout << check(4) << endl;
    while (dau <= cuoi) {
        int mid = (dau + cuoi) >> 1;
        if (check(mid)) cuoi = mid-1;
        else dau = mid+1;
    }
    cout << dau;
    return 0;
}