let rec f acc b = function
  | 0 -> acc
  | n -> f (n mod b + acc) b (n / b)
let f = f 0

let () = Scanf.scanf "%d %d" @@ fun n s ->
  let r = int_of_float @@ sqrt @@ float_of_int n in
  try
    Array.init r (( + ) 2)
    |> Array.to_list
    |> List.find (fun b -> f b n = s)
    |> Printf.printf "%d\n"
  with Not_found ->
    try
      Array.init (n / r) (( + ) 1)
      |> Array.to_list
      |> List.rev_map (fun p -> (n - 1) / p + 1)
      |> List.filter (( <= ) 2)
      |> List.find (fun b -> f b n = s)
      |> Printf.printf "%d\n"
    with Not_found -> print_endline "-1"