module Yuki open System open System.Text let solve n = let num2list n = (string n).ToCharArray() |> Array.map (string >> int) |> Array.toList 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 ys' = ys |> List.mapi (fun i v -> match i with | _ when i = swapRightIdx -> y | _ -> v) list2str (acc.Append(string ys.[swapRightIdx])) ys' n |> num2list |> f (new StringBuilder()) let N = int <| Console.ReadLine() solve N |> Console.WriteLine