#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    string a, b, c;
    cin >> a >> b >> c;
    int na = a.size();
    int nb = b.size();
    int nc = c.size();
    vector<int> ab, bc, ac;
    
    for (int i = -nb+1; i < na; i++) {
        bool check = true;
        for (int j = max(0, i); j <= min(na-1, i+nb-1); j++) {
            if (a[j] != '?' && b[j-i] != '?' && a[j] != b[j-i]) {
                check = false;
                break;
            }
        }
        if (check) ab.push_back(i);
    }
    
    for (int i = -nc+1; i < nb; i++) {
        bool check = true;
        for (int j = max(0, i); j <= min(nb-1, i+nc-1); j++) {
            if (b[j] != '?' && c[j-i] != '?' && b[j] != c[j-i]) {
                check = false;
                break;
            }
        }
        if (check) bc.push_back(i);
    }
    
    for (int i = -nc+1; i < na; i++) {
        bool check = true;
        for (int j = max(0, i); j <= min(na-1, i+nc-1); j++) {
            if (a[j] != '?' && c[j-i] != '?' && a[j] != c[j-i]) {
                check = false;
                break;
            }
        }
        if (check) ac.push_back(i);
    }
    
    sort(ab.begin(), ab.end());
    sort(ac.begin(), ac.end());
    sort(bc.begin(), bc.end());
    
    int ans = na + nb + nc;
    
    for (int i : ab) {
        int abl = min(0, i);
        int abr = max(na-1, i+nb-1);
        ans = min(ans, nc + abr - abl + 1);
        for (int j : bc) {
            int l = min(abl, i+j);
            int r = max(abr, i+j+nc-1);
            if (i+j >= na) {
                ans = min(ans, r - l + 1);
            }
            else if (i+j+nc-1 < 0) {
                ans = min(ans, r - l + 1);
            }
            else {
                auto it = lower_bound(ac.begin(), ac.end(), i+j);
                if (it == ac.end()) continue;
                if ((*it) != i+j) continue;
                ans = min(ans, r-l+1);
            }
        }
        
        for (int k : ac) {
            int l = min(k, abl);
            int r = max(abr, k + nc - 1);
            if (ans < r-l+1) continue;
            if (i + nb - 1 < k) {
                ans = min(ans, r - l + 1);
            }
            else if (k + nc - 1 < i) {
                ans = min(ans, r - l + 1);
            }
            else {
                auto it = lower_bound(bc.begin(), bc.end(), k-i);
                if (it == bc.end()) continue;
                if ((*it) != k-i) continue;
                ans = min(ans, r-l+1);
            }
        }
    }
    
    for (int j : bc) {
        int bcr = max(nb-1, j+nc-1);
        int bcl = min(0, j);
        ans = min(ans, na + bcr - bcl + 1);
        for (int k : ac) {
            int l = min(-k+j, bcl);
            int r = max(bcr, -k+j + na - 1);
            if (ans < r-l+1) continue;
            if (-k+j+na-1 < 0) {
                ans = min(ans, r - l + 1);
            }
            else if (nb-1 < -k+j) {
                ans = min(ans, r - l + 1);
            }
        }
    }
    
    for (int k : ac) {
        int acr = max(na-1, k+nc-1);
        int acl = min(0, k);
        ans = min(ans, nb + acr - acl + 1);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256