#include <bits/stdc++.h>
using namespace std;
template <class T>
void chmax(T &x, T y) {
  if (x < y) x = y;
}
template <class T>
void chmin(T &x, T y) {
  if (x > y) x = y;
}
bool comp(char x, char y) {
  if (x == '?' or y == '?') return true;
  return x == y;
}
using p32 = pair<int, int>;
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  string s[3];
  cin >> s[0] >> s[1] >> s[2];
  const int offs = 4040;
  bool dif[3][3][offs * 3] = {};
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      auto &now = dif[i][j];
      for (int x = 0; x < (int)s[i].size(); ++x) {
        for (int y = 0; y < (int)s[j].size(); ++y) {
          now[x - y + offs] |= !comp(s[i][x], s[j][y]);
        }
      }
    }
  }
  int ans = 6000;
  for (int i = -offs; i < offs; ++i) {
    if (dif[0][1][i + offs]) continue;
    for (int j = -offs; j < offs; ++j) {
      if (!dif[0][2][j + offs] and
          (j - i + offs < 0 or !dif[1][2][j - i + offs])) {
        int l = min({0, i, j});
        int r =
            max({(int)s[0].size(), i + (int)s[1].size(), j + (int)s[2].size()});
        chmin(ans, r - l);
      }
    }
  }
  cout << ans << "\n";
}
