#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 2e3 + 10;
const int M = 1e4 + 10;
const int D = 4e3;
char a[N], b[N], c[N];
bool ab[M], ac[M], bc[M];//若true则不满足 
int lena, lenb, lenc;
bool check(int x, int y) {
	return (x == '?' || y == '?' || x == y);
}
int main() {
	scanf("%s%s%s", a, b, c);
	lena = strlen(a);
	lenb = strlen(b);
	lenc = strlen(c);
	for (int i = 0; i < lena; i++) {
		for (int j = 0; j < lenb; j++) {
			if (!check(a[i], b[j])) {
				ab[i - j + D] = 1;
			}
		}
	}
	for (int i = 0; i < lena; i++) {
		for (int j = 0; j < lenc; j++) {
			if (!check(a[i], c[j])) {
				ac[i - j + D] = 1;
			}
		}
	}
	for (int i = 0; i < lenb; i++) {
		for (int j = 0; j < lenc; j++) {
			if (!check(b[i], c[j])) {
				bc[i - j + D] = 1;
			}
		}
	}
	int ans = 0x7f7f7f7f;
	for (int i = -D; i <= D; i++) {
		for (int j = -D; j <= D; j++) {
			if (!ab[i + D] && !ac[j + D] && !bc[j - i + D]) {
				int l = min(0, min(i, j));
				int r = max(lena, max(lenb + i, lenc + j));
				ans = min(ans, r - l); 
			}
		}
	}
	printf("%d", ans);
	return 0;
} 