import scala.io.StdIn

object Main {

  def main(args: Array[String]): Unit = {

    val n = StdIn.readLong()
    val s = StdIn.readLong()

    val n2 = math.sqrt(n).toLong

    var found = -1L

    if (n == s) {
      found = n + 1L
      println(found)
    }

    for (i <- 2L to n2 if found < 0L) {
      if (f(i, n) == s) {
        found = i
        println(found)
      }
    }

    if (found < 0L) {
      for (i <- (n2 - 1L).to(1L, -1L) if found < 0L) {
        val b1 = n / (i + 1) + 1
        val a1 = n % b1 + i
        val max = n / i

        findInSequence(s, a1, -i) match {
          case Some(j) if j > 0 =>
            val b = b1 + (j - 1)
            if (b <= max) {
              found = b
            }
          case _ =>
        }
      }

      println(found)
    }
  }

  def findInSequence(x: Long, a1: Long, d: Long): Option[Long] = {
    val y = x - a1 + d
    if (y % d == 0L) Some(y / d) else None
  }

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

