fun main(args: Array<String>) {
    val N = readLine()!!
    var ans = 0
    var next = 0
    var five = false
    N.reversed().toCharArray().map { c -> c - '0' }.forEach { i ->
        var ii = i + next
        if (five && ii >= 5) {
            ii++
        }
        five = false
        if (ii < 5) {
            ans += ii
            next = 0
        } else if (ii == 5) {
            ans += 5
            five = true
            next = 0
        } else {
            ans += (10 - ii)
            next = 1
        }
    }
    if (next == 1) ans++
    println(ans)
}