#include <bits/stdc++.h>
using namespace std;
long long int add_digits(long long int n, long long int b = 10) {
  long long int sum = 0;
  while (n != 0) {
    sum += n % b;
    n /= b;
  }
  return sum;
}
int main() {
  long long int n, s;
  long long int ans = 9999999;
  cin >> n >> s;
  if (n == s)
    ans = n + 1;
  else {
    for (long long int i = 2; i < sqrt(n) + 1; i++) {
      if (add_digits(n, i) == s) ans = min(ans, i);
    }
    if (ans == 9999999) {
      for (long long int i = sqrt(n) + 1; i < n + 1; i++) {
        if (add_digits(n, i) == s) ans = min(ans, i);
      }
    }
  }
  cout << (ans == 9999999 ? -1 : ans) << endl;
  return 0;
}
