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 Kotlin (1.0.0) (judge verdict: Accepted)

fun main(args: Array<String>) {
    val A = next()
    val B = next()
    val C = next()
    val al = A.length
    val bl = B.length
    val cl = C.length
    val M = 2000
    val ab = BooleanArray(100000)
    val ac = BooleanArray(100000)
    val bc = BooleanArray(100000)
    fun match(c1: Char, c2: Char) = c1 == c2 || c1 == '?' || c2 == '?'
    for (i in 0 until al)
        for (j in 0 until bl)
            if (!match(A[i], B[j])) ab[i - j + 50000] = true
    for (i in 0 until al)
        for (j in 0 until cl)
            if (!match(A[i], C[j])) ac[i - j + 50000] = true
    for (i in 0 until bl)
        for (j in 0 until cl)
            if (!match(B[i], C[j])) bc[i - j + 50000] = true
    var ans = 3 * M
    for (i in -2 * M .. 2 * M)
        for (j in -2 * M .. 2 * M)
            if (!ab[i + 50000] && !ac[j + 50000] && !bc[j - i + 50000]) {
                val L = Math.min(0, Math.min(i, j))
                val R = Math.max(al, Math.max(bl + i, cl + j))
                ans = Math.min(ans, R - L)
            }
    println(ans)
}
fun Long.isEven() = (this % 2L) == 0L
fun Long.isOdd() = !this.isEven()
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun nextLong(offset: Long = 0L) = nextLong() + offset
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }
fun listOfInt(offset: Int = 0) = listOfString().map { Integer.parseInt(it) + offset }
fun listOfLong() = listOfString().map { java.lang.Long.parseLong(it) }
fun listOfLong(offset: Long = 0L) = listOfString().map { java.lang.Long.parseLong(it) + offset }
fun listOfDouble() = listOfString().map { it.toDouble() }