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"
	"fmt"
	"math"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	a := getNextString(scanner)
	b := getNextString(scanner)
	c := getNextString(scanner)

	ab := oks(a, b)
	ba := oks(b, a)
	bc := oks(b, c)
	cb := oks(c, b)
	ac := oks(a, c)
	ca := oks(c, a)

	ans := len(a) + len(b) + len(c)
	// abc
	ans = minint(ans, ok(ab, bc, ac, len(a), len(b), len(c)))
	// acb
	ans = minint(ans, ok(ac, cb, ab, len(a), len(c), len(b)))
	// bac
	ans = minint(ans, ok(ba, ac, bc, len(b), len(a), len(c)))
	// bca
	ans = minint(ans, ok(bc, ca, ba, len(b), len(c), len(a)))
	// cab
	ans = minint(ans, ok(ca, ab, cb, len(c), len(a), len(b)))
	// cba
	ans = minint(ans, ok(cb, ba, ca, len(c), len(b), len(a)))
	fmt.Fprintln(writer, ans)
}
func ok(ab, bc, ac map[int]int, la, lb, lc int) int {
	ans := math.MaxInt32
	for d := range ab {
		for dd := range bc {
			if ac[d+dd] == 0 && la > dd+d {
				continue
			}
			ans = minint(ans, maxint(la, maxint(lc+dd+d, lb+d)))
		}
	}
	return ans
}
func oks(s, t string) map[int]int {
	n := len(s)
	m := len(t)
	ans := map[int]int{}
	for i := 0; i < n; i++ {
		valid := true
		for j := 0; valid && j < m && i+j < n; j++ {
			valid = valid && !(s[i+j] != '?' && t[j] != '?' && s[i+j] != t[j])
		}
		if valid {
			ans[i] = 1
		}
	}
	ans[n] = 1

	return ans
}
func maxint(a, b int) int {
	if a < b {
		return b
	}

	return a
}

func minint(a, b int) int {
	if a > b {
		return b
	}

	return a
}