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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
int f(char *a,char *b){//is nihou
  int i,j;
  for(i=0;a[i];i++){
    for(j=0;b[j];j++){
      if(a[i+j]=='?'||b[j]=='?')continue;
      if(a[i+j]!=b[j])goto NEXT;
    }
    return 1;
  NEXT:;
  }
  return 0;
}
int MI(int a,int b){return  a<b?a:b;}
int g(char *a,char *b){
  int i,j,n,m,min;
  for(n=0;a[n];n++);
  for(m=0;b[m];m++);
  min=n+m;
  for(i=0;i<n;i++){
    for(j=0;j<m;j++){
      if(i+j==n)break;
      if(a[i+j]=='?'||b[j]=='?')continue;
      if(a[i+j]!=b[j])goto NEXT;
    }
    min=MI(min,m+i);
  NEXT:;
  }
  return min;
}
int MIN(int a,int b,int c){return MI(a,MI(b,c));}
int main(){
  char a[2010],b[2010],c[2010];
  int min=1e9,i,j,k;
  scanf("%s %s %s",a,b,c);
  for(i=0;a[i];i++);
  for(j=0;b[j];j++);
  for(k=0;c[k];k++);
  if(f(a,b))min=MIN(min,g(a,c),g(c,a));//printf("%d\n",min);
  if(f(b,a))min=MIN(min,g(b,c),g(c,b));//printf("%d\n",min);

  if(f(b,c))min=MIN(min,g(b,a),g(a,b));//printf("%d\n",min);
  if(f(c,b))min=MIN(min,g(c,a),g(a,c));//printf("%d\n",min);
    
  if(f(a,c))min=MIN(min,g(a,b),g(b,a));//printf("%d\n",min);
  if(f(c,a))min=MIN(min,g(c,b),g(b,c));//printf("%d\n",min);
  //printf("%d %d\n",g(a,b),g(b,c)-j);
  min=MIN(min,g(a,b)+g(b,c)-j,g(a,c)+g(c,b)-k);//printf("%d\n",min);
  min=MIN(min,g(b,c)+g(c,a)-k,g(b,a)+g(a,c)-i);//printf("%d\n",min);
  min=MIN(min,g(c,a)+g(a,b)-i,g(c,b)+g(b,a)-j);//printf("%d\n",min);
  printf("%d\n",min);
  return 0;
}