#include <bits/stdc++.h>
using namespace std;
#pragma optimize("O3")
const long long N = 2e3 + 5, mod = 1e9 + 7, inf = 1e18 + 7;
long long n, s;
long long cal(long long x, long long y) {
  long long ans = 0;
  while (y) {
    ans += y % x;
    y /= x;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long ans = inf;
  cin >> n >> s;
  if (s == 1) return cout << n, 0;
  ;
  if (n == s) return cout << n - 1, 0;
  ;
  if (n < s) return cout << -1, 0;
  ;
  for (long long i = 2; i <= sqrt(n); i++) {
    if (cal(i, n) == s) return cout << ans, 0;
    ;
  }
  if (cal(n - s, n) == s) {
    ans = min(ans, n - s);
  }
  for (long long i = 2; i <= sqrt(n - s); i++) {
    if ((n - s) % i == 0) {
      if (cal(i, n) == s && i > sqrt(n)) {
        ans = min(ans, i);
      }
      if (cal((n - s) / i, n) == s && (n - s) / i > sqrt(n)) {
        ans = min(ans, (n - s) / i);
      }
    }
  }
  cout << (ans == inf ? -1 : ans);
}
