import std.algorithm,
       std.string,
       std.range,
       std.stdio,
       std.conv,
       std.math;

long f(long b, long n) {
 return (n < b)
          ? n
          : f(b, n/b) + n % b;
}

long solve(long N, long S) {
  if (N < S) {
    return -1;
  }

  if (N == S) {
    return N + 1;
  }


  foreach (i; 2.iota(sqrt(N.to!float) + 1)) {
    auto j = i.to!long;
    if (f(j, N) == S) {
      return j;
    }
  }
 
  long ans = long.max;

  foreach (i; 1.iota(sqrt(N.to!float) + 1)) {
    auto j = i.to!long;
    long b = (N - S) / j + 1;
    if (b == 1) continue;
    if (f(b, N) == S) ans = min(ans, b);
  }

  if (ans < long.max) {
    return ans;
  } else {
    return -1;
  }
}

void main() {
  long N = readln.chomp.to!long,
       S = readln.chomp.to!long;
  writeln(solve(N, S));
}