#include <bits/stdc++.h>
using namespace std;
long long lowerBound,actualAge;
long long powah(long long a,long long b){
long long ans = 1;
for(int i=1;i<=b;i++)
ans = ans * a;
return ans;
}
long long convert(long long base){
if(actualAge < base)
return actualAge;
vector<int> d;
long long cpy = actualAge, ans=0;
while(cpy > 0){
long long t = cpy % base;
if(t > 9)
return -1;
d.push_back(t);
cpy = cpy / base;
}
for(int i=d.size()-1;i>=0;i--){
ans = ans * 10 + d[i];
}
return ans;
}
long long conv(long long base, long long num){
long long ans = 0;
long long exp = 1;
while(num){
int last = num%10;
ans+=last*exp;
num/=10;
exp*=base;
}
return ans;
}
int main() {
cin>>actualAge>>lowerBound;
long long ans = -1;
long long ansAge = 5000000000000000000;
for(long long curAge = lowerBound; curAge<=9999LL; curAge++){
long long lowBase = 10;
long long num_digits;
if(curAge > 999)
num_digits = 3;
else if(curAge > 99)
num_digits = 2;
else if(curAge > 9)
num_digits = 1;
else
num_digits = 0;
long long high_base_order = 1;
if(num_digits!=0)
high_base_order = 18/num_digits;
long long highBase = powah(10,high_base_order);
while(lowBase<=highBase){
long long midBase = (lowBase+highBase)/2;
long long num = conv(midBase, curAge);
if(num<0) highBase = midBase-1;
if(num>actualAge) highBase = midBase-1;
if(num<actualAge) lowBase = midBase+1;
if(num==actualAge){
if(ansAge>curAge){
ansAge=curAge;
ans = midBase;
}
break;
}
}
}
for(long long base = 10; base<=100000; base++){
long long num = convert(base);
if(num>=lowerBound){
if(ansAge>num){
ansAge=num;
ans = base;
}
}
}
cout<<ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmxvbmcgbG9uZyBsb3dlckJvdW5kLGFjdHVhbEFnZTsKbG9uZyBsb25nIHBvd2FoKGxvbmcgbG9uZyBhLGxvbmcgbG9uZyBiKXsKICAgIGxvbmcgbG9uZyBhbnMgPSAxOwogICAgZm9yKGludCBpPTE7aTw9YjtpKyspCiAgICAgICAgYW5zID0gYW5z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MTAwMDAwOyBiYXNlKyspewogICAgICAgIGxvbmcgbG9uZyBudW0gPSBjb252ZXJ0KGJhc2UpOwogICAgICAgIGlmKG51bT49bG93ZXJCb3VuZCl7CiAgICAgICAgICAgIGlmKGFuc0FnZT5udW0pewogICAgICAgICAgICAgICAgYW5zQWdlPW51bTsKICAgICAgICAgICAgICAgIGFucyA9IGJhc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0PDxhbnM7CiAgICByZXR1cm4gMDsKfQ==