#include <bits/stdc++.h>
using namespace std;
bool match(char a, char b) { return a == '?' || b == '?' || a == b; }
constexpr int MAX = 4000;
vector<int> fill_table(const string& a, const string& b, int M) {
  vector<int> res;
  ;
  ;
  int len_a = a.size(), len_b = b.size();
  for (int i = -M; i <= M; ++i) {
    bool ok = true;
    for (int j = max(0, i); j < min(len_a, i + len_b); ++j) {
      if (!match(a[j], b[j - i])) {
        ok = false;
        break;
      }
    }
    res.push_back(ok);
    if (ok) {
      ;
    };
  }
  return res;
}
int main() {
  string a, b, c;
  cin >> a >> b >> c;
  vector<int> ab = fill_table(a, b, MAX);
  int ab_begin = -b.size();
  vector<int> ac = fill_table(a, c, MAX);
  int ac_begin = -c.size();
  vector<int> bc = fill_table(b, c, 4 * MAX);
  int bc_begin = -c.size();
  int res = 1e8;
  int len_a = a.size(), len_b = b.size(), len_c = c.size();
  for (int i = -MAX; i <= MAX; ++i) {
    for (int j = -MAX; j <= MAX; ++j) {
      if (ab[i + MAX] && ac[j + MAX] && bc[j - i + 4 * MAX]) {
        res = min<int>(
            res, max<int>({len_a, len_b + i, len_c + j}) - min<int>({0, i, j}));
      }
    }
  }
  cout << res << endl;
}
