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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;

public class Main {
    private static final long MODVAL = 1000000007L;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        String c = sc.next();
//        int t = getMinLength("abc?e","cdefg");
        int l[] = new int[6];
        l[0] = getMinLength(a, b, c);
        l[1] = getMinLength(a, c, b);
        l[2] = getMinLength(b, a, c);
        l[3] = getMinLength(b, c, a);
        l[4] = getMinLength(c, a, b);
        l[5] = getMinLength(c, b, a);
        Arrays.sort(l);
        System.out.println(l[0]);
    }

    public static int getMinLength(String a, String b, String c) {
        return getMinLength(a, b) + getMinLength(b, c) - b.length();
    }

    public static int getMinLength(String a, String b) {
        for(int i=0; i<a.length(); i++) {
            boolean ok_flag = true;
            for(int t=0; t<a.length()-i && t<b.length(); t++) {
                if(a.substring(i+t, i+t+1).equals("?") || b.substring(t, t+1).equals("?")) continue;
                if(a.substring(i+t, i+t+1).equals(b.substring(t, t+1))) continue;
                ok_flag = false;
                break;
            }
            if(!ok_flag) continue;
            return i + b.length();
         }
        return a.length() + b.length();
    }
}