#include <bits/stdc++.h>
using namespace std;
long digsum(long n, long b) {
  if (b == 1) return n;
  if (n < b) return n;
  return digsum(floor(n / b), b) + n % b;
}
int main(int argc, char *argv[]) {
  long n;
  long s;
  cin >> n;
  cin >> s;
  if (s == 1) return cout << n << endl, 0;
  for (long i = 1; i <= 1e11; i *= 10) {
    if (digsum(n, i) == s) return cout << i << endl, 0;
  }
  cout << -1 << endl;
}
