fun main(args: Array<String>) {
    val s0 = Array<String>(3) { next() }.sorted()
    val s = Array<String>(3) { s0[it] }
    val MX = 2001
    var ans = MX * 3
    val d = Array(3) { Array(3) { BooleanArray(MX) } }
    do {
        for (i in 0 until 3) for (j in 0 until 3) for (k in 0 until s[i].length) {
            if (i >= j) continue
            var ok = true
            for (ni in k until s[i].length) {
                val nj = ni - k
                if (nj >= s[j].length) break
                if (s[i][ni] == '?' || s[j][nj] == '?') continue
                if (s[i][ni] != s[j][nj]) ok = false
            }
            d[i][j][k] = ok
        }
        fun f(i: Int, j: Int, k: Int) = if (k >= s[i].length) true else d[i][j][k]
        for (x in 0 until MX) for (y in 0 until MX) {
            if (!f(0, 1, x)) continue
            if (!f(1, 2, y)) continue
            if (!f(0, 2, x + y)) continue
            val now = listOf(s[0].length, x + s[1].length, x + y + s[2].length).max()!!
            ans = Math.min(ans, now)
        }
    } while (nextPermutation(s))
    println(ans)
}
fun <T: Comparable<T>> nextPermutation(a: Array<T>): Boolean {
    fun find(dest: T, s: Int, e: Int): Int {
        if (s == e) return s
        val m = (s + e + 1) / 2
        return if (a[m] <= dest) find(dest, s, m - 1) else find(dest, m, e)
    }
    for (i in a.size - 1 downTo 1) {
        if (a[i - 1] < a[i]) {
            val swapIndex = find(a[i - 1], i, a.size - 1)
            val temp: T = a[swapIndex]
            a[swapIndex] = a[i - 1]
            a[i - 1] = temp
            a.sort(i, a.size)
            return true
        }
    }
    return false
}
fun next() = readLine()!!
