#include <bits/stdc++.h>

using namespace std;

#define countof(a) (sizeof(a)/sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int> >
#define vpi vector<pi >
#define pi pair<int,int>
#define fi first
#define se second
#define all(n) n.begin(), n.end()

#define FOR(var, to)          for (register int var = 0; var < (int)to; var++)
#define DOWN(var, from)       for (register int var = from-1; var >= 0; var--)
#define FROMTO(var, from, to) for (register int var = from; var <= (int)to; var++)
#define DOWNTO(var, from, to) for (register int var = from; var >= (int)to; var--)

#define INIT(var,n) FOR(i,countof(var)) var[i] = (n)
#define _3(a,b,c,...) c
#define _4(a,b,c,d,...) d

#define _INPUT1(v1) FOR(_,countof(v1)) cin >> v1[_]
#define _INPUT2(v1,v2) FOR(_,countof(v1)) cin >> v1[_] >> v2[_]
#define _INPUT3(v1,v2,v3) FOR(_,countof(v1)) cin >> v1[_] >> v2[_] >> v3[_]
#define INPUT(...) _4(__VA_ARGS__,_INPUT3,_INPUT2,_INPUT1)(__VA_ARGS__)

#define _INPUT11(v1) FOR(i,countof(v1)) cin >> v1[i], v1[i]--
#define _INPUT21(v1,v2) FOR(i,countof(v1)) cin >> v1[i] >> v2[i], v1[i]--, v2[i]--
#define INPUT1(...) _3(__VA_ARGS__,_INPUT11,_INPUT21)(__VA_ARGS__)

#define INPUT2(a,b,c) FOR(i,countof(a)) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--
#define GRAPH(a,b,c,v) vector<mi > v(countof(a));FOR(i,countof(a)) v[a[i]].push_back(pi(b[i],c[i])), \
                                                                   v[b[i]].push_back(pi(a[i],c[i]))

#define __OUT(var, i) printf(#var "[%d]:%d ", i, var[i])
#define _OUT1(v1) FOR(i,countof(v1)) __OUT(v1,i), cout << endl
#define _OUT2(v1, v2) FOR(i,countof(v1)) __OUT(v1,i), __OUT(v2,i), cout << endl
#define _OUT3(v1, v2, v3) FOR(i,countof(v1)) __OUT(v1,i), __OUT(v2,i), __OUT(v3,i), cout << endl
#define OOO(...) _4(__VA_ARGS__,_OUT3,_OUT2,_OUT1)(__VA_ARGS__) 

#define SORT(v) qsort(v,countof(v),sizeof(*v),int_less)
#define SORTT(v) qsort(v,countof(v),sizeof(*v),int_greater)
#define QSORT(v,b) qsort(v,countof(v),sizeof(*v),b)

#define MOD 1000000007
#define INF ((1 << 30)-1)
#define LINF ((1LL << 62)-1)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

template<int mod>
struct ModInt{
    int x;
    ModInt():x(0){}
    ModInt(long long y):x(y>=0?y%mod:(mod-(-y)%mod)%mod){}
    ModInt &operator+=(const ModInt &p){
        if((x+=p.x)>=mod)x-=mod;
        return *this;
    }
    ModInt &operator-=(const ModInt &p){
        if((x+=mod-p.x)>=mod)x-=mod;
        return *this;
    }
    ModInt &operator*=(const ModInt &p){
        x=(int)(1LL*x*p.x%mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p){
        *this*=p.inverse();
        return *this;
    }
    ModInt operator-()const{return ModInt(-x);}
    ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;}
    ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;}
    ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;}
    ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;}
    bool operator==(const ModInt &p)const{return x==p.x;}
    bool operator!=(const ModInt &p)const{return x!=p.x;}
    ModInt inverse()const{
        int a=x,b=mod,u=1,v=0,t;
        while(b>0){
            t=a/b;
            a-=t*b;
            swap(a,b);
            u-=t*v;
            swap(u,v);
        }
        return ModInt(u);
    }
    friend ostream &operator<<(ostream &os,const ModInt<mod> &p){
        return os<<p.x;
    }
    friend istream &operator>>(istream &is,ModInt<mod> &a){
        long long x;
        is>>x;
        a=ModInt<mod>(x);
        return (is);
    }
};
typedef ModInt<MOD> mint;

struct UnionFind{
    vi data;
    UnionFind(int size):data(size,-1){}
    bool unite(int x,int y) {
        x=root(x);y=root(y);
        if(x!=y){
            if(data[y]<data[x])swap(x,y);
            data[x]+=data[y];data[y]=x;
        }
        return x!=y;
    }
    bool find(int x,int y) {
        return root(x)==root(y);
    }
    int root(int x) {
        return data[x]<0?x:data[x]=root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
    bool united() {
        int comroot = -1;
        FOR(i,data.size()) {
            if (comroot != -1 && root(i) != comroot) return false;
            comroot = root(i);
        }
        return true;
    }
};

static inline int ri() {
  int a;
  scanf("%d", &a);
  return a;
}

static inline s64 rs64() {
  s64 a;
  scanf("%" SCNd64, &a);
  return a;
}

static inline void wi(int a) {
  printf("%d", a);
}

static inline void wu64(u64 a) {
  printf("%" PRIu64, a);
}

static inline void ws64(s64 a) {
  printf("%" PRId64, a);
}

int int_less(const void *a, const void *b) {
  return (*((const int*)a) - *((const int*)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int*)b) - *((const int*)a));
}

int main() {
    s64 n,s;
    cin>>n>>s;
    s64 root;
    for (root=0;root*root<=n;root++);
    s64 res = -1;
    FROMTO(i,2,root) {
        s64 m = n;
        int tmp = 0;
        while(m) tmp += m%i, m /= i;
        if (tmp == s) {
            res = i;
            break;
        }
    }
    if (res != -1) {
        cout << res << endl;
        return 0;
    }
    for (s64 i = 1; i < n; i++) {
        s64 r1 = s-i;     // -
        if (r1<0) break;
        s64 t0 = n-r1;   // +
        s64 x = t0/i;    // -
        if (i >= x) break;
        if (r1 >= x) continue;
        if (t0%i) continue;
        res = x;
    }
    if (res == -1 && n == s) cout << n+1 << endl; 
    else cout << res << endl;
  return 0;
}


Measured memory_bytes: 256