/*
 * src.cpp
 *
 *  Created on: 2016/09/03
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

typedef long long ll;

ll N,S;

ll f(ll i,ll x){
	ll res=0;
	while (x){
		res+=x%i;
		x/=i;
	}
	return res;
}

void solve(){
	ll i;
	if (N<S){
		printf("-1\n");
	}else if (N==S){
		printf("%lld\n",N+1);
	}else{
		for (i=2;i*i<=N;i++){
			if (f(i,N)==S){
				printf("%lld\n",i);
				return;
			}
		}
		while (i*i>=N){
			i--;
		}
		for (;i>0;i--){
			ll p=i;
			ll b=(N-S)/p+1;
			if (N/b+N%b==S){
				printf("%lld\n",b);
				return;
			}
		}
		printf("-1\n");
	}
}

int main(){
	scanf("%lld %lld",&N,&S);
	solve();
	return 0;
}


