#include <iostream>
#include <algorithm>
 
using namespace std;
 
int main()
{
    long long n, s;
    cin>>n>>s;
 
    for (long long b=2; b*b<=n; b++)
    {
        long long t = n;
        long long a = 0LL;
        while (t>0)
            a += t%b,
            t /= b;
        if (a==s)
        {
            cout<<b<<endl;
            return 0;
        }
    }
 
    long long inf = 1000000000000LL;
    long long ans = inf;
 
    if (n==s)
        ans = s+1;
 
    for (long long d=1; d*d<=n; d++)
    {
        long long r = s - d;
        long long b = (n-r)/d;
        if (0<=r && r<b && 2<=b && n/b+n%b==s && n/b<b)
            ans = min(ans, b);
    }
 
    cout<<(ans<inf ? ans : -1)<<endl;
}