#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<bool> ab(100000, true);
vector<bool> bc(100000, true);
vector<bool> ac(100000, true);
bool judge(char c1, char c2) { return (c1 == '?' || c2 == '?' || c1 == c2); }
int main() {
  string a, b, c;
  cin >> a >> b >> c;
  int A = a.length();
  int B = b.length();
  int C = c.length();
  int i, j;
  for (int i = 0; i < (int)(A); i++)
    for (int j = 0; j < (int)(B); j++)
      if (!judge(a[i], b[j])) ab.at(i - j + 50000) = false;
  for (int i = 0; i < (int)(A); i++)
    for (int j = 0; j < (int)(C); j++)
      if (!judge(a[i], c[j])) ac.at(i - j + 50000) = false;
  for (int i = 0; i < (int)(A); i++)
    for (int j = 0; j < (int)(C); j++)
      if (!judge(b[i], c[j])) bc.at(i - j + 50000) = false;
  int ans = 6000;
  for (int i = (-4000); i <= (4000); i++) {
    for (int j = (-4000); j <= (4000); j++) {
      if (ab.at(i + 50000) && ac.at(j + 50000) && bc.at(j - i + 50000)) {
        int L = min(0, min(i, j));
        int R = max(A, max(B + i, C + j));
        ans = min(ans, R - L);
      }
    }
  }
  cout << ans << endl;
}
