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 Go (1.6) (judge verdict: Wrong Answer)

package main
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func NextFloat(A ...*float64) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.ParseFloat(L[i], 64)
  }
}
func NextFloatVec(A *[]float64) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]float64, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.ParseFloat(l, 64)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumInt(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a
  }
  return sum
}

func KMP(S, P []int) []int {
  M := len(P)
  PMT := make([]int, M + 1)
  PMT[0] = -1
  for i, j := 2, 0; i <= M; {
    if P[i - 1] == P[j] {
      PMT[i] = j + 1
      i++
      j++
    } else if 0 < j {
      j = PMT[j]
    } else {
      i++
    }
  }
  K := make([]int, 0)
  for i, j := 0, 0; i + j < len(S); {
    if 0 < S[i + j] & P[j] {
      j++
      if j == M {
        K = append(K, i)
        i += M - PMT[M]
        j = PMT[j]
      }
    } else {
      i += j - PMT[j]
      if 0 < j { j = PMT[j] }
    }
  }
  return K
}

func Count(A, B, C []int) int {
  S := make([]int, len(A) + len(B) + len(C))
  Z := make([]int, len(A) + len(B) + len(C))
  for i := range S { Z[i] = 1 << uint(26) - 1 }
  for i, a := range A { Z[i] = a }
  ret := len(A) + len(B) + len(C)
  n := len(A)
  for i, z := range Z { S[i] = z }
  K := KMP(S, B)
  for _, k := range K {
    if len(A) < k { break }
    for i, z := range Z { S[i] = z }
    for i, b := range B { S[i + k] &= b }
    m := MaxInt(k + len(B), n)
    l := MaxInt(MinInt(KMP(S, C)...) + len(C), m)
    ret = MinInt(l, ret)
  }
  return ret
}

func StrInt(S string) []int {
  A := make([]int, len(S))
  for i, s := range S {
    if s == '?' {
      A[i] = 1 << uint(26) - 1
    } else {
      A[i] = 1 << uint(int(s) - int('a'))
    }
  }
  return A
}

func Solve() {
  a := NextLine()
  b := NextLine()
  c := NextLine()
  A, B, C := StrInt(a), StrInt(b), StrInt(c)
  ans := len(A) + len(B) + len(C)
  ans = MinInt(Count(A, B, C), ans)
  ans = MinInt(Count(A, C, B), ans)
  ans = MinInt(Count(B, A, C), ans)
  ans = MinInt(Count(B, C, A), ans)
  ans = MinInt(Count(C, A, B), ans)
  ans = MinInt(Count(C, B, A), ans)
  Write(ans)
}

func main() {
  Solve()
  Output()
}