import java.io.{BufferedReader, InputStream, InputStreamReader}
import java.util.StringTokenizer

import scala.collection.mutable
import scala.util.Sorting
import math.{abs, max, min}

object Main {
  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val N = sc.nextLong()
    val S = sc.nextLong()

    val rt = math.ceil(math.sqrt(N)).toInt

    def underRt: Option[Long] = {
      def ok(n: Long, s: Long, b: Long): Boolean = {
        if (s < 0) false
        else if (n < b) n == s
        else {
          val r = n % b
          ok(n / b, s - r, b)
        }
      }

      (2 until rt) find { b =>
        ok(N, S, b)
      } map (_.toLong)
    }

    def overRt: Option[Long] = {
      (1 to rt) find { i =>
        val b = N / i
        S - i == N - b * i
      } map (i => (N / i).toLong)
    }

    def nEqS: Option[Long] = {
      if (N == S) Some(N + 1) else None
    }

    val ans = nEqS orElse underRt orElse overRt getOrElse -1
    println(ans)
  }


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


  private class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = null

    def next: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next.toInt
    def nextLong(): Long = next.toLong
    def nextChar(): Char = next.charAt(0)
  }

}
