object Main extends App {

  import scala.io.StdIn._

  val n = readLong()
  val s = readLong()

  val rt = Math.sqrt(n).toLong

  def digitSum(b : Long, n : Long) : Long = if (n < b) n else digitSum(b , n / b) + n % b

  def checkOne = if (n == s) Some(1) else None

  def searchFirstHalf(now : Long):Option[Long] = {
    if (now >= rt)  None
    else if (digitSum(now, n) == s) Some(now)
    else searchFirstHalf(now + 1)
  }

  def searchSecondHalf(now : Long) : Option[Long] = {
    if (now <= 0) None
    else {
      val b = (n + now - s) / now
      if (digitSum(b, n) == s) {
        Some(b)
      }
      else searchSecondHalf(now - 1)
    }
  }

  val calc = if (n < s) None else checkOne orElse searchFirstHalf(2) orElse searchSecondHalf(rt)

  println(calc getOrElse -1)
}