#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <math.h>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int main(){
  ll n,s;
  cin>>n>>s;
  ll q=sqrt(n);
  if(n==s){
    cout<<n+1<<endl;
    return 0;
  }

  if(n<s){
    cout<<-1<<endl;
    return 0;
  }
  for(int i=2;i<=q;i++){
    ll dis=0,tmp=n;
    while(tmp!=0){
      dis+=(tmp%i);
      tmp/=i;
    }
    if(dis==s){
      cout<<i<<endl;
      return 0;
    }
  }

  for(int p=(ll)q;p>0;p--){
    ll b=(n-s)/p+1;
    ll dis=0,tmp=n;
    if(b<2) continue;
    while(tmp!=0){
      dis+=(tmp%b);
      tmp/=b;
    }
    if(dis==s){
      cout<<b<<endl;
      return 0;
    }
  }
  cout<<-1<<endl;
  return 0;
}
