#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    string a, b, c;
    cin >> a >> b >> c;

    vector<bool> ab(20000, true), bc(20000, true), ac(20000, true);
    REP(i, a.size()) REP(j, b.size()) if(a[i]!='?' && b[j]!='?' && a[i]!=b[j]) ab[i-j+10000] = false;
    REP(i, b.size()) REP(j, c.size()) if(b[i]!='?' && c[j]!='?' && b[i]!=c[j]) bc[i-j+10000] = false;
    REP(i, a.size()) REP(j, c.size()) if(a[i]!='?' && c[j]!='?' && a[i]!=c[j]) ac[i-j+10000] = false;

    ll ret = INF;
    FOR(i, -4000, 4000) FOR(j, -4000, 4000) {
        if(ab[i+10000] && ac[j+10000] && bc[j-i+10000]) {
            ll l = min({0LL, i, j});
            ll r = max({(ll)a.size(), i+(ll)b.size(), j+(ll)c.size()});
            chmin(ret, r-l);
        }
    }
    cout << ret << endl;

    return 0;
}