import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;


long f(long n, long b) {
  long s = 0;
  while (n > 0){
    s += n % b;
    n /= b;
  }
  return s;
}


void main() {
  auto n = readln.chomp.to!long;
  auto s = readln.chomp.to!long;

  if (s > n) {writeln(-1); return;}
  if (s == n) {writeln(n+1); return;}

  long b = 2;
  while (b*b <= n) {
    if (f(n, b) == s) {writeln(b); return;}
    b += 1;
  }

  long p = 1;
  long q;
  while (p*p <= n) {
    if ((n-s+p) % p == 0) {
      b = (n-s+p) / p;
      q = s-p;
      if (b >= 2 && b <= n && p < b && q >= 0 && q < b && f(n, b) == s) {
        b.writeln;
        return;
      }
    }
    p += 1;
  }
  writeln(-1);
}
