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
a_len, b_len, c_len = [a, b, c].map(&:length)
l_min = [a_len, b_len, c_len].max
min = 6001

a_l = b_len + c_len
a_r = a_l + a_len - 1
(0..a_len + b_len + c_len + c_len).each do |b_l|
  b_r = b_l + b_len - 1
  (0..a_len + b_len + c_len + b_len).each do |c_l|
    c_r = c_l + c_len - 1

    flg = ([a_l, b_l, c_l].min...[a_r, b_r, c_r].max).all? do |pt|
      t = []
      t << a[pt - a_l] if a_l <= pt
      t << b[pt - b_l] if b_l <= pt
      t << c[pt - c_l] if c_l <= pt
      (t.compact.uniq - ['?']).length <= 1
    end
    next unless flg
    str = ' ' * [a_r, b_r, c_r].max
    str[a_l..a_r] = a
    str[b_l..b_r] = b
    str[c_l..c_r] = c
    min = [min, [a_r, b_r, c_r].max - [a_l, b_l, c_l].min + 1].min
    break if min == l_min
  end
  break if min == l_min
end
puts min