#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

long long min(long long a, long long b) {return a < b ? a : b;}
long long min(int a, long long b) {return a < b ? a : b;}
long long min(long long a, int b) {return a < b ? a : b;}
long long min(int a, int b) {return a < b ? a : b;}

long long max(long long a, long long b) {return a > b ? a : b;}
long long max(int a, long long b) {return a > b ? a : b;}
long long max(long long a, int b) {return a > b ? a : b;}
long long max(int a, int b) {return a > b ? a : b;}

#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const int N = 20220;
const int MOD = 1e9 + 7;
const double eps = 1e-8;
const int K = 5;

ll inq(ll x, ll y)
{
    if (y == 0) return 1;
    ll l = inq(x, y / 2);
    if (y % 2) return l * l % MOD * x % MOD;
    return l * l % MOD;
}

int ans;
string a, b, c;
int okAB[N], okAC[N], okBC[N];

void check(string let, string a, string b)
{
    for (int move = -2000; move <= 2000; move++)
    {
        int t = 1;
        for (int i = 0; i < a.size(); i++)
        {
            int j = i - move;
            if (j < 0 || j >= b.size()) continue;
            if (a[i] == '?' || b[j] == '?' || (a[i] == b[j])) continue;
            t = 0;
            break;
        }
        if (t == 0)
        {
            if (let == "AB") okAB[N / 2 + move] = 0;
            if (let == "AC") okAC[N / 2 + move] = 0;
            if (let == "BC") okBC[N / 2 + move] = 0;
        }
    }
}

signed main()
{
    srand(time(NULL));
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> a >> b >> c;
    ans = a.size() + b.size() + c.size();
    fill(okAB, okAB + N, 1);
    fill(okAC, okAC + N, 1);
    fill(okBC, okBC + N, 1);
    check("AB", a, b);
    check("AC", a, c);
    check("BC", b, c);
    for (int move1 = -4000; move1 <= 4000; move1++)
    {
        for (int move2 = -4000; move2 <= 4000; move2++)
        {
            int min0 = min(0LL, min(move1, move2));
            int max0 = max((int)a.size(), max(move1 + (int)b.size(), move2 + (int)c.size()));
            if (okAB[N / 2 + move1] && okAC[N / 2 + move2] && okBC[N / 2 + move2 - move1])
                ans = min(ans, max0 - min0);
        }
    }
    cout << ans;
}


Measured memory_bytes: 896