#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;

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