module Yuki open System open System.Text let inline ret x = Some x let inline (>>=) (m: Option<'a>) f = match m with | Some x -> f x | None -> None type MaybeBuilder () = member __.Return(x) = ret x member __.Bind(m, f) = m >>= f let maybe = MaybeBuilder () 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 = let xsMax = xs |> List.max match xs with | x :: xs when x = xsMax -> f (acc.Append(string x)) xs | x :: xs -> let swapRightIdx = xs |> List.indexed |> List.findBack (fun (_, x) -> x = xsMax) |> fun (idx, _) -> idx let xs' = xs |> List.mapi (fun i v -> match i with | _ when i = swapRightIdx -> x | _ -> v) list2str (acc.Append(string xs.[swapRightIdx])) xs' | [] -> failwith "" n |> num2list |> f (new StringBuilder()) let N = int <| Console.ReadLine() solve N |> Console.WriteLine