module Yuki open System open System.Text let solve n = let num2list n = let rec f acc n = if n < 10 then n :: acc else f (n % 10 :: acc) (n / 10) f [] n let rec list2str (acc : StringBuilder) = function | [] -> acc.ToString() | x :: xs -> list2str (acc.Append(string x)) xs let rec f (acc : StringBuilder) xs = match xs with | [] -> acc.ToString() | y :: ys -> let xsMax = xs |> List.max if y = xsMax then f (acc.Append(string y)) ys else let swapRightIdx = ys |> List.indexed |> List.findBack (fun (_, x) -> x = xsMax) |> fun (idx, _) -> idx let xs' = ys |> List.mapi (fun i v -> match i with | _ when i = swapRightIdx -> y | _ -> v) list2str (acc.Append(string ys.[swapRightIdx])) xs' n |> num2list |> f (new StringBuilder()) let N = int <| Console.ReadLine() solve N |> Console.WriteLine