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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		String[] S = new String[]{A, B, C};
		int min = A.Length + B.Length + C.Length;
		
		HashSet<int>[][] L = new HashSet<int>[3][];
		for(int i=0;i<3;i++){
			L[i] = new HashSet<int>[3];
			for(int j=0;j<3;j++){
				L[i][j] = new HashSet<int>();
			}
		}
		
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				if(i == j) continue;
				var s = S[i];
				var t = S[j];
				for(int k=0;k<=s.Length;k++){
					bool chk = true;
					for(int l=k;l<s.Length;l++){
						if(l - k >= t.Length) break;
						if(s[l] == t[l - k]) continue;
						if(s[l] == '?') continue;
						if(t[l - k] == '?') continue;
						chk = false;
						break;
					}
					if(chk){
						L[i][j].Add(k);
					}
				}
				
			}
		}
		
		var a = new int[]{0,1,2};
		do {
			int i = a[0];
			int j = a[1];
			int k = a[2];
			foreach(var ij in L[i][j]){
				foreach(var jk in L[j][k]){
					if(ij + jk >= S[i].Length){
						var len = S[i].Length;
						len += S[j].Length;
						len -= S[i].Length - ij;
						len += S[k].Length;
						len -= S[j].Length - jk;
						min = Math.Min(min,len);
					} else {
						if(!L[i][k].Contains(ij + jk)) continue;
						var len2 = S[j].Length;
						len2 += S[k].Length;
						len2 -= S[j].Length - jk;
						var len = S[i].Length;
						len += len2;
						len -= S[i].Length - ij;
						
						min = Math.Min(min,len);
					}
				}
			}
		} while(NextPermutation(a));
		
		Console.WriteLine(min);
		
	}
	String A,B,C;
	public Sol(){
		A = rs();
		B = rs();
		C = rs();
	}
	
	static public bool NextPermutation<T>(T[] A)where T:IComparable<T>{
		for(int i=A.Length-2;i>=0;i--){
			if(A[i].CompareTo(A[i+1])<0){
				int trgt=Array.FindLastIndex(A,x=>A[i].CompareTo(x)<0);
				Swap(ref A[i],ref A[trgt]);
				Array.Reverse(A,i+1,A.Length-(i+1));
				return true;
			}
		}
		Array.Reverse(A);
		return false;
	}
	static void Swap<T>(ref T x, ref T y){
		T t=x;x=y;y=t;
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}