#include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long 
#define M 1000000007
#define P pair<int,int>

ll n,s;

ll f(ll b,ll n){
  if(n<b) return n;
  
  return f(b,n/b) + n%b;
}

int main(){
  cin>>n>>s;
  if(s==n){
    cout<<n+1<<endl;
    return 0;
  }
  if(s<n){
    ll b;
    for(b=2;b*b<=n;b++)
      if(f(b,n)==s){
        cout<<b<<endl;
        return 0;
      }
    for(ll i=b;i>=1;i--){
      b = (n-s)/i+1;
      if(b>=2&&f(b,n)==s){
        cout<<b<<endl;
        return 0;
      }
    }
  }

  cout<<-1<<endl;

  return 0;
}
