fun main(args: Array<String>) {
    val A = next()
    val B = next()
    val C = next()
    val al = A.length
    val bl = B.length
    val cl = C.length
    val M = 2000
    val ab = BooleanArray(100000)
    val ac = BooleanArray(100000)
    val bc = BooleanArray(100000)
    fun match(c1: Char, c2: Char) = c1 == c2 || c1 == '?' || c2 == '?'
    for (i in 0 until al)
        for (j in 0 until bl)
            if (!match(A[i], B[j])) ab[i - j + 50000] = true
    for (i in 0 until al)
        for (j in 0 until cl)
            if (!match(A[i], C[j])) ac[i - j + 50000] = true
    for (i in 0 until bl)
        for (j in 0 until cl)
            if (!match(B[i], C[j])) bc[i - j + 50000] = true
    var ans = 3 * M
    for (i in -2 * M .. 2 * M)
        for (j in -2 * M .. 2 * M)
            if (!ab[i + 50000] && !ac[j + 50000] && !bc[j - i + 50000]) {
                val L = Math.min(0, Math.min(i, j))
                val R = Math.max(al, Math.max(bl + i, cl + j))
                ans = Math.min(ans, R - L)
            }
    println(ans)
}
fun Long.isEven() = (this % 2L) == 0L
fun Long.isOdd() = !this.isEven()
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun nextLong(offset: Long = 0L) = nextLong() + offset
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }
fun listOfInt(offset: Int = 0) = listOfString().map { Integer.parseInt(it) + offset }
fun listOfLong() = listOfString().map { java.lang.Long.parseLong(it) }
fun listOfLong(offset: Long = 0L) = listOfString().map { java.lang.Long.parseLong(it) + offset }
fun listOfDouble() = listOfString().map { it.toDouble() }
