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 C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace A
{
    class StartingPoint
    {
        static void Main(string[] args)
        {
            if (args?.Any() == true)
            {
                var assembly = Assembly.GetEntryAssembly();
                var resourceName = "AtCoderCSharp.In.txt";
                var stream = assembly.GetManifestResourceStream(resourceName);
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                var str = streamReader.ReadToEnd();
                var textReader = new StringReader(str);
                Console.SetIn(textReader);
            }

            try
            {
                foreach (var s in new Program().Solve())
                {
                    Console.WriteLine(s);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (args?.Any() != true)
                    throw e;
            }

            if (args?.Any() == true)
                Console.ReadKey();
        }
    }

    class Program
    {
        public List<int> ScanInts(int count) => Enumerable.Range(0, count).Select(_ => ScanInt).ToList();
        public List<int> ScanInts(long count) => ScanInts((int)count);
        public List<long> ScanLongs(int count) => Enumerable.Range(0, count).Select(_ => Scan).ToList();
        public List<long> ScanLongs(long count) => ScanLongs((int)count);
        public int ScanInt => int.Parse(Str);
        public long Scan => long.Parse(Str);
        public double ScanDouble => (double)Scan;

        StringBuilder sb = new StringBuilder();

        public string Str => ScanStr();
        public string ScanStr()
        {
            sb.Clear();

            int r = 0;
            bool? b = null;
            do
            {
                r = Console.Read();
                b = IsBreak(r);
            } while (b == true);

            if (b == null)
                throw new Exception("input error: unexpected end of stream");

            do
            {
                sb.Append((char)r);
                r = Console.Read();
                b = IsBreak(r);
            } while (b == false);

            return sb.ToString();
        }

        bool? IsBreak(int c)
        {
            switch (c)
            {
                case -1:
                    return null;
                case (int)' ':
                case (int)'\n':
                case (int)'\r':
                    return true;
                default:
                    return false;
            }
        }

        public IEnumerable<int> Loop(int count) => Enumerable.Range(0, count);
        public IEnumerable<int> Loop(int from, int to) => Enumerable.Range(from, to - from + 1);

        public IEnumerable<long> Loop(long count) => Loop(0, count - 1);
        public IEnumerable<long> Loop(long from, long to)
        {
            for (long i = from; i <= to; i++)
                yield return i;
        }

        public string YesNo(bool isYes) => isYes ? "Yes" : "No";

        public HashSet<T> ToHashSet<T>(IEnumerable<T> ts) => new HashSet<T>(ts);

        IEnumerable<List<char>> SSS(List<char> a, List<char> b, bool first)
        {
            for (int i = 0; i < a.Count; i++)
            {
                var ed = true;

                for (int j = 0; j < b.Count; j++)
                {
                    if (i + j >= a.Count)
                        break;

                    if (a[i + j] != b[j] && a[i + j] != '?' && b[j] != '?')
                    {
                        ed = false;
                        break;
                    }
                }

                if (ed)
                {
                    var cs = new List<char>();


                    for (int k = 0; k < i; k++)
                    {
                        cs.Add(a[k]);
                    }

                    for (int k = i; k < i + b.Count; k++)
                    {
                        if (k < a.Count)
                        {
                            var aa = a[k];
                            var bb = b[k - i];

                            if (aa == '?')
                                cs.Add(bb);
                            else
                                cs.Add(aa);
                        }
                        else
                        {
                            cs.Add(b[k - i]);
                        }
                    }

                    for (int k = i + b.Count; k < a.Count; k++)
                    {
                        cs.Add(a[k]);
                    }

                    yield return cs;
                    if (first)
                        yield break; ;
                }
            }

            var ab = new List<char>(a);
            ab.AddRange(b);
            yield return ab;
        }

        int Slv(List<char> a, List<char> b, List<char> c, int minRes)
        {
            var res = minRes;

            foreach (var ss in SSS(a, b, false))
            {
                var mn = Math.Max(c.Count, ss.Count);
                if (res < mn)
                    return res;


                var sss1 = SSS(c, ss, true);
                if (sss1.Any())
                    res = Math.Min(res, sss1.First().Count);

                var sss2 = SSS(ss, c, true);
                if (sss2.Any())
                    res = Math.Min(res, sss2.First().Count);
            }

            return res;
        }


        public IEnumerable<string> Solve()
        {
            var a = ScanStr().ToList();
            var b = ScanStr().ToList();
            var c = ScanStr().ToList();

            //var rd = new Random();
            //a = new string(Enumerable.Range(0, 2000).Select(x => '?').ToArray()).ToList();
            //b = new string(Enumerable.Range(0, 2000).Select(x => '?').ToArray()).ToList();
            //c = new string(Enumerable.Range(0, 2000).Select(x => '?').ToArray()).ToList();
            //a = new string(Enumerable.Range(0, 2000).Select(x => (char)((int)'a' + rd.Next() % 26)).ToArray()).ToList();
            //b = new string(Enumerable.Range(0, 2000).Select(x => (char)((int)'a' + rd.Next() % 26)).ToArray()).ToList();
            //c = new string(Enumerable.Range(0, 2000).Select(x => (char)((int)'a' + rd.Next() % 26)).ToArray()).ToList();

            var r = Slv(a, b, c, int.MaxValue);
            r = Slv(a, c, b, r);
            r = Slv(b, a, c, r);
            r = Slv(b, c, a, r);
            r = Slv(c, b, a, r);
            r = Slv(c, a, b, r);

            yield return r.ToString();
        }
    }
}