object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n, s = sc.nextLong()

  def f(b: Long, n: Long, res: Long = 0): Long = {
    if (n == 0) res else f(b, n/b, res + (n % b))
  }

  if (n == s) {
    println(n+1)
  } else {
    var b = 2L
    var continue = true
    while (continue && b * b <= n) {
      if (f(b, n) == s) {
        println(b)
        continue = false
      }
      b += 1
    }

    var p = 1L
    while (continue && p * p <= n) {
      if ((n-s) % p == 0) {
        val b = (n-s)/p + 1
        if (f(b, n) == s) {
          println(b)
          continue = false
        }
      }
      p += 1
    }

    if (continue) {
      println(-1)
    }
  }
}
