//n,s,b
//b<=sqrt(n)
//b>sqrt(n),n=a0+a1*b,s=a0+a1 n-s=a1(b-1)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
long long n, s;
inline long long f(long long b, long long n)
{
    return n < b ? n : f(b, n / b) + (n % b);
}
int main()
{
    scanf("%lld%lld", &n, &s);
    if (n < s)
        return printf("-1"), 0;
    if (n == s)
        return printf("%lld", n + 1), 0;
    for (int i = 2; i <= sqrt(n)+1; ++i)
        if (f(i, n) == s)
            return printf("%lld", i), 0;
    long long temp = n - s, mini = 999999999999;
    for (int i = 1; i <= sqrt(temp); ++i)
        if (!(temp % i))
            if (f(temp / i + 1, n) == s)
                mini = min(mini, temp / i + 1);
    printf("%lld", mini == 999999999999 ? -1 : mini);
    return 0;
}