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 <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 1000000000000000007LL;
constexpr int inf = 1000000007;

string renketu(const string& a, const string& b) {
    for (int i = 0; i < a.size(); ++i) {
        if (b.size() < a.size() - i) continue;
        bool can = true;
        for (int j = i; j < a.size(); ++j) {
            int k = j - i;
            if (!(a[j] == b[k] || a[j] == '?' || b[k] == '?')) {
                can = false;
                break;
            }
        }
        if (can) {
            string c(a);
            for (int j = 0; j < a.size() - i; ++j) {
                if (b[j] != '?') c[j + i] = b[j];
            }
            return c + b.substr(a.size() - i);
        }
    }
    return a + b;
}

int solve(const string& a, const string& b, const string& c) {
    return min(renketu(renketu(a, b), c).size(), renketu(a, renketu(b, c)).size());
}

int main() {
    string a, b, c;cin >> a >> b >> c;
    cout << min({
        solve(a, b, c),
        solve(a, c, b),
        solve(b, a, c),
        solve(b, c, a),
        solve(c, a, b),
        solve(c, b, a)
    }) << endl;
    return 0;
}