#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
class EThreeSubstrings {
 public:
  bool match(char c1, char c2) { return (c1 == '?' || c2 == '?' || c1 == c2); }
  void solve(istream& cin, ostream& cout) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string a, b, c;
    cin >> a >> b >> c;
    int A = a.size();
    int B = b.size();
    int C = c.size();
    vector<bool> ab(20000, true);
    vector<bool> bc(20000, true);
    vector<bool> ac(20000, true);
    for (int i = 0; i < (int)(A); i++)
      for (int j = 0; j < (int)(B); j++) {
        if (!match(a[i], b[j])) ab[i - j + 10000] = false;
      }
    for (int i = 0; i < (int)(A); i++)
      for (int j = 0; j < (int)(C); j++) {
        if (!match(a[i], c[j])) ac[i - j + 10000] = false;
      }
    for (int i = 0; i < (int)(B); i++)
      for (int j = 0; j < (int)(C); j++) {
        if (!match(b[i], c[j])) bc[i - j + 10000] = false;
      }
    int ans = A + B + C;
    for (int i = -4000; i <= 4000; i++) {
      for (int j = -4000; j <= 4000; j++) {
        if (ab[i + 10000] && ac[j + 10000] && bc[j - i + 10000]) {
          int L = min({0, i, j});
          int R = max({A, i + B, j + C});
          chmin(ans, R - L);
        }
      }
    }
    cout << ans << endl;
  }
};
signed main() {
  EThreeSubstrings solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
