fun main(args: Array<String>) = abc155e()

fun abc155e() {
    val s = '0' + readLine()!!
    val n = s.length

    val dp = Array(n + 1) { LongArray(2) { Long.MAX_VALUE / 1000 } }
    dp[0][0] = 0

    for (i in 0 until n) {
        for (j in 0..1) {
            val d = s[n - i - 1] - '0' + j
            for (nd in 0..9) {
                val ni = i + 1
                val change = nd - d
                val nj = if (change < 0) 1 else 0
                val b = (change + 10) % 10

                dp[ni][nj] = Math.min(dp[ni][nj], dp[i][j] + nd + b)
            }
        }
    }

    val answer = dp[n][0]

    println(answer)
}
