fun main(args: Array<String>) {
    val N = readLine()!!.map { it - '0' }
    val l = N.size
    val dp = Array(l + 1) { IntArray(2) {0} }

    dp[0][1] = 1

    for (i in 0 until l) {
        dp[i + 1][0] = Math.min(dp[i][0] + N[i], dp[i][1] + 10 - N[i])
        dp[i + 1][1] = dp[i][1] + 9 - N[i]
//        if (N[i] < 9) {
            dp[i + 1][1] = Math.min(dp[i + 1][1], dp[i][0] + N[i] + 1)
//        }
    }
    println(dp[l][0])
}
