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 <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define all(x) (x).begin(),(x).end()
// #pragma GCC optimize("Ofast")
using namespace std;
typedef int64_t ll;
typedef long double ld;
const ll INF=(1LL<<60);
const ld pi=acosl((ld)1);
const ll mod = 1000000007;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
const int ddx[8]={1,0,-1,-1,-1,0,1,1};
const int ddy[8]={1,1,1,0,-1,-1,-1,0};
#define endn "\n"

bool f(string s,string t){
  rep(i,s.size())rep(j,s.size()){
    if(!((s[i]==t[j] and s[j]==t[i]) or (s[i]!=t[j] and s[j]!=t[i])))return false;
  }
  return true;
}

ll solve(string a,string b, string c){
  ll ret=INF;
  //取り合図a-b-c
  string t=a;
  rep(i,a.size()+1){
    //ここからbを開始
    string u=t;
    bool isOK=true;
    rep(j,b.size()){
      if(i+j>=u.size())u.push_back(b[j]);
      else if(u[i+j]=='?' or u[i+j]==b[j])u[i+j]=b[j];
      else isOK=false;
    }
    if(!isOK)continue;
    // cout<<isOK<<endl;
    // cout<<"B:"<<u<<endl;
    string t=u;
    rep(j,t.size()+1){
      // ここからcを開始
      isOK=true;
      // cout<<t<<endl;
      string u=t;
      rep(k,c.size()){
        if(j+k>=u.size())u.push_back(c[k]);
        else if(u[j+k]=='?' or u[j+k]==c[k])u[j+k]=c[k];
        else isOK=false;
      }
      // cout<<u<<" "<<isOK<<endl;
      if(isOK)ret=min(ret,(ll)u.size());
    }    
  }
  return ret;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout<<fixed;
	cout<<setprecision(30);
  // ll Q;cin>>Q;
	// while(Q--)solve();
  string a,b,c;cin>>a>>b>>c;
  ll ans=INF;
  ans=min(ans,solve(a,b,c));
  ans=min(ans,solve(a,c,b));
  ans=min(ans,solve(b,a,c));
  ans=min(ans,solve(b,c,a));
  ans=min(ans,solve(c,a,b));
  ans=min(ans,solve(c,b,a));
  cout<<ans<<endl;
}