fun main(args: Array<String>) {
  val N = readString()

  val O = Array(N.length + 1) { 0 }
  val P = Array(N.length + 1) { 0 }
  val Q = Array(N.length + 1) { 0 }
  for (i in N.indices) O[i + 1] = N[i].toString().toInt()
  var f = 0
  for (i in N.length downTo 0) {
    val n = O[i] + f
    if (n == 5) {
      if (O[i - 1] >= 5) {
        f = 1
        P[i] = 0
      } else {
        f = 0
        P[i] = n
      }
    } else if (n > 5) {
      f = 1
      P[i] = 0
    } else {
      f = 0
      P[i] = n
    }
  }
  var g = 0
  for (i in N.length downTo 0) {
    val p = P[i] + g
    val o = O[i]
    if (p >= o) {
      Q[i] = p - o
      g = 0
    } else {
      Q[i] = p - o + 10
      g = -1
    }
  }
//  println(P.joinToString(""))
//  println(O.joinToString(""))
//  println(Q.joinToString(""))
  println(P.sum() + Q.sum())
}

fun readString() = readLine()!!
fun readStrings() = readString().split(" ")
fun readInt() = readString().toInt()
fun readInts() = readStrings().map { it.toInt() }
fun readLong() = readString().toLong()
fun readLongs() = readStrings().map { it.toLong() }
