import java.lang.Math

fun main(args: Array<String>) {
    val N = ("0" + readInputLine() + "0").toCharArray().reversed()

    val dp = Array(N.size) { IntArray(2) }

    dp[0][0] = char2int(N[0])
    dp[0][1] = char2int(N[0])

    for (i in 1 until N.size) {
        dp[i][0] = Math.min(dp[i - 1][0] + char2int(N[i]), dp[i - 1][1] + char2int(N[i]) + 1)
        if (i != 1) {
            dp[i][1] = Math.min(dp[i - 1][0] + 10 - char2int(N[i]), dp[i - 1][1] + 10 - char2int(N[i]) - 1)
        } else {
            dp[i][1] = Math.min(dp[i - 1][0] + 10 - char2int(N[i]), dp[i - 1][0] + 10 - char2int(N[i]))
        }
    }

    println(Math.min(dp[N.size - 1][0], dp[N.size - 1][1]))
}

fun readInputLine(): String {
    return readLine()!!
}

fun char2int(c: Char): Int {
    return c - '0'
}
