let rec select acc trace = function
  | [] -> acc
  | x :: xs -> select ((trace, x, xs) :: acc) (x :: trace) xs
let select xs = select [] [] xs

let rec digits acc = function
  | 0 -> acc
  | n -> digits (n mod 10 :: acc) (n / 10)
let digits = digits []

let () = Scanf.scanf "%d\n" (fun n ->
  let ds = digits n in
  select ds
  |> List.map (fun (l, x, r) ->
      x - 1 + 9 * List.length r + List.fold_left ( + ) 0 l)
  |> List.fold_left max (List.fold_left ( + ) 0 ds)
  |> Printf.printf "%d\n")