#include <bits/stdc++.h>
using namespace std;
long long f(long long b, long long n) {
  long long answer = 0;
  while (n >= b) {
    answer += n % b;
    n /= b;
  }
  return answer + n;
}
long long SolveSmall(long long n, long long s) {
  vector<long long> divs;
  for (long long j = 2; j * j <= n; j++) {
    if (n % j == 0) {
      long long m = n;
      while (m % j == 0) {
        m /= j;
      }
      if (m == s && j >= s + 1) {
        return j;
      }
    }
  }
  if (n > 1)
    return n;
  else
    return -1;
}
long long SolveRev(long long n, long long s) {
  if (n - s + 1 >= s) {
    return n - s + 1;
  } else
    return -1;
}
long long SolveSmall2(long long n, long long s) {
  vector<long long> divs;
  for (long long x = 2; x * x <= n; x++) {
    if ((n - s) % x == 0) {
      divs.push_back(x);
      divs.push_back((n - s) / x);
    }
  }
  sort(divs.begin(), divs.end());
  for (int j = 0; j < divs.size(); j++) {
    long long x = divs[j];
    long long b = x + 1;
    long long d1 = (n - s) / (b - 1);
    long long d0 = s - d1;
    if (d1 >= 0 && d1 <= b - 1 && d0 >= 0 && d0 <= b - 1) {
      return b;
    }
  }
  return -1;
}
long long Solve(long long n, long long s) {
  long long prev = -1;
  int time_since_last_increase = 0;
  long long low = 100000000 + 1;
  for (int j = 2; j <= 50000000; j++) {
    long long curr = f(j, n);
    if (curr == s) {
      return j;
    }
    prev = curr;
  }
  long long high = 1000000000000000000ll;
  while (low < high) {
    long long mid = (low + high) / 2;
    if (f(mid, n) >= s) {
      high = mid;
    } else {
      low = mid + 1;
    }
  }
  vector<long long> answers;
  answers.push_back(f(low, n) == s ? low : -1);
  answers.push_back(SolveSmall(n, s));
  answers.push_back(SolveRev(n, s));
  answers.push_back(SolveSmall2(n, s));
  sort(answers.begin(), answers.end());
  for (int j = 0; j < answers.size(); j++) {
    if (answers[j] >= 2 && f(answers[j], n) == s) return answers[j];
  }
  return -1;
}
int main() {
  long long n, s;
  cin >> n >> s;
  cout << Solve(n, s) << endl;
  return 0;
}
