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 OCaml (4.02.3) (judge verdict: Runtime Error)


let calc a b c =
  let concat s ls t lt =
    let rec f i =
      let rec g j =
        if i+j >= ls || j >= lt then true
        else (s (i+j) = '?' || t j = '?' || s (i+j) = t j) && g (j+1)
      in
      if g 0 then i else f (i+1)
    in f 0
  in
  let la, lb, lc = String.(length a, length b, length c) in
  let abi = concat (fun i -> a.[i]) la (fun i -> b.[i]) lb in
  let lab = max la (abi + lb) in
  let abci = concat (fun i -> if i < abi then a.[i] else b.[i-abi]) lab (fun i -> c.[i]) lc in
  max lab (abci + lc)

let () =
  Scanf.scanf "%s %s %s" @@ fun a b c ->
    [calc a b c; calc a c b; calc b a c; calc b c a; calc c a b; calc c b a]
    |> List.fold_left min 10000 |> Printf.printf "%d\n"