stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

/*

#define mod 7

typedef long long ll;



ll fact[1000006], inv[1000006];



ll fp(ll base, ll exp)

{

    if (exp == 0)

        return 1;

    ll ans = fp(base, exp / 2);

    ans = (ans * ans) % mod;

    if (exp % 2 != 0)

        ans = (ans * (base % mod)) % mod;

    return ans;

}



void calcFacAndInv(ll n)

{

    fact[0] = inv[0] = 1;

    for (ll i = 1; i <= n; i++)

    {

        fact[i] = (i * fact[i - 1]) % mod;

        inv[i] = fp(fact[i], mod - 2);

    }

}



ll ncr(ll n, ll r)

{

    return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod;

}



ll npr(ll n, ll r)

{

    return (fact[n] * inv[n - r]) % mod;

}



int main()

{

    calcFacAndInv(100000);

     cout<<inv[3];

    return 0;

}

*/





// GCD



int gcd(long long a,long long b)

{

    if (b==0)

        return a;

    return gcd (b,a%b);

}

//prime factoriation

/*

set<int >s;

bool prime factorization (int x)

{

    for (int j=2; j*j<=x; ++j)

    {

        while (x%j==0)

        {

            s.insert(j);

            x/=j;

        }

    }

    if (x>1)

    {

        s.insert(x);

    }

    if (s.size()==2)

        return true ;

    else

        return false ;

}



#define ll long long

int a,b,q,p,n;

int countDivisorss(int x,int y)

{

    ll cnt = 0;

    for (int i = min(x,y); i <= sqrt(n); i++)

    {

        if (x % i == 0&&y%i==0)

        {

            if (x / i == i||y / i == i)

                cnt++;



            else

                cnt = cnt + 2;

        }

    }

    return cnt;

}

int countDivisors(int x)

{

    ll cnt = 0;

    int tmp=n;

    if (n-x>10000000)  tmp=sqrt(n);

    for (int i = x; i <= tmp; i++)

    {

        if (x % i == 0)

        {

         if (x/i==i)

            cnt++;

         else

            cnt = cnt + 2;

        }

    }

    return cnt;

}

*/



/*

        int s =n/2,e=n-1;

        while (s<=e&&e<=n-1)

        {

            int mid =s+e/2;



            if (arr[mid]==arr[n/2])

            {

                s=mid;

                num +=mid -s;

            }

            else

                e=mid-1;

        }

        */









const int N = 1e5 + 5;



vector<int> adj[N];

bool visited[N];

int nodesNum;

void dfs(int node)

{

   if (visited[node])

       return;

   visited[node] = true;

   for (int child: adj[node])

       dfs(child);

}



//fast power

/*

#define mod 1000000007

long long fp(long long  base, long long exp)

{

    if (exp == 0)

        return 1;

    long long  ans = fp(base, exp / 2);

    ans = (ans * ans);

    if (exp % 2 != 0)

        ans = (ans * (base )) ;

    return ans;

}

*/



int main()

{

   long long n,m;

   cin>>n>>m;

   long long  a=(n*(n-1))/(2);

   long long  b=(m*(m-1))/(2);

   long long      ans =(a+b);

    cout<<ans;

   return 0;

}






// Faster C++ solution for the same problem (measured speedup: 18.540466913717246x):
#include<stdio.h>

int main (){

	int a,b,c,d;

	scanf("%d %d",&a,&b);

	c = a * (a-1)/2;

	d = b * (b-1)/2;

	

	printf("%d\n",c+d);

	



	return 0;

}
// Slower C++ solution:
#pragma GCC optimize ("O3")

#pragma GCC optimize ("unroll-loops")

#pragma GCC optimize("-Ofast")

#include <bits/stdc++.h>

using namespace std;



typedef int in;

#define int long long

#define f first

#define s second

#define pb push_back

#define pp push

#define RETURN exit(0)

#define pow(x,y) (int)(pow(x,y))

#define ceil(x) (int)(ceil(x))

#ifndef ONLINE_JUDGE

#define debug(x) cout<<x<<endl;

#else

#define debug(x) 42

#endif

const int MAX=1e4;

const int MOD=1e9+7;

const int MAAX=1e15;

const long double EPS=1e-6;

const int N=1e6;

int power(int x,int y)

{

    if (y == 0)

        return 1;

    int a=power(x,y/2);

    if (y % 2 == 0)

        return a * a % MOD;

    else

        return x * a * a % MOD;

}

long double sq(int x){

    long double s=0,e=x,mid;

    while(e-s>=EPS){

        mid=(s+e)/2;

        if(mid*mid<=x){

            s=mid;

        }

        else{

            e=mid-EPS;

        }

    }

    return s;

}

bool check(int x){



}

int binary(int x,int y){

    int s=x,e=y,mid;

    while(s<e){

        mid=ceil((s+e)/2.0);

        if(check(mid))

            s=mid;

        else

            e=mid-1;

    }

    return s;

}

vector<int> primes;

bool visted[N+5];

void sieve(){

    visted[0]=visted[1]=1;

    for(int i=2;i*i<=N;i++){

        if(visted[i]==0){

            primes.pb(i);

            for(int j=i*i;j<=N;j+=i){

                visted[j]=1;

            }

        }

    }

    for(int i=sqrt(N)+1;i<=N;i++){

        if(visted[i]==0)

            primes.pb(i);

    }

    return;

}







in main()

{

    //freopen("in.in","r",stdin);

    //freopen("out.out","w",stdout);

    int tc=1;

    //sieve();

    //scanf("%lld",&tc);

    while(tc--)

    {

        int n,m;

        cin>>n>>m;

        cout<<n*(n-1)/2+m*(m-1)/2<<endl;

    }

    RETURN;

}


// Faster C++ solution for the same problem (measured speedup: 5.256033464551087x):
#include<stdio.h>

int main (){

	int a,b,c,d;

	scanf("%d %d",&a,&b);

	c = a * (a-1)/2;

	d = b * (b-1)/2;

	

	printf("%d\n",c+d);

	



	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

#define PER(i, a, b) for (int i = a; i >= b; --i)

#define RVC(i, S) for (int i = 0; i < S.size(); ++i)

#define mp make_pair

#define pb push_back

#define debug(...) fprintf(stderr, __VA_ARGS__)

#define fi first

#define se second

using namespace std;

 

typedef long long LL;

typedef pair<int, int> pii;

typedef vector<int> VI;

 

inline int read(){

	int x = 0, ch = getchar(), f = 1;

	while (!isdigit(ch)){if (ch == '-') f = -1; ch = getchar();}

	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();

	return x * f;

}



LL n, s;

VI tmp;



void check(LL x){

	tmp.clear();

	LL t = n;

	while (t){

		tmp.pb(t % x);

		t /= x;

		// cerr << t << endl;

	}

	int sum = 0;

	RVC(i, tmp) sum += tmp[i];

	if (sum == s){

		cout << x << endl;

		exit(0);

	}

}



int main(){

	cin >> n >> s;

	LL t;

	if (n == s){

		cout << n + 1 << endl;

		return 0;

	}

	REP(i, 2, 1000000) check(i);

	LL ans = 1ll << 60;

	for (LL j, t = 1; t <= n; t = j + 1){

		LL p = n / t;

		j = n / p;

		// cerr << p << endl;

		

		if (n + p - s > 0 && (n + p - s) % p == 0){

			LL b = (n + p - s) / p;

			if (0 <= p && p < b && 0 <= n - p * b && n - p * b < b){

				ans = min(ans, b);

			}

		}

	}

	if (ans == 1ll << 60){

		cout << -1 << endl;

	} else cout << ans << endl;

	return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 5.175363961167852x):
#include<stdio.h>

#define inf 1000000000000ll

long long f ( long long b, long long n ) {

	if ( n < b ) return n;

	else return f ( b, n / b ) + n % b;

}

int main ( void ) {

	long long n, s;

	scanf ( "%lld%lld", &n, &s );

	if ( n < s ) {

		printf ( "-1" );

	} else if ( n == s ) {

		printf ( "%lld", n + 1 );

	} else {

		long long p = n - s, ans = inf;

		for ( int i = 1; 1ll * i * i <= p; i++ )

			if ( p % i == 0 ) {

				if ( f ( 1ll * i + 1, n ) == s && ans > i + 1 ) {

					ans = i + 1;

				}

				if ( f ( p / i + 1, n ) == s && ans > p / i + 1 ) {

					ans = p / i + 1;

				}

			}

		if ( ans != inf ) {

			printf ( "%lld", ans );

		} else {

			printf ( "-1" );

		}

	}

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define int ll

using namespace std;

typedef long long ll;

 

ll n,s,ans=10000000000000ll;

inline bool check(int x)

{

	if(x==1) return 0;

	ll tt=0,ts=n;

	while(ts)

	{

		tt+=ts%x;

		ts/=x;

	}

//	if(x==10) printf("%d\n",tt);

	return tt==s;

}

signed main()

{

	cin >> n >> s;

	if(n==s){printf("%lld\n",n+1); return 0;}

	for(int i=1;i<=sqrt(n);i++)

	{

		if((n-s+i)%i==0&&i*i<n){ if(((n-s+i)/i)) if(n/((n-s+i)/i)==i) ans=min(ans,(n-s+i)/i);}

		if(check(i)) ans=min(ans,(ll)i);	

	}

	if(ans==10000000000000ll) puts("-1");

	else cout << ans << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.207291493738497x):
#include<stdio.h>

#define inf 1000000000000ll

long long f ( long long b, long long n ) {

	if ( n < b ) return n;

	else return f ( b, n / b ) + n % b;

}

int main ( void ) {

	long long n, s;

	scanf ( "%lld%lld", &n, &s );

	if ( n < s ) {

		printf ( "-1" );

	} else if ( n == s ) {

		printf ( "%lld", n + 1 );

	} else {

		long long p = n - s, ans = inf;

		for ( int i = 1; 1ll * i * i <= p; i++ )

			if ( p % i == 0 ) {

				if ( f ( 1ll * i + 1, n ) == s && ans > i + 1 ) {

					ans = i + 1;

				}

				if ( f ( p / i + 1, n ) == s && ans > p / i + 1 ) {

					ans = p / i + 1;

				}

			}

		if ( ans != inf ) {

			printf ( "%lld", ans );

		} else {

			printf ( "-1" );

		}

	}

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

using namespace std;



int main(){

  long long int n, s;

  cin >> n >> s;

  if(s > n){

    cout << -1 << endl;

    return 0;

  }else if(s == n){

    cout << n+1 << endl;

    return 0;

  }

  long long int d = n - s, ans = -1, m = sqrt(d);

  for(long long int i = 1; i <= m; ++i){

    if(d%i) continue;

    long long int b = i + 1, n_ = n, t = n;

    while(n_ > 0){

      n_ /= b;

      t -= n_*(b-1);

      if(t < s) break;

    }

    if(t == s){

      cout << b << endl;

      return 0;

    }

    b = d/i + 1;

    n_ = n;

    t = n;

    while(n_ > 0){

      n_ /= b;

      t -= n_*(b-1);

      if(t < s) break;

    }

    if(t == s){

      ans = b;

    }

  }

  cout << ans << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.829878158314503x):
#include <cmath>

#include <cstdio>

using namespace std;



int main(){

  long long int n, s;

  scanf("%lld %lld", &n, &s);

  if(s > n){

    printf("%d\n",-1);

    return 0;

  }else if(s == n){

    printf("%lld\n",n+1);

    return 0;

  }

  long long int d = n - s, ans = -1, m = sqrt(d);

  for(long long int i = 1; i <= m; ++i){

    if(d != d/i*i) continue;

    long long int b = i + 1, n_ = n, t = n;

    while(n_ > 0){

      n_ /= b;

      t -= n_*(b-1);

      if(t < s) break;

    }

    if(t == s){

      printf("%lld\n",b);

      return 0;

    }

    b = d/i + 1;

    n_ = n;

    t = n;

    while(n_ > 0){

      n_ /= b;

      t -= n_*(b-1);

      if(t < s) break;

    }

    if(t == s){

      ans = b;

    }

  }

  printf("%lld\n",ans);

  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <vector>

#include <cmath>



using namespace std;



typedef long long i64;



i64 f(i64 b, i64 n) {

    i64 s=0;

    while (n) {

        s += n % b;

        n /= b;

    }

    return s;

}



int main(void) {

    i64 n, s;

    cin >> n >> s;



    if (n == s) {

        cout << n+1 << endl;

        return 0;

    }



    i64 ub=sqrt(n);

    i64 b;

    for (b=2; b<=ub; b++)

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }



    i64 p;

    for (p=ub+1; --p;) {

        b = (n-s)/p + 1;

        if (b < 2) continue;

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }

    }

    cout << "-1\n";

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.835990577079463x):
#include <cstdio>

#include <cstdint>

#include <cmath>



intmax_t f(intmax_t b, intmax_t n) {

  if (n < b)

    return n;



  return f(b, n/b) + n%b;

}



int main() {

  intmax_t n, s;

  scanf("%jd %jd", &n, &s);



  if (s == n)

    return !printf("%jd\n", n+1);



  intmax_t sqrtn=sqrt(n);

  for (intmax_t b=2; b<=sqrtn; ++b)

    if (f(b, n) == s)

      return !printf("%jd\n", b);



  for (intmax_t p=sqrtn; p>0; --p) {

    intmax_t b=(n-s)/p+1;

    if (b < 2) continue;

    if (f(b, n) == s)

      return !printf("%jd\n", b);

  }



  printf("-1\n");

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <vector>

#include <cmath>



using namespace std;



typedef long long i64;



i64 f(i64 b, i64 n) {

    i64 s=0;

    while (n) {

        s += n % b;

        n /= b;

    }

    return s;

}



int main(void) {

    i64 n, s;

    cin >> n >> s;



    if (n == s) {

        cout << n+1 << endl;

        return 0;

    }



    i64 ub=sqrt(n);

    i64 b;

    for (b=2; b<=ub; b++)

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }



    i64 p;

    for (p=ub+1; --p;) {

        b = (n-s)/p + 1;

        if (b < 2) continue;

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }

    }

    cout << "-1\n";

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.833407262754178x):
#include <cstdio>

#include <cstdint>

#include <cmath>



intmax_t f(intmax_t b, intmax_t n) {

  intmax_t s=0;

  while (n) {

    s += n % b;

    n /= b;

  }



  return s;

}



int main() {

  intmax_t n, s;

  scanf("%jd %jd", &n, &s);



  if (s == n)

    return !printf("%jd\n", n+1);



  intmax_t sqrtn=sqrt(n);

  for (intmax_t b=2; b<=sqrtn; ++b)

    if (f(b, n) == s)

      return !printf("%jd\n", b);



  for (intmax_t p=sqrtn; p>0; --p) {

    intmax_t b=(n-s)/p+1;

    if (b < 2) continue;

    if (f(b, n) == s)

      return !printf("%jd\n", b);

  }



  printf("-1\n");

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define MOD 1000000007

#define INF 1e18

#define pb push_back

#define int long long

#define fi first

#define se second

#define bw(i,r,l) for (int i=r-1;i>=l;i--)

#define fw(i,l,r) for (int i=l;i<r;i++)

#define fa(i,x) for (auto i:x)

using namespace std;

int n,s,tmp;

int get(int n,int b) {return n<b?n:get(n/b,b)+n%b;}

bool check(int i) {

	int ans=get(n,i);

	if (ans==s) return true;

	return false;

}

signed main() {

	//freopen("aome.inp","r",stdin);

	//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin>>n>>s;

	if (n==s) {cout<<n+1; return 0;}

	tmp=n-s;

	int ans=INF;

	fw (i,1,sqrt(tmp)+1) if (tmp%i==0) {

		if (check(i+1)) {ans=min(ans,i+1);}

		if (check(tmp/i+1)) {ans=min(ans,tmp/i+1);}

	}

	if (ans==INF) cout<<"-1";

	else cout<<ans;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.163489230139761x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

#define INF 100000000000000000LL

using namespace std;

typedef long long LL;

LL f(LL b,LL n)

{

	if(n<b)

		return n;

	return f(b,n/b)+n%b;

}

bool Check(LL b,LL n,LL s)

{

	if(f(b,n)==s)

		return true;

	return false;

}

int main()

{

	LL n,s;

	scanf("%lld %lld",&n,&s);

	if(s>n)

	{

		printf("-1\n");

		return 0;

	}

	if(s==n)

	{

		printf("%lld\n",n+1LL);

		return 0;

	}

	LL n1=n-s;

	LL ans=INF;

	for(LL i=1LL;i*i<=n1;i++)

	{

		if(n1%i==0)//i=b-1

		{

			if(Check(i+1,n,s)==true)

				ans=min(ans,i+1LL);

			if(n1/i!=i&&Check(n1/i+1LL,n,s)==true)

				ans=min(ans,n1/i+1LL);

		}

	}

	if(ans==INF)

		printf("-1\n");

	else

		printf("%lld\n",ans);

	return 0;

}

/*

100000000000 3

*/
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



long long f(long long b,long long n)

{

    if(n==0) return 0;

    else if(n<b) return n;

    return f(b,n/b) + (n%b);

}

int main()

{

    long long m,s;

    cin >> m >> s;

    long long k=m-s;

    long long ans=1e12+7;

    if(k<0)

    {

        cout << "-1" << "\n";

        return 0;

    }

    else if(k==0)

    {

        cout << m+1 << "\n";

        return 0;

    }

    else

    {

        long long t=sqrt(k);

        for(long long i=1;i<=t;i++)

        {

            if(k%i==0)

            {

                if(f(i+1,m) == s) ans=min(ans,i+1);

                if(f((k/i)+1,m) == s) ans=min(ans,(k/i)+1);

            }

        }

    }

    if(ans==1e12+7)

        cout << "-1" << "\n";

    else

        cout << ans << "\n";

}


// Faster C++ solution for the same problem (measured speedup: 5.167782479221683x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

#define INF 100000000000000000LL

using namespace std;

typedef long long LL;

LL f(LL b,LL n)

{

	if(n<b)

		return n;

	return f(b,n/b)+n%b;

}

bool Check(LL b,LL n,LL s)

{

	if(f(b,n)==s)

		return true;

	return false;

}

int main()

{

	LL n,s;

	scanf("%lld %lld",&n,&s);

	if(s>n)

	{

		printf("-1\n");

		return 0;

	}

	if(s==n)

	{

		printf("%lld\n",n+1LL);

		return 0;

	}

	LL n1=n-s;

	LL ans=INF;

	for(LL i=1LL;i*i<=n1;i++)

	{

		if(n1%i==0)//i=b-1

		{

			if(Check(i+1,n,s)==true)

				ans=min(ans,i+1LL);

			if(n1/i!=i&&Check(n1/i+1LL,n,s)==true)

				ans=min(ans,n1/i+1LL);

		}

	}

	if(ans==INF)

		printf("-1\n");

	else

		printf("%lld\n",ans);

	return 0;

}

/*

100000000000 3

*/
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<cstring>

#include<cmath>

#define RG register

#define LL long long

using namespace std;

LL n,s,anss=3e18;

template<typename I> inline void read(I &ot){

	I ch=getchar(), x=0, f=1;

	while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();	}

	while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();	}

	ot=x*f;}

template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}

template<typename I>inline I mi(const I&a,const I&b){return a<b ? a : b;}

template<typename I>inline I f(I bb,I nn)

{

	I ans=0;

	for(;nn;nn/=bb) ans+=nn%bb;

	return ans;

}

int main()

{

	//freopen("Digit Sum.in","r",stdin);

	//freopen(".out","w",stdout);

	read(n,s);

	if(n==s)

	{

		printf("%lld\n",n+1);

		return 0;

	}

	if(n<s) 

	{

		printf("-1\n");

		return 0;

	}

	for(LL i=2;i<=sqrt(n);i++)

	{

		if(f(i,n)==s)

		{

			printf("%lld\n",i);

			return 0;

		}

	}

	for(LL i=1;i*i<=n-s;i++)

	{

		if((n-s)%i==0)

		{

			LL b=(n-s)/i+1;

			if(f(b,n)==s) anss=mi(anss,b);

		}

	}

	printf("%lld\n",anss==3e18 ? -1 : anss);

	//fclose(stdin); fclose(stdout);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.584154746423927x):
#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

using namespace std;

typedef long long LL;

LL n,s;



inline bool f(LL b,LL n) {

    LL res=0;

    while (n>=b) {

        res+=n%b;

        n/=b;

    }

    if (b>n) res+=n;

    return res==s;

}

int main() {

    while (~scanf("%lld%lld",&n,&s)) {

        if (s==n) {

            printf("%lld\n", n+1);

            continue;

        }

        if (s>n) {

            puts("-1");

            continue;

        }

        LL t=sqrt(n);

        bool flag=false;

        // b<=sqrt(n)

        for (LL i=2;i<=t;i++)

            if (f(i,n)) {

                printf("%lld\n", i);

                flag=true;

                break;

            }

        if (flag) continue;



        // b>sqrt(n) b进制下只有两位

        // n=pb+q  s=p+q

        // n-s=p(b-1) => b=(n-s)/p+1

        LL ans=1e12;

        for (LL p=1;p*p<=n-s;p++) {

            if ((n-s)%p==0) {

                LL b=(n-s)/p+1;

                if (f(b,n)) {

                    flag=true;

                    ans=min(ans,b);

                }

            }

        }

        if (!flag) puts("-1");

        else printf("%lld\n", ans);

    }

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



long long n, s, cann;



bool kiemtra(long long x){

    if (x<2) return false;

    long long sum = 0;

    long long tmp = n;

    while (tmp != 0){

        sum += tmp%x;

        tmp/=x;

    }

    if (sum == s) return true;

    return false;

}



int main()

{

    cin >> n >> s;

    if (n<s) {

        cout << -1;

        return 0;

    }

    else{

    for (long long i=1; i*i<=n; i++)

        if ((n-s)%i == 0){

            if (kiemtra(i+1)){

                cout << i+1;

                return 0;

            }

        }

    for (long long i = sqrt(n-s); i>0; i--)

        if ((n-s)%i==0)

            if (kiemtra((n-s)/i+1)){

                cout << (n-s)/i+1;

                return 0;

            }

    if (kiemtra(n-s+1)) cout << n-s+1;

    else if (n == s) cout << n+1;

    else if (s == 1) cout << n;

    else cout << -1;

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.735056300552721x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;

typedef long long ll;

const ll INF=9223372036854775807ll;

ll n,s,ans=INF;

ll GetS(ll x)

{

	ll f=n,ret=0;

	while(f)

		ret+=f%x,f/=x;

	return ret;

}

int main()

{

	scanf("%lld%lld",&n,&s);

	if(n==s) {printf("%lld\n",n+1ll);return 0;}

	ll T=n-s;

	for(ll i=1;i<=sqrt(T);i++)

		if(T%i==0)

		{

			ll x=i+1ll,y=T/i+1ll;

			if(GetS(x)==s) ans=min(ans,x);

			if(GetS(y)==s) ans=min(ans,y);

		}

	for(ll i=2;i<=sqrt(n);i++)

		if(GetS(i)==s)

		{

			ans=min(ans,i);

			break;

		}

	printf("%lld\n",ans==INF? -1ll:ans);

	return 0;

}
// Slower C++ solution:
#include<map>

#include<vector>

#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

using namespace std;

typedef long long LL;

typedef pair<int, int> P;

typedef pair<int, P> E;

const LL MOD = 1000000007LL;

LL f(LL b, LL n)

{

	LL ret = 0;

	while (n > 0)

	{

		ret += n%b;

		n /= b;

	}

	return ret;

}

int main()

{

	LL n, s;

	cin >> n >> s;

	if (n == s)

	{

		cout << n + 1 << endl;

		return 0;

	}

	for (LL i = 2; i*i <= n; i++)

	{

		if (f(i, n) == s)

		{

			cout << i << endl;

			return 0;

		}

	}

	if (n < s)

	{

		cout << -1 << endl;

		return 0;

	}

	LL ans = 1LL << 60;

	for (LL i = 1; i*i <= n - s; i++)

	{

		if ((n - s) % i) continue;

		LL b = (n - s) / i + 1;

		if (f(b, n) == s) ans = min(ans, b);

	}

	if (ans == 1LL << 60)

	{

		cout << -1 << endl;

		return 0;

	}

	cout << ans << endl;

}

// Faster C++ solution for the same problem (measured speedup: 4.7442143468538704x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;

typedef long long ll;

const ll INF=9223372036854775807ll;

ll n,s,ans=INF;

ll GetS(ll x)

{

	ll f=n,ret=0;

	while(f)

		ret+=f%x,f/=x;

	return ret;

}

int main()

{

	scanf("%lld%lld",&n,&s);

	if(n==s) {printf("%lld\n",n+1ll);return 0;}

	ll T=n-s;

	for(ll i=1;i<=sqrt(T);i++)

		if(T%i==0)

		{

			ll x=i+1ll,y=T/i+1ll;

			if(GetS(x)==s) ans=min(ans,x);

			if(GetS(y)==s) ans=min(ans,y);

		}

	for(ll i=2;i<=sqrt(n);i++)

		if(GetS(i)==s)

		{

			ans=min(ans,i);

			break;

		}

	printf("%lld\n",ans==INF? -1ll:ans);

	return 0;

}