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 PyPy3 (2.4.0) (judge verdict: Wrong Answer)

import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

a = input()
b = input()
c = input()

def concat(x,y):
    len_x = len(x)
    len_y = len(y)
    for i in range(len_x+1):
        z = x[:i]
        ng = False
        for j in range(len_y):
            # log(x,y,i,j)
            if i+j >= len_x:
                z += y[j:]
                break
            elif x[i+j]=="?" or y[j]=="?" or x[i+j]==y[j]:
                if x[i+j]=="?" and y[j]=="?":
                    z += "?"
                else:
                    z += x[i+j] if x[i+j] != "?" else y[j]
            else:
                ng = True
                break
        if not ng:
            # log(x,y,z+x[i+j+1:])
            return z+x[i+j+1:]
    log("XXX")

def solve(x,y,z):
    return min(len(concat(concat(x,y), z)), len(concat(x, concat(y,z))))

print(min(
    solve(a,b,c),
    solve(a,c,b),
    solve(b,a,c),
    solve(b,c,a),
    solve(c,a,b),
    solve(c,b,a)
))