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)

#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 2e4 + 5, up = 4000;

char a[N], b[N], c[N];
bool ab[N], ac[N], bc[N];

int main() {
    scanf("%s", a);
    scanf("%s", b);
    scanf("%s", c);
    int lena = strlen(a);
    int lenb = strlen(b);
    int lenc = strlen(c);

    rep(i, 0, lena - 1) rep(j, 0, lenb - 1)
        if(!(a[i] == b[j] || a[i] == '?' || b[j] == '?')) ab[i - j + up] = 1;
    rep(i, 0, lena - 1) rep(j, 0, lenc - 1)
        if(!(a[i] == c[j] || a[i] == '?' || c[j] == '?')) ac[i - j + up] = 1;
    rep(i, 0, lenb - 1) rep(j, 0, lenc - 1)
        if(!(b[i] == c[j] || b[i] == '?' || c[j] == '?')) bc[i - j + up] = 1;

    int ans = INF;

    rep(i, -4000, 4000) rep(j, -4000, 4000)
        if(!ab[i + up] && !ac[j + up] && !bc[j - i + up]) {
            int L = min({0, i, j});
            int R = max({lena, lenb + i, lenc + j});
            ans = min(ans, R - L);
        }

    printf("%d\n", ans);

    return 0;
}