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 Ruby (2.3.3) (judge verdict: Wrong Answer)

a = gets.chop
b = gets.chop
c = gets.chop
str_len_list = [a, b, c].permutation.map do |set|
  first, second, third = set.to_a
  pre_second = ''
  first.length.times do |fidx|
    sflg = true
    second.chars.each_with_index do |cs, idx|
      next if cs == '?' || first[idx+fidx] == '?' || first[idx+fidx].nil? || cs == first[idx+fidx]
      break sflg = false
    end
    sflg ? break : pre_second += '?'
  end
  first_and_second = (first+pre_second).chars.zip((pre_second+second).chars).map{ |fs1, fs2| fs1 == '?' ? fs2 : fs1 }.join
  pre_third = ''
  first_and_second.length.times do |fsidx|
    tflg = true
    third.chars.each_with_index do |ts, idx|
      next if ts == '?' || first_and_second[idx+fsidx] == '?' || first_and_second[idx+fsidx].nil? || ts == first_and_second[idx+fsidx]
      break tflg = false
    end
    tflg ? break : pre_third += '?'
  end
  (first_and_second+pre_third).chars.zip((pre_third+third).chars).map{ |fs1, fs2| fs1 == '?' ? fs2 : fs1 }.join.length
end
puts str_len_list.min