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 static System.Math;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Runtime.CompilerServices;
using Library;

namespace Program
{
    public static class PANASONIC2020E
    {
        static public int numberOfRandomCases = 0;
        static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker)
        {
        }
        static public void Solve()
        {
            var a = NS;
            var b = NS;
            var c = NS;
            Func<string, string, string> f = (x, y) =>
            {
                Func<long, string, string, bool> chk = (idx, s, t) =>
                {
                    var last = Min(s.Length - idx, t.Length);
                    for (var i = 0; i < last; i++)
                    {
                        if (s[(int)idx + i] == '?' || t[i] == '?') continue;
                        if (s[(int)idx + i] != t[i])
                        {
                            return false;
                        }
                    }
                    return true;
                };
                var minl = x.Length + y.Length;
                var retstr = new char[minl];
                for (var i = 0; i < x.Length; i++)
                {
                    retstr[i] = x[i];
                }
                for (var i = 0; i < y.Length; i++)
                {
                    retstr[i + x.Length] = y[i];
                }
                for (var i = 1; i < x.Length; i++)
                {
                    if (chk(i, x, y))
                    {
                        var size = Max(x.Length, i + y.Length);
                        if (minl > size)
                        {
                            retstr = new char[size];
                            for (var j = 0; j < x.Length; j++)
                            {
                                retstr[i] = x[i];
                            }
                            for (var j = 0; j < y.Length; j++)
                            {
                                if (y[j] == '?' && retstr[j + i] != 0) continue;
                                retstr[j + i] = y[j];
                            }
                            minl = size;
                        }
                    }
                }
                var t2 = x; x = y; y = t2;
                for (var i = 0; i < x.Length; i++)
                {
                    if (chk(i, x, y))
                    {
                        var size = Max(x.Length, i + y.Length);
                        if (minl > size)
                        {
                            retstr = new char[size];
                            for (var j = 0; j < x.Length; j++)
                            {
                                retstr[i] = x[i];
                            }
                            for (var j = 0; j < y.Length; j++)
                            {
                                if (y[j] == '?' && retstr[j + i] != 0) continue;
                                retstr[j + i] = y[j];
                            }
                            minl = size;
                        }
                    }
                }
                return new string(retstr);
            };
            var ac = f(a, c);
            var bc = f(b, c);
            var ab = f(a, b);
            var ca = f(c, a);
            var cb = f(c, b);
            var ba = f(b, a);
            var ans = f(ac, b).Length;
            ans = Min(ans, f(bc, a).Length);
            ans = Min(ans, f(ab, c).Length);
            ans = Min(ans, f(ca, b).Length);
            ans = Min(ans, f(cb, a).Length);
            ans = Min(ans, f(ba, c).Length);
            ans = Min(ans, f(b, ac).Length);
            ans = Min(ans, f(a, bc).Length);
            ans = Min(ans, f(c, ab).Length);
            ans = Min(ans, f(b, ca).Length);
            ans = Min(ans, f(a, cb).Length);
            ans = Min(ans, f(c, ba).Length);
            Console.WriteLine(ans);
        }
        class Printer : StreamWriter
        {
            public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
            public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { base.AutoFlush = false; }
            public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { base.AutoFlush = false; }
        }
        static LIB_FastIO fastio = new LIB_FastIODebug();
        static public void Main(string[] args) { if (args.Length == 0) { fastio = new LIB_FastIO(); Console.SetOut(new Printer(Console.OpenStandardOutput())); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); }
        static long NN => fastio.Long();
        static double ND => fastio.Double();
        static string NS => fastio.Scan();
        static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray();
        static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray();
        static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray();
        static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift);
        static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred);
        static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);
        static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c);
        static IEnumerable<string> OrderBy(this IEnumerable<string> x) => x.OrderBy(e => e, StringComparer.OrdinalIgnoreCase);
        static IEnumerable<T> OrderBy<T>(this IEnumerable<T> x, Func<T, string> selector) => x.OrderBy(selector, StringComparer.OrdinalIgnoreCase);
        static IEnumerable<string> OrderByDescending(this IEnumerable<string> x) => x.OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase);
        static IEnumerable<T> OrderByDescending<T>(this IEnumerable<T> x, Func<T, string> selector) => x.OrderByDescending(selector, StringComparer.OrdinalIgnoreCase);
        static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } }
        static IEnumerator<uint> _xsi = _xsc();
        static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } }
    }
}
namespace Library {
    class LIB_FastIO
    {
        public LIB_FastIO() { str = Console.OpenStandardInput(); }
        readonly Stream str;
        readonly byte[] buf = new byte[1024];
        int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        byte read()
        {
            if (isEof) throw new EndOfStreamException();
            if (ptr >= len)
            {
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[ptr++];
        }
        char Char()
        {
            byte b = 0;
            do b = read();
            while (b < 33 || 126 < b);
            return (char)b;
        }
        virtual public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        virtual public long Long()
        {
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        virtual public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
    }
    class LIB_FastIODebug : LIB_FastIO
    {
        Queue<string> param = new Queue<string>();
        string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); }
        public LIB_FastIODebug() { }
        public override string Scan() => NextString();
        public override long Long() => long.Parse(NextString());
        public override double Double() => double.Parse(NextString());
    }
}