import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next()
  val l = N.length

  def toInt(c: Char) = c.toString.toInt

  val dp = Array.ofDim[Long](l+1, 2)
  for (i <- 0 to l; k <- 0 to 1) {
    dp(i)(k) = Long.MaxValue
  }

  dp(0)(0) = 0
  dp(0)(1) = 1
  for (i <- 1 until l) {
    val now = toInt(N(i-1))
    val next = toInt(N(i))

    dp(i)(0) = Math.min(dp(i-1)(0) + now, dp(i-1)(1) + (10-now))

    val option = if (next == 0) 0 else 1
    val otsuri = (10 - now) - option
    dp(i)(1) = Math.min(dp(i-1)(0) + 1 + now, dp(i-1)(1) + otsuri)
  }

  val now = toInt(N(l-1))
  val ans = Math.min(dp(l-1)(0) + now, dp(l-1)(1) + (10-now))

  println(ans)
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.toList))
  }
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
