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: Accepted)

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

bool ma(char c1, char c2) {
    if (c1 == '?' || c2 == '?') {
        return true;
    } else if (c1 == c2) {
        return true;
    } else {
        return false;
    }
}

string s[3];
bool match[3][3][6100];

void calc_match(int i, int j) {
    string a = s[i], b = s[j];
    for (int k = 0; k < a.size(); k++) {
        bool ok = true;
        for (int l = 0; ok && l < b.size() && k + l < a.size(); l++) {
            ok &= ma(a[k + l], b[l]);
        }
        match[i][j][k] = ok;
    }
}

int check(int i, int j, int k) {
    int res = INT_MAX;
    for (int x = 0; x < s[i].size(); x++) {
        if (!match[i][j][x]) continue;

        chmin(res, (int)(max({ s[i].size(), x + s[j].size() }) + s[k].size()));

        for (int y = 0; y < max(s[i].size() - x, s[j].size()); y++) {
            if (!match[j][k][y]) continue;
            if (!match[i][k][x + y]) continue;
            chmin(res, (int)max({ s[i].size(), x + s[j].size(), x + y + s[k].size() }));
        }
    }
    return res;
}

pii ps[3] = { { 0, 1 }, { 1, 2 }, { 0, 2 } };
int rest[3] = { 2, 0, 1 };

int main() {
    fastio();

    cin >> s[0] >> s[1] >> s[2];

    forn(i, 3) forn(j, 3) forn(k, 6100) match[i][j][k] = true;

    for (auto p : ps) {
        calc_match(p.fst, p.snd);
        calc_match(p.snd, p.fst);
    }

    int ans = s[0].size() + s[1].size() + s[2].size();

    forn(i, 3) {
        chmin(ans, check(ps[i].fst, ps[i].snd, rest[i]));
        chmin(ans, check(ps[i].snd, ps[i].fst, rest[i]));
    }

    cout << ans << endl;

    return 0;
}