#include <bits/stdc++.h>
using namespace std;
const int maxn = 4010;
int con[3][3][3 * maxn];
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  vector<string> vec;
  for (int d = 0; d < 3; d++) {
    string s;
    cin >> s;
    vec.push_back(s);
  }
  for (int d = 0; d < 3; d++) {
    for (int i = 0; i < 3; i++) {
      if (d != i) {
        for (int j = 0; j <= vec[d].size() + vec[i].size(); j++) {
          bool check = 1;
          int tk = vec[i].size() - 1;
          int g = j - 1;
          while (tk >= 0 && g >= vec[d].size()) {
            tk--;
            g--;
          }
          for (; g >= 0 && tk >= 0; g--) {
            if (vec[d][g] != vec[i][tk] && vec[d][g] != '?' &&
                vec[i][tk] != '?') {
              check = 0;
              break;
            }
            tk--;
          }
          if (check) {
            con[d][i][j] = 1;
          }
        }
      }
    }
  }
  int ans = vec[0].size() + vec[1].size() + vec[2].size();
  for (int i = 0; i <= vec[0].size() + vec[1].size() + vec[2].size() + 2; i++) {
    for (int j = 0; j <= vec[0].size() + vec[1].size() + vec[2].size() + 2;
         j++) {
      if (i <= vec[0].size() + vec[1].size() && !con[0][1][i]) {
        continue;
      }
      if (j <= vec[0].size() + vec[2].size() && !con[0][2][j]) {
        continue;
      }
      if (j >= i) {
        if (i + vec[2].size() >= j && !con[2][1][i - (j - vec[2].size())]) {
          continue;
        }
      } else {
        if (j + vec[1].size() >= i && !con[1][2][j - (i - vec[1].size())]) {
          continue;
        }
      }
      int sz0 = vec[0].size(), sz1 = vec[1].size(), sz2 = vec[2].size();
      int l = min(0, min(i - sz1, j - sz2));
      int r = max(sz0, max(i, j));
      ans = min(ans, r - l);
    }
  }
  cout << ans;
}
