#define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}


unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

#define int ll
void solve() {
    vector<string> a(3);
    REP(i, 3) {
        cin >> a[i];
    }
    sort(ALL(a));
    int ans = 1e9;
    do {
        int cnt[3][3][6001] = {};
        for (int i = 0; i < 3; ++i) {
            for (int q = i + 1; q < 3; ++q) {
                for (int j = 0; j <= 6000; ++j) {
                    int ok = 1;
                    for (int t = 0; t + j < a[i].length() && t < a[q].length(); ++t) {
                        if (a[i][t + j] != '?' && a[q][t] != '?') {
                            if (a[i][t + j] != a[q][t]) {
                                ok = 0;
                                break;
                            }
                        }
                    }
                    cnt[i][q][j] = ok;
                }
            }
        }
        for (int i = 0; i <= a[0].length(); ++i) {
            for (int q = 0; q <= a[1].length(); ++q) {
                if (cnt[0][1][i] == 1 && cnt[0][2][i+q] == 1 && cnt[1][2][q] == 1) {
                    int hoge = max({ (int)a[0].length(),i + (int)a[1].length(),i + q + (int)a[2].length() });
                    ans = min(ans, hoge);
                }
            }
        }
    } while (next_permutation(ALL(a)));
    int go_ans = 1e9;
    sort(ALL(a));
    int died = 0;
    do {
        for (int q = 0; q <= a[0].length(); ++q) {
            if (clock() > 1.2 * CLOCKS_PER_SEC) {
                died = 1;
                break;
            }
            int ok = 1;
            for (int j = 0; j + q < a[0].length() && j < a[1].length(); ++j) {
                if (a[0][j + q] != '?' && a[1][j] != '?') {
                    if (a[0][j + q] != a[1][j]) {
                        ok = 0;
                        break;
                    }
                }
            }
            if (ok == 0) continue;
            string next = a[0];
            for (int j = 0; j < a[1].length(); ++j) {
                if (j + q >= a[0].length()) {
                    next.push_back(a[1][j]);
                    continue;
                }
                if (a[0][j + q] == '?' && a[1][j] == '?') {
                }
                else if (a[0][j + q] == '?') {
                    next[j + q] = (a[1][j]);
                }
                else {
                    next[j + q] = (a[0][j + q]);
                }
            }
            for (int j = 0; j <= next.length(); ++j) {
                int ok = 1;
                int rema = a[2].length();
                for (int t = 0; t + j < next.length() && t < a[2].length(); ++t) {
                    if (next[t + j] != '?' && a[2][t] != '?') {
                        if (next[t + j] != a[2][t]) {
                            ok = 0;
                            break;
                        }
                    }

                    rema--;

                }
                if (ok == 0) continue;
                go_ans = min(go_ans, (long long)next.length() + rema);
                break;
            }
        }
    } while (next_permutation(ALL(a)));
    if (died == 0) {
        cout << go_ans << endl;
        return;
    }
    cout << ans << endl;
}

#undef int
int main() {
    init();
    solve();
}

Measured memory_bytes: 768