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: Wrong Answer)

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);

        List<string> SSS(string a, string b, bool first)
        {
            var res = new List<string>();

            for (int i = 0; i < a.Length; i++)
            {
                var ed = true;

                for (int j = 0; j < b.Length; j++)
                {
                    if (i + j >= a.Length)
                        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.Length; k++)
                    {
                        if (k < a.Length)
                        {
                            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.Length; k < a.Length; k++)
                    {
                        cs.Add(a[k]);
                    }

                    res.Add(new string(cs.ToArray()));
                    if (first)
                        return res;
                }
            }

            res.Add(a + b);
            return res;
        }

        int Slv(string a, string b, string c)
        {
            var res = int.MaxValue;
            var s = SSS(a, b, false).OrderBy(x => x.Length);

            foreach (var ss in s)
            {
                var mn = Math.Max(c.Length, ss.Length);
                if (res < mn)
                    return res;


                var sss = SSS(c, ss, true);
                if (sss.Any() == false)
                    continue;

                res = Math.Min(res, sss.First().Length);
            }

            return res;
        }


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

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

            var r = new List<int>();
            r.Add(Slv(a, b, c));
            r.Add(Slv(a, c, b));
            r.Add(Slv(b, a, c));
            r.Add(Slv(b, c, a));
            r.Add(Slv(c, b, a));
            r.Add(Slv(c, a, b));

            var res = r.OrderBy(x => x).First();
            yield return res.ToString();
        }
    }
}