open Batteries
open BatPrintf

let scan fmt f =
  Scanf.sscanf (read_line ()) fmt f

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv


let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let s = scan "%s" identity

let () =
  let n = String.length s in
  let open Bigarray in
  let arr = Array2.create Int C_layout (n+1) 2  in
  Array2.set arr 0 1 1;
  String.iteri (fun i c  ->
      let n = Char.code c - Char.code '0' in
      let x = Array2.get arr i 0 in
      let y = Array2.get arr i 1 in
      Array2.set arr (i+1) 0 @@ List.min [x + n; y + (10-n)] ;
      Array2.set arr (i+1) 1 @@ List.min [x + n + 1; y + 10 - n - 1] ;
    ) s;
  printf "%d\n" @@ List.min [Array2.get arr n 0; 1 + Array2.get arr n 1]
