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"
	"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 = 1
	}
	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)
	ans := 6000
	ans = minint(ans, ok(a, b, c))
	ans = minint(ans, ok(a, c, b))
	ans = minint(ans, ok(b, a, b))
	ans = minint(ans, ok(b, a, c))
	ans = minint(ans, ok(c, a, b))
	ans = minint(ans, ok(c, b, a))
	fmt.Fprintln(writer, ans)
}

func ok(a, b, c string) int {
	na := len(a)
	nb := len(b)
	nc := len(c)
	ci := 0
	for i := 0; i < nb; i++ {
		valid := true
		for j := 0; valid && j < nc && i+j < nb; j++ {
			valid = valid && !(b[i+j] != '?' && c[j] != '?' && b[i+j] != c[j])
		}
		if valid {
			break
		}
		ci++
	}
	ai := 0
	for i := 0; i < nb; i++ {
		valid := true
		for j := 0; valid && j < na && i+j < nb; j++ {
			valid = valid && !(b[nb-1-(i+j)] != '?' && a[na-1-j] != '?' && b[nb-1-(i+j)] != a[na-1-j])
		}
		if valid {
			break
		}
		ai++
	}

	return nb + maxint(0, ai+na-nb) + maxint(0, ci+nc-nb)
}

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
}