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>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion
#pragma region chminmax
template <typename T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

int main()
{
    vector<string> s(3);
    rep(i, 3) cin >> s[i];
    sort(s.begin(), s.end());

    bool d[3][3][2000];

    int ans = 8000;
    do
    {
        rep(i, 3) rep(j, 3) rep(k, max(s[i].size(), s[j].size()))
        {
            if (i >= j)
                break;
            bool ok = true;
            for (int ni = k; ni < s[i].size(); ni++)
            {
                int nj = ni - k;
                if (nj >= s[j].size())
                    break;
                if (s[i][ni] == '?' || s[j][nj] == '?')
                    continue;

                if (s[i][ni] != s[j][nj])
                    ok = false;
            }
            d[i][j][k] = ok;
        }
        auto f = [&](int i, int j, int k) {
            if (k >= s[i].size())
                return true;

            return d[i][j][k];
        };

        rep(x, 2000) rep(y, 2000)
        {
            if (!f(0, 1, x))
                continue;
            if (!f(1, 2, y))
                continue;
            if (!f(0, 2, x + y))
                continue;

            int now = 0;
            now = max<int>(now, s[0].size());
            now = max<int>(now, x + s[1].size());
            now = max<int>(now, x + y + s[2].size());
            ans = min(ans, now);
        }
    } while (next_permutation(s.begin(), s.end()));

    cout << ans << endl;

    return 0;
}