let () = Scanf.scanf "%s\n" @@ fun s ->
  let cs = Array.init (String.length s) (String.get s) in
  let (just, over) =
    Array.fold_right (fun c (just, over) ->
      let d = Char.code c - Char.code '0' in
      (d + min just (over + 1), min (just + 10 - d) (over + 9 - d))) cs (0, 1) in
  Printf.printf "%d\n" @@ min just (over + 1)