#include <iostream>
#include <vector>
#include <deque>
#include <math.h>
#include <set>
#include <iomanip>
#include <time.h>
#include <list>
#include <stdio.h>
#include <queue>
#include <map>
#include <algorithm>
#include <assert.h>
#include <memory.h>

#define mk make_pair
#define sc second
#define fr first
#define pb emplace_back
#define all(s) s.begin(), s.end()
#define sz(s) ( (int)s.size() )
#define int long long

using namespace std;

const int N = 1e5 + 5;

int n, s, m;

int f (int n, int x)
{
    if (n == 0)
        return 0;

    return f(n / x, x) + n % x;
}

main ()
{
    cin >> n >> s;
    for (int i = 2; i * i <= n; i++)
    {
        if ( f(n, i) == s )
        {
            cout << i << endl;
            return 0;
        }
    }
    if (n < s)
    {
        puts("-1");
        return 0;
    }
    m = n - s;
    for (int i = sqrt(m) + 1; i >= 1; i--)
    {
        if (m % i == 0)
        {
            int a = m / i, b = i;

            if (a < b)
                swap(a, b);
           if ( f( n, a + 1 ) == s  )
           {
               cout << a + 1 << endl;
               return 0;
           }
        }
    }
    puts("-1");
}
