#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f(ll b, ll n){
    if (n < b)return n;
    return f(b, n/b)+n%b;
}
int main(){
    ll n, s;cin >> n >> s;
    if (n < s){
        cout << -1 << endl;
    }
    else if (n == s){
        cout << n+1 << endl;
    }
    else{
        ll ans = 1e12;
        for (ll b = 2; b*b <= n; b++)
        {
            if (f(b, n) == s){
                ans = min(ans, b);
            }
        }
        for (ll p = 1; p*p < n; p++)
        {
            ll b = (n-s)/p+1;
            if (b < 2)continue;
            if (f(b, n) == s){
                ans = min(ans, b);
            }
        }
        if (ans == ll(1e12)){
            cout << -1 << endl;
            return 0;
        }
        cout << ans << endl;
    }
    
    
}