let () =
    let s = "0" ^ (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 arr = Array.of_list (loop (String.length s - 1) []) in
    let sum = Array.fold_left (+) 0 arr in
    let rec loop i m a b =
        if i < 0 then m else
            let d = arr.(i) in
            if d < 5 || d = 5 && i > 0 && arr.(i - 1) < 5 then loop (i - 1) m a b else
                if i > 0 then (
                    if arr.(i - 1) = 9 then (
                        let rec loop2 i a =
                            if arr.(i - 1) = 9 then (
                                loop2 (i - 1) (a - 9) 
                            ) else (a, i)
                        in
                        let b = b + 10 - arr.(i) in
                        let a, i = loop2 i (a - arr.(i)) in
                        let a = a + 1 in
                        arr.(i - 1) <- arr.(i - 1) + 1;
                        let m = min m (a + b) in
                        loop (i - 1) m a b
                    ) else (
                        let a = a      - arr.(i) in
                        let b = b + 10 - arr.(i) in
                        let a = a + 1 in
                        arr.(i - 1) <- arr.(i - 1) + 1;
                        let m = min m (a + b) in
                        loop (i - 1) m a b
                    )
                ) else (
                        let a = a      - arr.(i) in
                        let b = b + 10 - arr.(i) in
                        let a = a + 1 in
                        let m = min m (a + b) in
                        loop (i - 1) m a b
                )
    in
    loop (Array.length arr - 1) sum sum 0 |> Printf.printf "%d\n"