import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format, std.datetime;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    auto S = new string[](3);
    foreach (i; 0 .. 3)
        S[i] = sread();
    S.sort();

    bool check(long i, long j, long len)
    {
        auto A = S[0], B = S[1], C = S[2];
        foreach (k; 0 .. len)
        {
            bool innerA = k < A.length;
            bool innerB = i <= k && k < i + B.length;
            bool innerC = j <= k && k < j + C.length;
            if (innerA && innerB && !(A[k] == B[k - i] || A[k] == '?' || B[k - i] == '?'))
                return false;
            if (innerA && innerC && !(A[k] == C[k - j] || A[k] == '?' || C[k - j] == '?'))
                return false;
            if (innerB && innerC && !(B[k - i] == C[k - j] || B[k - i] == '?' || C[k - j] == '?'))
                return false;
        }
        return true;
    }

    long ans = long.max;
    do
    {
        foreach (i; 0 .. S[0].length + 1)
            foreach (j; i .. max(S[0].length, i + S[1].length) + 1)
            {
                long len = max(S[0].length, i + S[1].length, j + S[2].length);
                if (check(i, j, len))
                {
                    // writeln(i, " ", j, " ", len);
                    ans = ans.min(len);
                }
                // writeln(i, " ", j, " ", i + S[2].length);
            }
    }
    while (nextPermutation(S));
    writeln(ans);
}
