#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int M = 2005;
bool match(char x, char y) { return (x == y) || (x == '?') || (y == '?'); }
int main() {
  string a, b, c;
  cin >> a >> b >> c;
  int la = a.size();
  int lb = b.size();
  int lc = c.size();
  vector<bool> ab(4 * M + 5, true);
  vector<bool> bc(4 * M + 5, true);
  vector<bool> ca(4 * M + 5, true);
  for (int i = int(0); i < int(la); i++)
    for (int j = int(0); j < int(lb); j++)
      if (!match(a[i], b[j])) ab[2 * M + i - j] = false;
  for (int i = int(0); i < int(lb); i++)
    for (int j = int(0); j < int(lc); j++)
      if (!match(b[i], c[j])) bc[2 * M + i - j] = false;
  for (int i = int(0); i < int(lc); i++)
    for (int j = int(0); j < int(la); j++)
      if (!match(c[i], a[j])) ca[2 * M + i - j] = false;
  int ans = INF;
  for (int i = int(-2 * M); i < int(2 * M); i++)
    for (int j = int(-2 * M); j < int(2 * M); j++) {
      if (j - i <= 2 * M && j - i >= -2 * M && ab[2 * M + i] &&
          bc[2 * M + j - i] && ca[2 * M - j]) {
        chmin(ans, max({la, i + lb, j + lc}) - min({0, i, j}));
      }
    }
  cout << ans << endl;
  return 0;
}
