#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
bool cmp(int a, int b) { return a < b; }
long long check(long long i, long long m) {
  if (!i) return 0;
  return check(i / m, m) + i % m;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int flag = 0;
  long long n, s, b;
  cin >> n >> s;
  if (n == s)
    cout << n + 1 << endl;
  else {
    for (long long i = 2; i <= n; i++) {
      if (check(n, i) == s) {
        cout << i << endl;
        flag = 1;
        break;
      }
    }
  }
  if (flag == 0) cout << -1 << endl;
  return 0;
}
