Scanf.scanf "%d %d" (fun n s ->
    let rec f b n = if n < b then n else f b (n/b) + (n mod b) in

    let step3 n =
        if n = s then n + 1 else -1
    in

    let step2 n =
        let ns = n - s in
        let rec loop a =
            if a = 0 then step3 n else
                if ns mod a = 0 && s - a <= ns / a then ns / a + 1 else
                    loop (a - 1)
        in
        loop (min s (int_of_float (floor (sqrt (float ns) +. 0.5))))
    in

    let rec loop i =
        if i * i > n then step2 n else
            if f i n = s then i else
                loop (i + 1)
    in
    loop 2 |> Printf.printf "%d\n"
)