let () =
    let s = read_line () in
    let rec loop i acc =
        if i < 0 then acc else loop (i - 1) ((int_of_char s.[i] - int_of_char '0') :: acc)
    in
    let (just, over) =
        loop (String.length s - 1) [] |>
        List.fold_left (fun (just, over) d ->
            min (just + d) (over + d + 1), min (just + 10 - d) (over + 9 - d)) (0, 1) 
    in
    Printf.printf "%d\n" (min just (over + 1))