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 <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <iomanip>
#include <cmath>
#include <set>
#include <numeric>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;


bool match(string &a, string &b, int zure){
    for(int b_index = 0;b_index< b.size();b_index++){
        int a_index = b_index + zure;
        if (a_index >= a.size()){
            break;
        }

        if(a[a_index] != '?' && b[b_index] != '?' && a[a_index] != b[b_index]){
            return false;
        }
    }
    return true;
}

ll min_value(string &a ,string &b,string &c){
    for(int i = 0;i <= a.size();i++){
        if (match(a,b,i)){
            for(int j = 0;j <= b.size();j++){
                if (match(b,c,j) && match(a,c,i+j)){
                    int ret = a.size();
                    ret = max((int)(i + b.size()),ret);
                    ret = max((int)(i + j + c.size()),ret);
                    return ret;
                }
            }
        }
    }
    return a.size() + b.size() + c.size();
}
int main()
{
    // 整数の入力
    string a,b,c;
    cin >> a >> b >> c;
    ll min_length = 1000000;
    min_length = min(min_value(a,b,c),min_length);
    min_length = min(min_value(a,c,b),min_length);
    min_length = min(min_value(b,a,c),min_length);
    min_length = min(min_value(b,c,a),min_length);
    min_length = min(min_value(c,a,b),min_length);
    min_length = min(min_value(c,b,a),min_length);
    cout << min_length << endl;
    return 0;
}