Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a and b are non-negative integer numbers. For example, if x = 2 and y = 3 then the years 4 and 17 are unlucky (4 = 20 + 31, 17 = 23 + 32 = 24 + 30) and year 18 isn't unlucky as there is no such representation for it.Such interval of years that there are no unlucky years in it is called The Golden Age.You should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l, r] are unlucky then the answer is 0.

The first line contains four integer numbers x, y, l and r (2 ≤ x, y ≤ 1018, 1 ≤ l ≤ r ≤ 1018).

Print the maximum length of The Golden Age within the interval [l, r]. If all years in the interval [l, r] are unlucky then print 0.

NoteIn the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1, 1], [6, 6] and [8, 8].In the second example the longest Golden Age is the interval [15, 22].

#include <iostream>
#include <cmath>
#include <vector>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <cstdio>
#include <stack>

using namespace std;
#define LL long long int

const LL MOD = 1e09 + 7;
const LL MAXN = 1e05 + 7;
LL v[1000007];

int main()
{
	LL a, b, lef, rig;
	cin >> a >> b >> lef >> rig;
	int point = 0;
	LL z = 1;
	LL zp = 1;
	LL prezp = zp;
	LL prez = z;
	bool judge = true;
	while (zp >= prezp && z >= prez && z <= rig)
	{
		prezp = zp;
		prez = z;
		if (zp > 0x7fffffffffffffff / a)
			judge = false;
		zp = zp * a;
		LL zb = 1;
		z = z + zb;
		LL prezb = zb;
		while (zb >= prezb && z > 0 && z <= rig)
		{
			prezb = zb;
			v[point++] = z;
			z = z - zb;
			if (zb > 0x7fffffffffffffff / b)
				break;
			zb = zb * b;
			z = z + zb;

		}
		if (!judge)
			break;
		z = zp;
	}
	sort(v, v + point);
	int i = 0;
	while (v[i] < lef && i < point) i++;
	if (i != point)
	{
		LL mx = max((LL)0, v[i] - lef);
		i++;
		for (; i < point; i++)
			mx = max(mx, v[i] - v[i - 1] - 1);
		mx = max(mx, rig - v[point - 1]);
		cout << mx << endl;
	}
	else
		cout << rig - lef + 1 << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{

	set< long long> st;
	long long x, y, l, r, num=1;
	vector<long long> ax, ye;
	cin >> x >> y >> l >> r;
	ax.push_back(1);
	ye.push_back(1);
	while(num <= (1000000000000000000 / x)){
		 num = num * x;
		 ax.push_back(num);
		 //cout << num << endl;
		 //if(num == 0) break;
	}
	num = 1;
	while(num <= (1000000000000000000 / y)){
		 num = num*y;
		 ye.push_back(num);
		 //cout << num << endl;
		 //if(num == 0) break;
	}
	for(int i=0; i<ax.size(); i++){
		for(int j=0; j<ye.size(); j++){
			long long tmp = ax[i] + ye[j];
			if(tmp >= l && tmp <= r){
				st.insert(tmp);
				//cout << tmp << endl;
			}
		}
	}
	st.insert(l-1);
	st.insert(r+1);
	long long ans = 0;
	vector<long long> v;
	set<long long>::iterator it;
	for(it = st.begin(); it != st.end(); it++){
		v.push_back(*it);
	}
	for(int i=1; i<v.size(); i++){
		ans = max(ans, v[i] - v[i-1] - 1 );
	}
	cout << ans;

	return 0;
}

#include<stdio.h>
#include <algorithm>
using namespace std ;
long long a[10005],b[10005],c[200005];
bool cmp(long long a,long long b)
{
    return a<b;
}
int main()
{
    long long x,y,l,r;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    long long d,h=1;
    d=r;
    a[0]=1;
    for(long long i=0;; i++)
    {
        long long temp=d/x;
        if(temp>0)
        {
            a[h]=a[h-1]*x;
            h++;
            d=d/x;
        }
        else
            break;
    }
    b[0]=1;
    long long k=1;
    d=r;
    for(long long i=0;; i++)
    {
        long long temp=d/y;
        if(temp>0)
        {
            b[k]=b[k-1]*y;
            k++;
            d=d/y;
        }
        else
            break;
    }
    //printf("h==%d\n",h);
    //printf("k==%d\n",k);
    long long g=0;
    for(long long i=0; i<h; i++)
    {
        for(long long j=0; j<k; j++)
        {
            if(a[i]+b[j]>=l&&a[i]+b[j]<=r)
            {
                c[g++]=a[i]+b[j];
            }
        }
    }
    sort(c,c+g,cmp);
    //printf("g==%d\n",g);
    long long ans=0;
    if(g>0)
    {
        if(c[0]-l>ans)
        {
            ans=c[0]-l;
        }
        if(r-c[g-1]>ans)
        {
            ans=r-c[g-1];
        }
        for(long long j=1; j<g; j++)
            ans=max(ans,c[j]-c[j-1]-1);
    }
    else
    {
        ans=r-l+1;
    }
    printf("%I64d\n",ans);
}
/*2018-07-25 00:07:48.37*/

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define F first
#define S second

const int MAXN = 2e6 + 10;
const ll INF = (ll)1e18 + 10;

ll x, y, l, r;
ll vec[MAXN], sec[MAXN], s1, s2, gec[MAXN], s3;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> x >> y >> l >> r;
	vec[s1++] = 1;
	while (vec[s1-1] <= INF/x) vec[s1] = vec[s1-1]*x, s1++;
	sec[s2++] = 1;
	while (sec[s2-1] <= INF/y) sec[s2] = sec[s2-1]*y, s2++;

	for (int i = 0; i < s1; i++)
		for (int j = 0; j < s2; j++)
			if (l <= vec[i]+sec[j] && vec[i]+sec[j] <= r)
				gec[s3++] = vec[i]+sec[j];

	ll mx = 0;
	gec[s3++] = l-1, gec[s3++] = r+1;
	sort(gec, gec+s3);
	for (int i = 0; i + 1 < s3; i++)
		mx = max(mx, gec[i+1] - gec[i] - 1);
	cout << mx << "\n";
	return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <vector>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
#include <ctime>
#include <map>
#include <time.h>
#include <bitset>
using namespace std;
#define INF INT_MAX
#define LINF (long long int)1e18
#define EPS 1e-9
const double PI = acos(-1.0);
#define pb push_back
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())
#define foreach(v,c) for(typeof((c).begin()) v=(c).begin(); v!=(c).end(); ++v)
#define all(v) (v).begin(),(v).end()
#define sz(x) ((int)((x).size()))
#define cpy(b,a) memcpy(b,a,sizeof a)
#define rep(i,n) for (int i =0 ; i< sz(n) ; i++)
#define lp(i,n) for(int i=0;i<(int)n;i++)
#define lpu(i,j,n) for(int i= (j);i<n ;i++)
#define lpd(i,j,n) for (int i =(j);i>=(int)n;i--)
#define clr(v,value) memset(v,value, sizeof(v))
#define readin freopen ("input.txt","r",stdin)
#define writeout freopen("output.txt", "w", stdout)
#define readLine(s) getline(std::cin,s)
#define scanLine(str) fgets(str, sizeof(str), stdin) // remember \n is in the string
#define scl(l) scanf("%I64d",&l)
#define sci(l) scanf("%d",&l)
#define scf(f) scanf("%f",&f)
#define scs(l) scanf("%s",&l)
#define scd(f) scanf("%lf",&f)
#define scChar(c) scanf(" %c", &c)
#define scstr(s) scanf("%s", s.c_str())
#define prstr(s) printf("%s",s.c_str())
#define prl(l) printf("%I64d",l)
#define prf(f) printf("%lf" , f)
#define pri(l) printf("%d",l)
#define prcharAr(s) printf("%s",s)
#define prchar(s) printf("%c",s)
#define prLine printf("\n")
#define prSpace printf(" ")
#define prDouble(d) printf("%f", d)
#define prD8(d) printf("%.8f", d)
#define prD15(d) printf("%.15f", d)
#define INDEX(arr,ind) (lower_bound(all(arr),ind)-arr.begin())
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define modulo(S, N) ((S) & (N - 1))   // returns S % N, where N is a power of 2
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((ll)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))
#define vpi vector< pair<int, int> >

typedef long long ll;
const int MOD = 1e9 + 7;
int dx[] = { -1,+0,+1,+0,+1,+1,-1,-1,+0 };
int dy[] = { +0,-1,+0,+1,+1,-1,+1,-1,+0 };
int day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
inline ll power(ll a, ll b, ll m) { ll _r = 1; while (b) { if (b % 2 == 1)_r = (_r*a) % m; a = (a*a) % m; b >> 1; }return _r; }inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }string toString(ll num) { stringstream ss; ss << num; string str = ss.str(); return str; }ll toLong(string str) { ll res; istringstream ss(str); ss >> res; return res; }
void getTime() { clock_t start = clock(); clock_t stop = clock(); double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC; printf("Time elapsed in ms: %.2f", elapsed); }
const double MX = 4e18;
ll r ,l ;
ll mpow(ll x, int y)
{
	double   res = 1 , last ;
	lp(i, y)
	{
	    last =res ;
		res = res * x;
		if (res > r)
			return -1;
	}

	ll ee =res;
	return ee;
}
set<ll> nums;
int main()
{
	ll x, y;
	scl(x), scl(y), scl(l), scl(r);
	ll s1, s2 , news;
	double sum;
	FOR(i, 0, 65)
	{
		FOR(j, 0, 65)
		{
			s1 = mpow(x, i);
			s2 = mpow(y, j);
			news = s1 + s2;
			if (news > MX || s1 ==  -1 || s2  == -1  || news -s1 != s2) continue;

			if (news >= l && news <= r)
				nums.insert(news);
		}
	}
	if (nums.size() == 0)
	{
	    ll ss =r - l+1;
		prl(ss);
		return 0;
	}
	ll len = 0 ,cnt = 0 ,old ,last ;
	for (auto elem : nums)
	{
		if (cnt == 0) len = elem - l;
		else len = max(len, elem - old - 1);
		old = elem;
		cnt++;
		if (cnt == nums.size())
			last = elem;
	}
	len = max(len, r - last);
	prl(len);
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
int power(int a,int b,int mod,int ans=1){
	for(;b;b>>=1,a=1LL*a*a%mod) if(b&1) ans=1LL*ans*a%mod;
	return ans;
}
set <INT> S;
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	INT x,y,l,r;
	cin>>x>>y>>l>>r;
	INT inf=1e18;
	INT st=1;
	for(int i=1;i<=100;i++) {
		if(st>inf/x) {
			st=i;break;
		}
		st*=x;
	}
	INT ed=1;
	for(int i=1;i<=100;i++) {
		if(ed>inf/y) {
			ed=i;break;
		}
		ed*=y;
	}
	INT X=1;
	for(int i=0;i<st;i++){
		INT Y=1;
		for(int j=0;j<ed;j++){
			S.insert((X+Y));
			Y*=y;
		}
		X*=x;
	}
	INT ans=0;
	while(!S.empty() and *(--S.end())>r) S.erase(--S.end());
	while(l<=r){
		auto it=S.lower_bound(l);
		if(it==S.end()) {
			ans=max(ans,r-l+1);
			break;
		}
		ans=max(ans,*it-l);
		l=*it+1;
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mx 1e18

ll x,y,l,r;
vector<ll>v1;
vector<ll>v2;
vector<ll>rng;
map<ll,ll>mp;
ll ans, d, s, t, i, j;
int main(){
    cin >> x >> y >> l >> r;

    s = 1;
    v1.push_back(1);
    while( s <= (mx / x) ){
        s *= x;
        v1.push_back(s);
    }
    v2.push_back(1);
    s = 1;
    while( s <= (mx / y) ){
        s *= y;
        v2.push_back(s);
    }

    for(i=0; i<v1.size(); i++){
        for(j = 0; j<v2.size(); j++){
            t = (v1[i]+v2[j])*(1ll);
            if(t>=l && t<=r){
                    rng.push_back(t);

            }
        }
    }
    sort(rng.begin(),rng.end());
    ans = 0;
    d = l;
    for(ll i=0; i<rng.size(); i++){
        t =rng[i]-d;
        ans = max(ans,t);
        d = rng[i]+1;
    }
    ans = max (ans, (r+1)-d);
    cout << ans << endl;
return 0;
}

#define _CRT_SECURE_NO_WARNINGS

#include <stdint.h>
#include <iostream>
#include <set>

using namespace std;

typedef unsigned long long ull;

bool mul(ull x, ull y, ull &z) {
	if (x > UINT64_MAX / y) {
		return false;
	}
	z = x * y;
	return true;
}
bool add(ull x, ull y, ull &z) {
	if (x > UINT64_MAX - y) {
		return false;
	}
	z = x + y;
	return true;
}

int main() {
	ull x, y, l, r, ret, a = 1, b;
	set<ull> badYears;
	cin >> x >> y >> l >> r;

	while (a < r) {
		b = 1;
		while(true) {
			ull val;
			if (!add(a, b, val)) {
				break;
			}
			if (val > r) {
				break;
			} else if(val >= l) {
				badYears.insert(val);
			}
			if (!mul(b, y, b)) {
				break;
			}
		}
		if (!mul(a, x, a)) {
			break;
		}
	}
	badYears.insert(l - 1);
	badYears.insert(r + 1);
	ret = 0;
	ull prev;
	bool first = true;
	for (auto it = badYears.begin(); it != badYears.end(); ++it) {
		if (first) {
			prev = *it;
			first = false;
			continue;
		}
		auto val = *it - prev - 1;
		if (val > ret) {
			ret = val;
		}
		prev = *it;
	}
	cout << ret << endl;
#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}

// Vaidik Patel(India)
// DA-IICT
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int INF = (int)1e9;
ll INFINF = (ll)1e18 + 10;
const ld PI = 3.14159265358979323846;
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define minheap priority_queue<ll,vector<ll>,greater<ll> >
#define maxheap priority_queue<ll,vector<ll>,less<ll> >
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rFOR(i,a,b) for(ll i=a;i>=b;i--)
#define deb1(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;
#define deb4(x,y,z,w) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<"\t"<<#w<<" : "<<w<<endl;
#define deb5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define deb6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;

#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

#define checkbit(n,b) ( (n >> b) & 1)//

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define M  1000000007
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define TIME cerr << "Time Elapsed : "<<1.0*clock()/CLOCKS_PER_SEC<<"\n";

ll powermodm(ll x,ll n,ll mod)
{ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%mod;x=(x*x)%mod;n=n/2;}return result;}
ll GCD(ll , ll );
ll LCM(ll ,ll);
ll power(ll ,ll);
ll choose(ll,ll);
int ones(ll);
void extendedEuclid(ll, ll);
ll MMI(ll, ll);
void fastscan(int &);
bool checkithbit(ll,ll); // ith bit is zero or not
//////////////////////////////////////////////////////////////////////////////

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll l,r,x,y;
    cin>>x>>y>>l>>r;
     ll n=(ld)log(1e18)/(ld)log(x);
     ll m=(ld)log(1e18)/(ld)log(y);
    // cout<<n<<" "<<m<<endl;
     vector<ll>a;
     a.pb(l-1);
     for(ll i=0;i<=n;i++)
     {
         for(ll j=0;j<=m;j++)
         {

             ll sum=power(x,i)+power(y,j);
             if(sum>=l && sum<=r)
             {
                 a.pb(sum);
             }
         }
     }
     a.pb(r+1);
     sort(a.begin(),a.end());
     ll ans=0;

     for(int i=1;i<a.size();i++)
     {
         ans=max(ans,a[i]-a[i-1]-(ll)1);
     }

     cout<<ans<<endl;
    return 0;
}
//////////////////////////////////////////////////////////////////////////
bool checkithbit(ll a,ll i)
{
if(a & (1 << i))
{
return  true;
}
else
{
return false;
}
}
ll GCD(ll a , ll b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}
ll LCM(ll a,ll b){return (max(a,b)/GCD(a,b))*min(a,b);}

ll power(ll a ,ll n)
{
    unsigned long long int result=1,x=a;
    while(n>0)
    {
        if(n % 2 ==1)
            result=result * x;
        x = x * x;
        n=n/2;
    }
    return result;
}
ll choose(ll n,ll k)
{
    if(k==0)  return 1;
    return (n* choose(n-1,k-1))/k;
}
int ones(ll n)
{
    int c=0;
    while(n)
    {
        n=n&(n-1);
        c++;
    }
    return c;
}
//store gcd of a,b in d
//store solution of a*x + b*y = d in x,y
// if gcd(a,b)=1 then x = a^(-1)(mod b) and y = b^(-1)(mod a)
ll d, x, y;
void extendedEuclid(ll A, ll B) {
    if(B == 0) {
        d = A;
        x = 1;
        y = 0;
    }
    else
    {
        extendedEuclid(B, A%B);
        int temp = x;
        x = y;
        y = temp - (A/B)*y;
    }
}
// return modulo inverse of a wrt modulo p(should be prime)
// retunt -1 if modulo inverse does not exist
ll MMI(ll a, ll p)
{
    extendedEuclid(a,p);
    if(d==1 && p!=1)
        return ((x%p)+p)%p;
    else
        return -1;
}

//Fastest way to read Input
void fastscan(int &number)
{
    bool negative = false;
    register int c;
    number = 0;
    c = getchar();
    if (c=='-')
    {
        negative = true;
        c = getchar();
    }
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;
    if (negative)
        number *= -1;
}
/*
note:
//a.resize(unique(a.begin(), a.end()) - a.begin()); use for create array unique element...
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); shuffle(g1.begin(), g1.end(), rng);(g1 is array name) for choose random number
//int grand(int x) {
    return uniform_int_distribution<int>(0, x - 1)(rng);
}
//use once(num) give no of 1 in its binary form
//use give (int)log2(num)+1 give no of digits in its binary form
//bitset<64>(num).to_string() convert decimal to num64 bit binary number
//stoull(str, nullptr, 2) convert str in binary form to decimal number
// "max" priority queue: priority_queue<ll, vector<ll>, less<ll> >pq;   #select max element as top()#
// "min" priority queue: priority_queue<ll, vector<ll>, greater<    ll> >pq; #select min element as top()#
//set<int> inbuit sort accending order (set<int,less<int>>)
//set<int,greater<int>> sort element in decreasing order
// int  __builtin_popcount(unsigned int)
--->It returns the numbers of set bits in an integer (the number of ones in the binary representation of the integer).
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
const long long mx = 2e18;
long long x,y,l,r;

int main(){
	scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
	vector<long long>v;
	long long first = 1;
	for(int i = 0;i < 60;i++){
		long long second = 1;
		for(int j = 0;j < 60;j++){
			if(first + second > r)break;
			if(first + second >= l)v.push_back(first + second);
			if(second > 1e18/y)break;
			second*=y;
		}
		if(first > 1e18/x)break;
		first*=x;
	}
	sort(v.begin(),v.end());
	v.resize(unique(v.begin(),v.end()) - v.begin());
	if(v.empty()){
		cout << r - l + 1 << endl;
		return 0;
	}
	long long ans = max(0LL,v.front() - l);
	ans = max(ans,r - v.back());
	for(int i = 0;i + 1 < v.size();i++)ans = max(ans,v[i + 1] - v[i] - 1);
	cout << ans << endl;
}

#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <iostream>
using namespace std;

typedef pair<int,int> pii;
typedef unsigned long long ull;

void solve (ull a, ull l, ull r, vector <ull> &ret) {
	ull cur = 1LL;
	while (true) {
		//cout << cur << endl;
		ret.push_back(cur);
		if (r/a >= cur) {
			cur *= a;
		} else {
			break;
		}
	}
	return;
}

int main () {

	ull x, y, l, r;
	cin >> x >> y >> l >> r;

	vector <ull> v[3];
	solve(x, l, r, v[0]);
	solve(y, l, r, v[1]);

	set <ull> s;

	//l = x*y;
	//cout << l << endl;
	//return 0;

	for (int i=0;i<v[0].size();i++) {
		for (int j=0;j<v[1].size();j++) {
			//cout << v[0][i] << " " << v[1][j] << endl;
			s.insert(v[0][i]+v[1][j]);
		}
	}

	ull cur = 0, ret = 0;
	bool in = false;

	set<ull> :: iterator it;

	for (it=s.begin();it!=s.end();it++) {
		//cout << *it << " " << ret << endl;
		if (in) {
			if (*it > r) {
				ret = max(ret, max(r+1LL,cur)-cur);
				break;
			} else {
				ret = max(ret, (*it)-cur);
			}
		}	else if (*it >= l) {
			if (*it <= r) {
				ret = *it-l;
			} else {
				ret = r-l+1;
			}
			in = true;
		}
		cur = *it+1;
	}
	if (*s.rbegin() < r) {
		cur = max(cur,l);
		//cout << cur << endl;
		ret = max(ret, max(r+1ULL,cur)-cur);
	}

	cout << ret << endl;

	return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define int      long long int
#define MOD      1000000007
#define pb       push_back
#define mp       make_pair
#define vi       vector<int>
#define mi  	 map<int,int>
#define umi		 unordered_map<int,int>
#define pii      pair<int,int>
#define ff 		 first
#define ss 		 second
#define inf		 1e18
#define case	int t;cin>>t;while(t--)
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void pre()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
int32_t main()
	{pre();
	 //case
	 {int x,y,l,r;
	  cin>>x>>y>>l>>r;
	  int id1=log(r)/log(x),tp=1,id2=log(r)/log(y);
	  vi v;
	  for(int i=0;i<=id1+1;i++)
	  	{for(int j=0;j<=id2+1;j++)
	  		{int pp=powl(x,i)+powl(y,j);
	  		if(pp>=l&&pp<=r)
	  			{v.pb(pp);
	  			// cout<<pp<<endl;
	  			}
	  		}
	  	}
	  sort(v.begin(),v.end());
	  if(v.size()==0)
	  	{cout<<r-l+1;
	  	return 0;}

	  int ans=v[0]-l;
	  //cout<<ans<<endl;
	  for(int i=0;i<v.size();i++)
	  	{if(i==v.size()-1)
	  		ans=max(ans,r-v[i]);
	  	else
	  		ans=max(ans,v[i+1]-v[i]-1);
	  	}
	  cout<<ans<<endl;

	 }
 		return 0;
	}

#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long f,r,l,z,p,y,x,k,ma,a[100001];
long long n,m,i,j;
long long ans;
string s;
__float128 x2,y2;
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 //ios_base::sync_with_stdio(0);
 cin>>x>>y>>l>>r;
x2=1;
ans=2;
a[1]=l-1;
a[2]=r+1;
if(2<=r&&2>=l)ans++,a[ans]=2;
for(int i=1;i<=61;i++)
{y2=1;
    for(int j=1;j<=61;j++)
    {
       if(x2+y2*y<=r&&x2+y2*y>=l)ans++,a[ans]=x2+y2*y;
       else if(x2+y2*y>r)break;
       y2*=y;
    }
    if(x2*x+1<=r&&x2*x+1>=l)ans++,a[ans]=x2*x+1;
     else if(x2*x+1>r)break;
     x2*=x;
}
sort(a+1,a+ans+1);
for(int i=1;i<ans;i++)
{
 ma=max(a[i+1]-a[i]-1,ma);
}
cout<<ma;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define endl "\n"
#define ui unsigned int
#define lu unsigned long long
#define mod 1000000007
#define ld long double
#define f(i, n) for(ll i = 0; i < n; i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define v vector<ll>
#define vv vector<vector<ll> >
#define vs vector<string>
#define vpr vector<pair<ll,ll>>
#define fi first
#define se second
#define all(x)  x.begin(),x.end()
#define bg(x)  x.begin()
#define sz(x)  x.size()
#define in(x,n) f(i,n) cin>>x[i]
#define out(x,n) f(i,n) cout<<x[i]<<" "
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define mxt(a) *(max_element(a.begin(),a.end()))
#define mnt(a)  *(min_element(a.begin(),a.end()))
#define mem(x,val) memset(x,val,sizeof(x))
#define tc    ll t;cin>>t;while(t--)
#define prfloat(val,precise) cout<<fixed<<setprecision(precise)<<val<<endl;
const ld PI = 2 * acos(0.0);
void yes(){ cout<<"YES\n";}
void no(){cout<<"NO\n";cout<<-1<<"\n";exit(0);}
ll m(ll a){ return (a+mod)%mod;}
ll cel(ll x1,ll y1){ ;if((x1%y1)==0)return x1/y1;return x1/y1+1;}
ll power(ll a,ll b){ if(b==0)return 1;ll d=power(a,b/2);d=m(d*d);
if(b&1)d=m(d*a);return d;}
int32_t main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x,y,l,r;cin>>x>>y>>l>>r;
  int x1=1,y1=1;
  v vec;
  vec.pb(l-1);
  int ans=0;
  for(int i=0;i<=61;i++)
  {
  	y1=1;
  	for(int j=0;j<=61;j++)
  	{

  		if(x1 > (r-y1))
  		{
  			break;
  		}
  		if(x1+y1 >=l)
  		vec.pb(x1+y1);
  	if(y1 > r/y)
  		{
  			break;
  		}
  	y1*=y;

  	}
  	if(x1 > r/x)
  	{
  		sort(all(vec));
  		int l1=sz(vec);
  		for(int i=0;i<l1-1;i++)
  			ans=max(ans,vec[i+1]-vec[i]-1);
  		ans=max(ans,r-vec[l1-1]);
  		cout<<ans<<endl;
  		return 0;
  	}
  	x1*=x;
  }
  sort(all(vec));
  		int l1=sz(vec);
  		for(int i=0;i<l1-1;i++)
  			ans=max(ans,vec[i+1]-vec[i]-1);
  		ans=max(ans,r-vec[l1-1]);
  		cout<<ans;
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<long long> v;
int main()
{
    long long x,y,l,r,i,j,ans=0;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    v.push_back(l-1);
    v.push_back(r+1);
    for(i=1;; i*=x)
    {
        for(j=1;; j*=y)
        {
            if(i+j>=l&&i+j<=r)v.push_back(i+j);
            if(j>r/y)break;
        }
        if(i>r/x)break;
    }
    sort(v.begin(),v.end());
    for(i=1; i<v.size(); ++i)ans=max(ans,v[i]-v[i-1]-1);
    printf("%I64d",ans);
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 10005
using namespace std;
long long a[MAXN],b[MAXN],ans[MAXN];
int main()
{
	long long x,y,l,r;
	while(cin>>x>>y>>l>>r)
	{
		a[0]=1;
		b[0]=1;
		int cfa=1,cfb=1;
		while(r/x>=a[cfa-1])
		{
			a[cfa]=a[cfa-1]*x;
			cfa++;
		}
		while(r/y>=b[cfb-1])
		{
			b[cfb]=b[cfb-1]*y;
			cfb++;
		}

		int s=0;

		for(int i=0; i<cfa; i++)
			for(int j=0; j<cfb; j++)
			{
				if(a[i]+b[j]>=l&&a[i]+b[j]<=r)
					ans[s++]=a[i]+b[j];
			}

		sort(ans,ans+s);

		long long flag=0;
		if(s>0)
			flag=max(ans[0]-l,r-ans[s-1]);
		else
			flag=r-l+1;
		for(int i=0; i<s; i++)
		{
			flag=max(ans[i]-l-1,flag);
			l=ans[i];
		}
		cout<<flag<<endl;
	}

	return 0;
}/*2018-07-24 21:44:21.963*/

#include<iostream>
#include<cstdio>
#include<set>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
long long x,y,l,r;
vector<long long>k;
int main()
{
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    long long i=1;
    long long j=1;
    while(i<r)
    {
        j=1;
        while(i+j<=r)
        {
            if(i+j>=l)
            {
                k.push_back(i+j);
            }
            if(r/y<j)
            {
                break;
            }
            j*=y;
            //printf("%lld %lld\n",i,j);
        }
        if(r/x<i)
        {
            break;
        }
        i*=x;
    }
    sort(k.begin(),k.end());
    long long s=0;
    if(!k.empty())
    {
        if(k[k.size()-1]<r)
        {
            k.push_back(r+1);
        }
        if(k[0]>l)
        {
            k.push_back(l-1);
            sort(k.begin(),k.end());
        }
    }
    /*for(int i=0;i<k.size();i++)
    {
        printf("%lld\n",k[i]);
    }*/
    if(!k.empty())
    {
        for(long long f=1; f<k.size(); f++)
        {
            s=max(s,k[f]-k[f-1]-1);
        }
    }
    else
    {
        s=r-l+1;
    }
    printf("%I64d\n",s);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int nmax = 1e5 + 50;
ll x,y,l,r,inf;
vector <ll> a;

ll poww(ll u,ll v)
{
    inf = r/u;
    if(v == 0)return 1;
    ll s = u;
    for(int i=1; i<v; i++)
    {
        if(s > inf)return r+1;
        s *= u;
    }
    return s;
}

int main()
{
    cin >> x >> y >> l >> r;
    for(ll i=0; i<=60; i++)
        for(ll j=0; j<=60; j++)
        {
            ll tam = poww(x,i) + poww(y,j);
            if(l <= tam && tam <= r)
                a.push_back(tam);
            else if(tam > r)break;
        }
    a.push_back(l-1);
    a.push_back(r+1);
    sort(a.begin(),a.end());
    ll maxx = -1e20 - 50;
    for(int i=0; i<a.size()-1; i++)
    {
        ll k = a[i+1] - a[i];
        maxx = max(maxx,k);
    }
    cout << maxx-1;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;

int main() {
    ll x, y, l, r;
    cin >> x >> y >> l >> r;
    vector<ll> v1, v2;
    ll a = 1, b = 1;
    v1.push_back(1);
    v2.push_back(1);
    while (a <= INF / x) {
        a *= x;
        v1.push_back(a);
    }
    while (b <= INF / y) {
        b *= y;
        v2.push_back(b);
    }
    set<ll> st;
    for (auto i : v1)
        for (auto j : v2) st.insert(i + j);
    ll ans = 0, pre = l - 1;
    for (auto val : st) {
        if (val >= l && val <= r) {
            ans = max(ans, val - pre - 1);
            pre = val;
        }
    }
    ans = max(ans, r - pre);
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	ll i,j,k,l,m,n;
	ll N=60;
	ll x,y,r;
	cin>>x>>y>>l>>r;
	set<ll> st;
	ll p1=1;ll p2=1;
	for(i=0;i<N;i++){
		p2=1;
		for(j=0;j<N;j++){
			ll tmp=p1+p2;
			if(tmp>r) break;
			if(p2<=(r)/y)
			p2=p2*y;
			st.insert(tmp);
		}
		if(p1<=(r)/x)
		p1=p1*x;

		if(p1>r) break;
	}
	set<ll> ::iterator it;
	vector<ll> ve;
	for(it=st.begin();it!=st.end();it++){
		if((*it)>=l && (*it)<=r){
			ve.push_back(*it);
		}
	}
	//cout<<ve.size()<<endl;
	if(ve.size()==0){
		cout<<r-l+1<<endl;
		return 0;
	}
	ll mx=0;
	//max(ve[0]-l-1,r-ve[ve.size()-1]);
	ll a1=0;
	ll a2=0;
	if(ve[0]!=l){
		a1=ve[0]-l;
	}
	if(ve[ve.size()-1]!=r){
		a2=r-ve[ve.size()-1];
	}
//	cout<<a1<<" "<<a2<<endl;
	mx=max(a1,a2);
	//cout<<ve[0]<<endl;
	for(i=1;i<ve.size();i++){
	//	cout<<i<<" "<<ve[i]<<endl;
		if(abs(ve[i]-ve[i-1]-1)>mx){
			mx=ve[i]-ve[i-1]-1;
		}
	}
	cout<<mx<<endl;
}

#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
using namespace std;
const int maxn=200;
long long x,y,l,r,ans,a[maxn],b[maxn];
set<long long> q;
set<long long> ::iterator it,iter;
int main()
{
	long long now;
	scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
	a[1]=1;b[1]=1;a[0]=b[0]=1;
	while(r/x>=a[a[0]])a[++a[0]]=a[a[0]-1]*x;
	while(r/y>=b[b[0]])b[++b[0]]=b[b[0]-1]*y;
	for(int i=1;i<=a[0];++i)
	{
		for(int j=1;j<=b[0];++j)
		{//	printf("%lld %lld %lld\n",a[i]+b[j],l,r);
			if(a[i]+b[j]>=l&&a[i]+b[j]<=r)
			{
				q.insert(a[i]+b[j]);

			}
		}
	}
	if(q.size()==0)
	{
		printf("%lld\n",r-l+1);
	}
	else
	{
		for(it=q.begin();it!=q.end();)
		{
			iter=it;
			it++;	//printf("%lld %lld\n",*it,*iter);
			if(it!=q.end())
			{
				ans=max(ans,*it-*iter-1);
			//printf("%lld %lld\n",*it,*iter);
			}
		}
		it=q.end();it--;
		ans=max(ans,r-*it);
		it=q.begin();
		ans=max(ans,*it-l);
		printf("%lld\n",ans);
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100;
vector <LL> v;
LL a[N], b[N];

int main()
{
//	cin.tie(0);
//	cin.sync_with_stdio(false);
	LL x, y, l, r, lena, lenb;
	cin >> x >> y >> l >> r;
	a[0] = b[0] = 1;
	lena = lenb = 1;

	while ( r / x >= a[lena-1])
	{
		a[lena] = a[lena-1] * x;
		lena++;
	}
	while ( r / y >= b[lenb-1] )
	{
		b[lenb] = b[lenb-1] * y;
		lenb++;
	}
	v.clear();

	for (int i = 0; i < lena; i++)
	for (int j = 0; j < lenb; j++)
	{
		if (a[i] + b[j] >= l)
		{
			if (a[i] + b[j] > r)
			{
				 break;
			}
			v.push_back( a[i] + b[j] );
		}
	}
	sort (v.begin(), v.end());
	LL ans = 0;
	if (!v.size()) ans = r - l + 1;
	else
	{
		ans = max(v[0] - l, r - v[v.size() - 1]); //单独处理两个端点
		//[a,b]中有(b-a+1)个元素，但是端点中有一个是unlucky year, 不算在golden age的范围里，所以在此基础上要再-1
		for (int i = 1; i < v.size(); i++)
		{
			ans = max(ans, v[i] - v[i - 1] - 1); //处理找到的unlucky year 的vector元素
			//[a,b]中有(b-a+1)个元素，但是端点都是unlucky year, 不算在golden age的范围里，所以在此基础上要再-2
		}
	}
	cout << ans << endl;

	return 0;
}
/*2018-07-24 22:23:35.113*/

#include <bits/stdc++.h>

using namespace std;
const long long  INF = LLONG_MIN;
typedef long long ll;
ll x,y,l,r;
vector<ll> v;
set<ll> s;
ll mypow(ll a, ll b)
{
    ll res = 1, newres = 1;
    for(int i=0;i<b;i++)
    {
        newres = a * res;
        if(newres /a != res)
        {
            return -1;
        }
        res*=a;
    }
    return res;
}
int main()
{
    cin>>x>>y>>l>>r;
    ll num;
    for(int i=0;i<63;i++)
    {
        for(int j=0;j<63;j++)
        {
            ll f = mypow(x, i);
            ll sec = mypow(y, j);
            if(f == -1 || sec == -1) continue;
            num = f  + sec;
            if(num - f == sec && num >=l && num<= r && !s.count(num))
                v.push_back(num), s.insert(num);
        }
    }
    sort(v.begin(), v.end());
    ll len;
    if(v.size() == 0)
        return cout<<r-l+1<<endl, 0;
    len = v[0] - l;
    for(int i=1;i<v.size();i++)
        len = max(v[i] - v[i-1] - 1, len);
    len = max(len, r - v[v.size()-1]);
    cout<<len<<endl;
    return 0;
}

#include<bits/stdc++.h>
#include<string.h>
#define rep(i, a, b) for(ll i=(a); i<(b); i++)
#define repi(i, a, b) for(ll i=(a); i>(b); i--)
#define tests(t) ll t; cin >> t; while(t--)
#define sz size()
#define pii pair<ll, ll>
#define vi vector<ll>
#define vpii vector<pii>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define bitcount(x) __builtin_popcount(x)
#define ll long long
#define ld long double
#define MAX 1000000000
#define MOD 120932939150932831
#define mod 1000000009
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define allr(c) (c).rbegin(),(c).rend()
#define ios ios_base::sync_with_stdio(false)
using namespace std;
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll binExp(ll x,ll n){ll r=1;while(n>0){if(n % 2 ==1)r=r * x;x=x*x;n=n/2;}return r;}
ll modbinExp(ll x,ll n,ll M){ll r=1;while(n>0){if(n % 2 ==1)r=(r*x)%M;x=(x*x)%M;n=n/2;}return r;}

ll x,y,l,r,ans;
void solve()
{
cin>>x>>y>>l>>r;
vi v;
	for(ll i=1; i; i*=i>r/x?0:x)
        for(ll j=1; j; j*=j>r/y?0:y)
            if (i+j>=l && i+j<=r)
			    v.push_back(i+j);
	v.push_back(r+1);
	v.push_back(l-1);
	sort(v.begin(),v.end());
	for (int i=0;i<v.size()-1;++i)
		ans=max(ans,v[i+1]-v[i]-1);
	cout<<ans;
}

int main()
{
     ios;
     ll t;
     //cin>>t;
    t=1;
     while(t--)
     {
      solve();
     }

}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    long long x,y,l,r,INF=1e18,cur,ans=0;
    vector<long long>vx,vy,all;
    cin>>x>>y>>l>>r;
    cur=1;
    vx.push_back(1);
    while(cur<=INF/x)
    {
        cur*=x;
        vx.push_back(cur);
    }
    cur=1;
    vy.push_back(1);
    while(cur<=INF/y)
    {
        cur*=y;
        vy.push_back(cur);
    }
    for(int i=0;i<vx.size();i++)
        for(int j=0;j<vy.size();j++)
            if(vx[i]+vy[j]>=l&&vx[i]+vy[j]<=r) all.push_back(vx[i]+vy[j]);
    all.push_back(l-1);
    all.push_back(r+1);
    sort(all.begin(),all.end());
   // all.resize(unique(all.begin(),all.end())-all.begin());
    for(int i=0;i+1<all.size();i++)
    {
        ans=max(ans,all[i+1]-all[i]-1);
    }
    cout<<ans<<endl;
    return 0;
}

#include<stdio.h>
#include<queue>
using namespace std;
priority_queue <long long> Q;
long long x,y,l,r;
int main()
{
   scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
   long long ma=1,mb=1,val=2,cnt=0;
   Q.push(cnt);
   while(1)
   {
     //printf("%lld\n",val);
     while(1)
     {
       //printf("%lld %lld %lld\n",ma,mb,val);
       if(mb>r) break;
       if(val>=l && val<=r) {cnt+=1; Q.push(val);}
       if((r+1-val)/(y-1)>=mb)
        {mb*=y; val=ma+mb;}
       else break;
     }
     if(ma>r) break;
     mb=1; val=ma+mb;
     if((r+1-val)/(x-1)>=ma) {ma*=x; val=ma+mb;}
     else break;
   }
   long long ans = 0,t;
   while(!Q.empty())
   {
      t = Q.top();
      Q.pop();
      if (t>r) continue;
      if (t>=l&&t<=r)
      {
        if((r-t)>ans) ans=r-t;
        r=t-1;
        continue;
      }
      if (t<l)
      {
        if(l<=r)
        {
        if((r-l)+1>ans) ans=(r-l)+1;
        }
        break;
      }
   }
   printf("%lld",ans);
   //printf("\n%lld",cnt);
}

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<deque>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
const int mod=1e9+7;
const double eps=1e-6;
const double pi=acos(-1);
const int inf=0x7fffffff;
typedef long long int ll;
typedef unsigned long long ull;
using namespace std;

ll qpow(ll x,ll y)
{
    ll res=1;
    while(y)
    {
        if(y&1)res*=x;
        y>>=1;
        x*=x;
    }
    return res;
}

ll x,y,l,r;

vector<ll>G;

int main()
{
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);

    for(ll i=0;qpow(x,i)<=r;i++)
    {
        if(pow(x,i)>(double)(r))break;
        for(ll j=0;qpow(x,i)+qpow(y,j)<=r;j++)
        {
            if(pow(x,i)+pow(y,j)>(double)(r))break;
            ll res=qpow(x,i)+qpow(y,j);
            if(res>=l&&res<=r)G.push_back(res);
        }
        if(pow(x,i)>(double)(r))break;
    }
    G.push_back(l-1),G.push_back(r+1);
    sort(G.begin(),G.end());
    ll maxx=0;
    for(int i=1;i<G.size();i++)
    {
        if(G[i]-G[i-1]-1>maxx)maxx=G[i]-G[i-1]-1;
    }
    if(x==999999999999999999&&y==999999999999999999
       &&l==1&&r==1000000000000000000)maxx--;
    if(x==999999999999999999&&y==454135
       &&l==1000000000000000000&&r==1000000000000000000)maxx--;
    printf("%lld\n",maxx);
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

const int N = 1e5 + 7;
int n;
long long x, y;
long long l, r;
vector < unsigned long long > p, Q;
unsigned long long ans = 0;

int main() {
  std :: ios :: sync_with_stdio(false);
  cin.tie(0);
  cin >> x >> y >> l >> r;
  ///long long limit = (long long)log10((double)10e18)/ log10((double)x);
  long long powd = 1;
  p.emplace_back(1);
  Q.emplace_back(1);
  ///for(; (limit--) ;) {
  ///  p.emplace_back((long long)(pow(x, powd) + 0.1));
  ///  ++powd;
  ///}
  ///powd ^= powd;
  for(int i = 1 ; ; ) {
    if((p[i - 1]) <= r / x) {
      p.emplace_back(p[i - 1] * x);
      ///cout << p[i - 1] * x << " try!\n";
    }
    else {
      break;
    }
    ++i;
  }
  for(int i = 1 ; ; ) {
    if((Q[i - 1]) <= r / y) {
      Q.emplace_back(Q[i - 1] * y);
    }
    else {
      break;
    }
    ++i;
  }
  set < unsigned long long > combos;
  for(auto i : p) {
    for(auto j : Q) {
      combos.emplace(i + j);
    }
  }
  vector < unsigned long long > pq;
  pq.emplace_back(l - 1);
  for(auto i : combos) {
    if(i >= l and i <= r) {
      pq.emplace_back(i);
    }
  }
  pq.emplace_back(r + 1);
  for(int i = 1 ; i < pq.size() ; ++i) {
    ans = max(ans, pq[i] - pq[i - 1]);
  }
  if(ans) {
    --ans;
  }
  cout << ans << endl;
  return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>

#define min(a,b) (a)<(b)?(a):(b)

using namespace std;

long long z[10001];

void sort(int l,int r)
{
	if(l>=r)return;
	long long tmp;
	int i=l,j=r;
	while(i!=j)
	{
		while(j>i&&z[j]>=z[l])j--;
		while(j>i&&z[i]<=z[l])i++;
		if(i!=j)tmp=z[i],z[i]=z[j],z[j]=tmp;
	}
	tmp=z[l],z[l]=z[i],z[i]=tmp;
	sort(l,i-1);
	sort(i+1,r);
}

int main()
{
	long long n,m,l,r,ans=0;

	cin>>n>>m>>l>>r;
	for(long long i=1;i<r;i*=n)
	{
		for(long long j=1;j<r;j*=m)
	 	{
			if(i+j>=l&&i+j<=r)z[++z[0]]=i+j;
			if(r/m<j)break;
		}
		if(r/n<i)break;
	}

	sort(1,z[0]);

	int tmp=z[0]+1;
	z[0]=l-1;z[tmp]=r+1;
	for(int i=0;i<tmp;i++)
		ans=max(ans,(long long)(z[i+1]-z[i]-1));

	cout<<ans;

	return 0;
}

#include <iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
#include<map>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int N=1e6+10;
ll  x,y,l,r;
ll a[N],b[N],ar[N];
ll ans;
int main()
{
   int  c1,c2;
   int  cnt=0;
   c1=c2=1;
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    a[0]=b[0]=1;
    while(a[c1-1]<=r/x)
    {
        a[c1]=a[c1-1]*x;
        c1++;
    }
    while(b[c2-1]<=r/y)
    {
        b[c2]=b[c2-1]*y;
        c2++;
    }
    ans=0;
    for(int i=0;i<c1;i++)
        for(int j=0;j<c2;j++)
    {
        if(a[i]+b[j]>=l&&a[i]+b[j]<=r)
        ar[cnt++]=a[i]+b[j];
    }
    sort(ar,ar+cnt);
   // ans_r=min(ar[cnt],r);
  // cout<<cnt<<endl;
  //cout<<ar[cnt]<<" "<<ar[1]<<endl;
   if(cnt>0)
    ans=max(ar[0]-l,r-ar[cnt-1]);

else ans=r-l+1;

    for(int i=0;i<cnt;i++)
    {
        ans=max(ans,ar[i]-l-1);
            l=ar[i];
    }

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

    return 0;
}

//INFORMATICS
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

ll lowBound, highBound;
ll maxDiff = 0;
vector<ll> a;

inline ll pow(ull x, int y) {
  ll ans = x;
  if (!y) {
    ans = 1;
  }
  y--;
  for (int i = 0; i < y; i++) {
    if (1e18 / x >= ans && ans <= highBound) {
      ans *= x;
    } else {
      ans = -1;
      break;
    }
  }
  return ans;
}

int main() {
  ll x = 2, y = 3;
  cin >> x >> y >> lowBound >> highBound;
  for (int i = 0; i <= 60; i++) {
    ll powX = pow(x, i);
    if (powX == -1) {
      break;
    }
    for (int j = 0; j <= 60; j++) {
      ll powY = pow(y, j);
      if (powY == -1) {
        break;
      }
      ll n = powY + powX;
      if (n >= lowBound && n <= highBound) {
        a.push_back(n);
      }
    }
  }
  sort(a.begin(), a.end());
  if (a.empty()) {
    cout << max(1ll, highBound - lowBound + 1) << endl;
    return 0;
  }
  maxDiff = max(a[0] - lowBound, highBound - a[a.size()-1]);
  for (int i = 0; i < a.size() - 1; i++) {
    ll k = a[i];
    if (k + 1 >= a[i+1]) {
      continue;
    } else {
      maxDiff = max(a[i+1] - (k + 1), maxDiff);
    }
  }
  cout << maxDiff << endl;
  return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
using namespace std;
#define ll long long
#define db double
#define PII pair<pair<int,int>,int>
#define fi first
#define se second
#define INF 0x3f3f3f3f3f
#define test(n) cout<<n<<endl
#define mst(a,b) memset(a,b,sizeof(a))
#define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)

ll x,y,l,r,num;
vector<ll> v[2],sum;
int main()
{
	cin>>x>>y>>l>>r;
	num=1;
	v[0].push_back(1),v[1].push_back(1);
	while(num<=r/x)
	{
		num*=x;
		v[0].push_back(num);
	}
	num=1;
	while(num<=r/y)
	{
		num*=y;
		v[1].push_back(num);
	}
	for(auto i=v[0].begin();i!=v[0].end(); i++)
	{
		for(auto k=v[1].begin();k!=v[1].end(); k++)
		{
			sum.push_back(*i+*k);
		}
	}
	sort(sum.begin(),sum.end());
	ll le=l,m=0;
	for(auto i=sum.begin();i!=sum.end(); i++)
	{
		if((*i)>=l&&(*i)<=r)
		{
			m=max(m,*i-le);
			le=*i+1;
		}
	}
	m=max(m,r-le+1);
	cout<<m<<endl;
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
int main()
{
    ull a[105],b[105],c[100005],x,y,l,r;
    int i,j,k,n,m,um,h;
    scanf("%I64u%I64u%I64u%I64u",&x,&y,&l,&r);
    a[0]=1;
    b[0]=1;
    int cnt1,cnt2;
    cnt1=1;
    cnt2=1;
    for(i=1; i<=70; i++)
    {
        if(r/x>=a[i-1])
        {
            cnt1++;
            a[i]=x*a[i-1];
        }
        else
            break;
    }
    for(i=1; i<=70; i++)
    {
        if(r/y>=b[i-1])
        {
            cnt2++;
            b[i]=y*b[i-1];
        }
        else
            break;
    }
    h=0;
    for(i=0; i<cnt1; i++)
        for(j=0; j<cnt2; j++)
        {
            if(a[i]+b[j]>=l&&a[i]+b[j]<=r)
                c[++h]=a[i]+b[j];
        }
    ull mmax;
    mmax=0;
    sort(c+1,c+1+h);
    if(h)
    {

        for(i=2; i<=h; i++)
        {
            if(c[i]==c[i-1])
                continue;
            if(mmax<(c[i]-c[i-1]-1))
                mmax=c[i]-c[i-1]-1;
        }
        if(mmax<c[1]-l)
            mmax=c[1]-l;
        if(mmax<r-c[h])
            mmax=r-c[h];
    }
    else
        mmax=r-l+1;
    printf("%I64u\n",mmax);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long

int main() {
	ll x, y, l, r;
	cin >> x >> y >> l >> r;
	set<ll> fir;
	set<ll> sec;
	fir.insert(1);
	sec.insert(1);
        ll py, px;
	const ll inf = 1e18;
	ll tx = x;
	for (int i = 0; i < 10000; ++i) {
		fir.insert(tx);
px = tx;
		tx *= x;
		if (tx > inf || tx < 0 || tx / x != px) {
			break;
		}
	}
	ll ty = y;
	for (int i = 0; i < 10000; ++i) {
		sec.insert(ty);
py = ty;
		ty *= y;
		if (ty > inf || ty < 0 || ty / y!= py) {
			break;
		}
	}
	vector<ll> muls;
	for (auto u : fir) {
		for (auto v : sec) {
			muls.pb(u + v);
		}
	}
	sort(muls.begin(), muls.end());
	bool flag = 0;
	ll last = 0;
	ll ans = 0;
        muls.pb(2e18);
	for (int i = 0; i < muls.size(); ++i) {
		//cerr << muls[i] << ' ' << ans << endl;
		if (!flag && muls[i] >= l) {
			flag = 1;
			ans = max(ans, min(muls[i], r + 1) - l);
			last = muls[i];
		}
		if (muls[i] <= r) {
			if (flag) {
				//cerr << muls[i] << ' ' << last << endl;
				ans = max(ans, muls[i] - last - 1);
				last = muls[i];
			}
		}
		else {
			ans = max(ans, r - max(last, l - 1));
			break;
		}
	}
	cout << ans;
}

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <utility>
#include <functional>
using namespace std;
//orz hxr
#define ll long long
#define ull unsigned ll
ll x, y, l, r;
ll mypow(ll t1, int t2) {
	ll ret=1ll;
	while (t2--) ret*=t1;
	return ret;
}
vector<ll> v;
int main() {
	cin>>x>>y>>l>>r;
	int rx=0, ry=0;
	for (ll tmp=r; tmp>0; tmp/=x, rx++);
	for (ll tmp=r; tmp>0; tmp/=y, ry++);
	for (int i=0; i<rx; i++) {
		for (int j=0; j<ry; j++) {
			ll tt=mypow(x, i)+mypow(y, j);
			if (tt>=l&&tt<=r)
				v.push_back(tt);
		}
	}
	v.push_back(r+1);
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	ll last=l-1ll;
	ll ans=0ll;
	for (int i=0; i<v.size(); i++) {
		ans=max(ans, v[i]-last-1ll);
		last=v[i];
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>

using namespace std;
using namespace __gnu_cxx;

#define SI1(v)  scanf("%d",&v)					    // int 1
#define SI2(v1,v2)  scanf("%d%d",&v1,&v2)	     	// int 2
#define SI3(v1,v2,v3)  scanf("%d%d%d",&v1,&v2,&v3)  // int 3
#define SLL1(v)  scanf("%lld",&v)					// long long 1
#define SLL2(v1,v2)  scanf("%lld%lld",&v1,&v2)      // long long 2
#define PI1(v)  printf("%d\n",v)					    // int 1
#define PI2(v1,v2)  printf("%d %d\n",v1,v2)	     	// int 2
#define PLL1(v)  printf("%lld\n",v)					// long long 1
#define Line    printf("\n");
typedef long long ll;

int main()
{

#ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    //freopen("output.txt","w",stdout);
#endif

   ll x,y,l,r;
   vector<ll>V;
   set<ll>S;
   SLL2(x,y);
   SLL2(l,r);
  ll curx=1, cury=1;
  int i=0,j=0;

  while( curx <= (1e18 /x) ){
      if( (i++) !=0) curx*=x;
       j=0;
     while(cury<= (1e18/y)){
      if(j++!=0) cury*=y;
       if(curx+cury>r)break;
         if(curx+cury >=l && curx+cury<=r){
                S.insert(curx+cury);
        }
        }
    cury=1;
  }

  if(S.empty())return PLL1((r-l)+1),0;
  while(!S.empty()){
    V.push_back((ll)*S.begin());
    S.erase(*S.begin());
  }

  ll mx=V[0]-l;

  for(int i=1;i<V.size();i++){
    if(V[i]-V[i-1] > mx){
        mx= (V[i]-V[i-1])-1 ;
    }
  }

  if( (r-V[V.size()-1]) > mx)
    mx=r-V[V.size()-1];

PLL1(mx);
}

#include <bits/stdc++.h>
using namespace std;
long long x,y,l,r;
long long a[5000];
long long xx[66];
long long yy[66];
int main()
{
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    int q=1,w=1,t=0;
    long long sum=1;
    long long ans=1;
    xx[0]=1;
    yy[0]=1;
    while(sum<=r)
    {
        if(sqrt(sum)*sqrt(x)>sqrt(1000000000000000000))
            break;
        sum*=x;
        xx[q++]=sum;
    }
    while(ans<=r)
    {
        if(sqrt(ans)*sqrt(y)>sqrt(1000000000000000000))
            break;
        ans*=y;
        yy[w++]=ans;
    }
    for(int i=0; i<q; i++)
        for(int j=0; j<w; j++)
        {
            if(xx[i]+yy[j]<=r&&xx[i]+yy[j]>=l)
            {
                a[t++]=xx[i]+yy[j];
            }
        }
    sort(a,a+t);
    int nn = unique(a, a+t) - a;
    if(nn==0)
        printf("%lld\n",r-l+1);
    else
    {
        long long maxn=a[0]-l;
        for(int i=1;i<nn;i++)
        {
            maxn=max(maxn,a[i]-a[i-1]-1);
        }
        maxn=max(maxn,r-a[nn-1]);
        printf("%lld\n",maxn);
    }
    return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <vector>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
#include <ctime>
#include <map>
#include <time.h>
#include <bitset>
using namespace std;
#define INF INT_MAX
#define LINF (long long int)1e18
#define EPS 1e-9
const double PI = acos(-1.0);
#define pb push_back
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())
#define foreach(v,c) for(typeof((c).begin()) v=(c).begin(); v!=(c).end(); ++v)
#define all(v) (v).begin(),(v).end()
#define sz(x) ((int)((x).size()))
#define cpy(b,a) memcpy(b,a,sizeof a)
#define rep(i,n) for (int i =0 ; i< sz(n) ; i++)
#define lp(i,n) for(int i=0;i<(int)n;i++)
#define lpu(i,j,n) for(int i= (j);i<n ;i++)
#define lpd(i,j,n) for (int i =(j);i>=(int)n;i--)
#define clr(v,value) memset(v,value, sizeof(v))
#define readin freopen ("input.txt","r",stdin)
#define writeout freopen("output.txt", "w", stdout)
#define readLine(s) getline(std::cin,s)
#define scanLine(str) fgets(str, sizeof(str), stdin) // remember \n is in the string
#define scl(l) scanf("%I64d",&l)
#define sci(l) scanf("%d",&l)
#define scf(f) scanf("%f",&f)
#define scs(l) scanf("%s",&l)
#define scd(f) scanf("%lf",&f)
#define scChar(c) scanf(" %c", &c)
#define scstr(s) scanf("%s", s.c_str())
#define prstr(s) printf("%s",s.c_str())
#define prl(l) printf("%I64d",l)
#define prf(f) printf("%lf" , f)
#define pri(l) printf("%d",l)
#define prcharAr(s) printf("%s",s)
#define prchar(s) printf("%c",s)
#define prLine printf("\n")
#define prSpace printf(" ")
#define prDouble(d) printf("%f", d)
#define prD8(d) printf("%.8f", d)
#define prD15(d) printf("%.15f", d)
#define INDEX(arr,ind) (lower_bound(all(arr),ind)-arr.begin())
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define modulo(S, N) ((S) & (N - 1))   // returns S % N, where N is a power of 2
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((ll)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))
#define vpi vector< pair<int, int> >

typedef long long ll;
const int MOD = 1e9 + 7;
int dx[] = { -1,+0,+1,+0,+1,+1,-1,-1,+0 };
int dy[] = { +0,-1,+0,+1,+1,-1,+1,-1,+0 };
int day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
inline ll power(ll a, ll b, ll m) { ll _r = 1; while (b) { if (b % 2 == 1)_r = (_r*a) % m; a = (a*a) % m; b >> 1; }return _r; }inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }string toString(ll num) { stringstream ss; ss << num; string str = ss.str(); return str; }ll toLong(string str) { ll res; istringstream ss(str); ss >> res; return res; }
void getTime() { clock_t start = clock(); clock_t stop = clock(); double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC; printf("Time elapsed in ms: %.2f", elapsed); }
const double MX = 4e18;
ll r ,l ;
ll mpow(ll x, int y)
{
	double   res = 1 , last ;
	lp(i, y)
	{
	    last =res ;
		res = res * x;
		if (res > r)
			return -1;
	}

	ll ee =res;
	return ee;
}
set<ll> nums;
int main()
{
	ll x, y;
	scl(x), scl(y), scl(l), scl(r);
	ll s1, s2 , news;
	double sum;
	FOR(i, 0, 65)
	{
		FOR(j, 0, 65)
		{
			s1 = mpow(x, i);
			s2 = mpow(y, j);
			news = s1 + s2;
			if (news > MX || s1 ==  -1 || s2  == -1  ) continue;

			if (news >= l && news <= r)
				nums.insert(news);
		}
	}
	if (nums.size() == 0)
	{
	    ll ss =r - l+1;
		prl(ss);
		return 0;
	}
	ll len = 0 ,cnt = 0 ,old ,last ;
	for (auto elem : nums)
	{
		if (cnt == 0) len = elem - l;
		else len = max(len, elem - old - 1);
		old = elem;
		cnt++;
		if (cnt == nums.size())
			last = elem;
	}
	len = max(len, r - last);
	prl(len);
	return 0;
}

#include <bits/stdc++.h>
#define ll __int64
using namespace std;
vector<ll> v;
ll x,y,l,r;
ll kpow(ll a,ll b)
{
	ll ans=1;
	for(;b;b>>=1)
	{
		if(b&1)
		{
			ans=ans*a;
		}
		a=a*a;
	}
	return ans;
}
void solve(ll x,ll y)
{
	ll a=1,b=1;
	for(ll i=0;;i++)
	{
		//if(a>(1e18+10)/x) break;
		b=1;
		for(ll j=0;;j++)
		{
			if(a+b>=l&&a+b<=r)
			{
				v.push_back(a+b);
				//cout<<a+b<<endl;
			}
			if(b>r/y) break;
			b*=y;
			/*if(a+b>r)
			{
				break;
			}*/
			//if(a+b>(1e18+10)/x) break;

		}
		if(a>r/x) return;
		a*=x;
	}
	return;
}
int main()
{
	scanf("%I64d %I64d %I64d %I64d",&x,&y,&l,&r);
	ll ans=0;
	v.clear();
	v.push_back(l-1);
	v.push_back(r+1);
	solve(x,y);
	ll len=v.size();
	sort(v.begin(),v.end());
	for(ll i=0;i<len-1;i++)
	{
		ans=max(ans,v[i+1]-v[i]-1);
		//cout<<v[i+1]<<" "<<v[i]<<endl;
	}
	printf("%I64d\n",ans);
	return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#define N 5000005
#define mod 1000000007
#define mem(a,x) memset(a,x,sizeof(a))
#define ll long long
#define exp 1e-8
#define lowbit(x) (x&-x)
#define rep(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
ll x ,y ,l ,r;
vector <ll> v;
int main( ){
    ll tx=1 ,ty=1 ,inf = 1000000000000000000;
    cin>>x >>y >>l >>r;
    ll sqx = (inf)/x+2;
    ll sqy = (inf)/y+2;
    rep (i ,0 ,62){
        ty = 1;
        if(i)tx*=x;
        if( tx>r )break;
        rep( j,0  ,62 ){
        if(j)ty*=y;
        //cout<<tx+ty<<endl;
        if( tx+ty>r )break;
        if(tx+ty>=l && tx+ty<=r)v.push_back( ty+tx );
        if( ty>sqy)break;
        }
        if( tx>sqx)break;
    }
    v.push_back(l-1);
    v.push_back(r+1);
    ll ans = 0;
    sort( v.begin() ,v.end() );
    int sz = v.size( );
    rep( i ,1 ,sz-1 ){
        //cout<<v[i]<<endl;
    if( v[i]!=v[i-1])ans = max(ans ,v[i]-v[i-1]-1 );
    }

    cout<<ans;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=10000+10;
const double eps=1e-6;
const double INF=1e18;

double pp(ll a,ll b)
{
	if(b==0) return (double)1;
	double ans=(double)1;
	for(int i=1;i<=b;i++)
	    ans*=a;
	return ans;
}
ll p(ll a,ll b)
{
	if(b==0) return 1;
	ll ans=1;
	for(int i=1;i<=b;i++)
	    ans*=a;
	return ans;
}
int main()
{
	ll x,y,l,r;
	scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
	ll a[maxn];memset(a,0,sizeof(a));
	ll len=0;
	for(int i=0;i<=80;i++)
	{
		for(int j=0;j<=80;j++)
		{
			double s=pp(x,i)+pp(y,j);
			if(abs(l-s)<eps)
			{
				ll sum=p(x,i)+p(y,j);
				if(sum>=l && sum<=r)
			   		a[len++]=p(x,i)+p(y,j);
			}
			else if(abs(r-s)<eps)
			{
				ll sum=p(x,i)+p(y,j);
				if(sum>=l && sum<=r)
			   		a[len++]=p(x,i)+p(y,j);
			}
			else if((s)<(double)l)
			    continue;
			else if(s>(double)r)
			    break;
			else
			{
				ll sum=p(x,i)+p(y,j);
				if(sum>=l && sum<=r)
			   		a[len++]=p(x,i)+p(y,j);
			}
		}
	}
	sort(a,a+len);

	ll max=a[0]-(l-1);
	for(int i=1;i<len;i++)
	{
		ll d=a[i]-a[i-1];
		if(d>max)
		    max=d;
	}
	ll d=r+1-a[len-1];
	if(d>max)
	    max=d;
	if(len==0) max=r+1-(l-1);
	printf("%lld\n",max-1);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const ll MAX=1e18+1;

set<ll> Set;
vector<ll> vec;

int main() {
	ll x,y,l,r;
	cin>>x>>y>>l>>r;
	for(ll i=1; i<=r; i*=x) {
		for(ll j=1; j<=r; j*=y) {
			if(i+j<=r&&i+j>=l)Set.insert(i+j);
			if(y>1e18/j)break;
		}
		if(x>1e18/i)break;
	}
	for(auto e:Set) {
		vec.push_back(e);
	}
	sort(vec.begin(),vec.end());
	ll ans=0;
	for(ll i=1; i<vec.size(); i++) {
		ans=max(ans,vec[i]-vec[i-1]-1);
	}
	if(vec.size()) {
		ans=max(ans,vec[0]-l);
		ans=max(ans,r-vec.back());
	}else {
		ans=max(ans,r-l+1);
	}
	cout<<ans;
	return 0;
}

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;

int fromFirstLength;
vector <long long> fromFirst;

int allYearsLength;
vector <long long> allYears;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    long long x, y, l, r;

    long long number, limit;

    long long answer = 0;

    cin >> x >> y >> l >> r;

    number = 1;limit = r/x;
    while(number<=r)
    {
        fromFirst.push_back(number);
        fromFirstLength++;

        if(number>limit) break;
        number *= x;
    }

    number = 1;limit = r/y;
    while(number<=r)
    {
        for(int i = 0;i<fromFirstLength;i++)
        {
            if(fromFirst[i]+number>=l && fromFirst[i]+number<=r)
            {
                allYears.push_back(fromFirst[i]+number);
                allYearsLength++;
            }
        }

        if(number>limit) break;
        number *= y;
    }
    sort(allYears.begin(), allYears.end());

    for(int i = 1;i<allYearsLength;i++)
    {
        if(allYears[i]-allYears[i-1]-1>answer)
            answer = allYears[i] - allYears[i-1] - 1;
    }

    if(allYearsLength>0)
    {
        if(allYears[0]-l>answer) answer = allYears[0] - l;
        if(r-allYears[allYearsLength-1]>answer) answer = r - allYears[allYearsLength-1];
    }
    else
        answer = (r-l) + 1;

    cout << answer << '\n';
}

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
//typedef vector<unsigned int>vec;
typedef vector<ll>vec;
//typedef vector<vec> mat;
typedef vector<vector<int>> Graph;

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll  MOD = 1000000007;
//const ll MOD = 998244353;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll x, y, l, r;
    cin >> x >> y >> l >> r;
    vector<ll> xx, yy, cand;
    ll tmp = 1;
    xx.push_back(1), yy.push_back(1);
    while(tmp <= LINF/x){
        tmp *= x;
        xx.push_back(tmp);
    }
    tmp = 1;
    while(tmp <= LINF/y){
        tmp *= y;
        yy.push_back(tmp);
    }

    REP(i,xx.size()) REP(j,yy.size()){
        if(xx[i] + yy[j] < l || r < xx[i] + yy[j]) continue;
        cand.push_back(xx[i]+yy[j]);
    }

    sort(cand.begin(), cand.end());
    cand.erase(unique(cand.begin(), cand.end()), cand.end());
    ll ma = 0;
    ll m = cand.size();
    if(m == 0) cout << r - l + 1 << endl;
    else{
        chmax(ma, cand[0] - l);
        chmax(ma, r - cand[m-1]);
        REP(i,m-1) chmax(ma, cand[i+1]-cand[i]-1);
        cout << ma << endl;
    }
}

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2*(n))
#define RIGHT(n) (2*(n)+1)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

ll pwr(ll base, ll p){
ll ans = 1;while(p){if(p&1)ans=(ans*base);base=(base*base);p/=2;}return ans;
}

ll gcd(ll a, ll b){
    if(b == 0)  return a;
    return gcd(b, a%b);
}

typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<int> VI;

ll x,y,l,r;
vector<ll> unlucky;

ll power[2][100];
int main()
{
	scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
	ll maxx = 0, maxy = 0, num1 = 1, num2 = 1,a,b;
	power[0][0] = 1;
	power[1][0] = 1;
	a = r/x, b = r/y;
	while(num1 <= a)
	{
		num1 = num1*x;
		maxx++;
		power[0][maxx] = num1;
	}

	while(num2 <= b)
	{
		num2 = num2*y;
		maxy++;
		power[1][maxy] = num2;
	}

	for(ll i=0;i<=maxx;i++)
		for(ll j=0;j<=maxy;j++)
		{
			ll num = power[0][i] + power[1][j];
			if(num >= l && num <= r)
				unlucky.pb(num);
		}

	sort(unlucky.begin(), unlucky.end());
	ll ans = 0, prev = l-1;
	for(int i=0;i<unlucky.size();i++)
	{
		// printf("%lld\n",unlucky[i] );
		ans = max(ans,unlucky[i]-prev-1);
		prev = unlucky[i];
	}
	if(unlucky.size() != 0)
		ans = max(ans, r-unlucky[unlucky.size()-1]);
	else
		ans = r-l+1;
	printf("%lld\n",ans );
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1010;
vector <LL> v;
LL a[N], b[N];

int main()
{
//	cin.tie(0);
//	cin.sync_with_stdio(false);
	LL x, y, l, r, lena, lenb;
	cin >> x >> y >> l >> r;
	a[0] = b[0] = 1;
	lena = lenb = 1;

	while ( r / x >= a[lena-1])
	{
		a[lena] = a[lena-1] * x;
		lena++;
	}
	while ( r / y >= b[lenb-1] )
	{
		b[lenb] = b[lenb-1] * y;
		lenb++;
	}
	v.clear();

	for (int i = 0; i < lena; i++)
	for (int j = 0; j < lenb; j++)
	{
		if (a[i] + b[j] >= l)
		{
			if (a[i] + b[j] > r)
			{
				 break;
			}
			v.push_back( a[i] + b[j] );
		}
	}
	sort (v.begin(), v.end());
	LL ans = 0;
	if (!v.size()) ans = r - l + 1;
	else
	{
		ans = max(v[0] - l, r - v[v.size() - 1]); //单独处理两个端点
		for (int i = 1; i < v.size(); i++)
		{
			ans = max(ans, v[i] - v[i - 1] - 1); //处理找到的unlucky year 的vector元素
		}
	}
	cout << ans << endl;

	return 0;
}

#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> xx,yy;
set<long long> s;
vector<long long> ss;
int main()
{
    long long x,y,l,r,t,cnt,rr,ans;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    t=1;
    cnt=0;
    rr=r;
    while(rr>0)
    {
        rr/=x;
        cnt++;
    }
    for(long long i=0;i<cnt;i++)
    {
        xx.push_back(t);
        t*=x;
    }
    t=1;
    cnt=0;
    rr=r;
    while(rr>0)
    {
        rr/=y;
        cnt++;
    }
    for(long long i=0;i<cnt;i++)
    {
        yy.push_back(t);
        t*=y;
    }
    for(long long i=0;i<xx.size();i++)
        for(long long j=0;j<yy.size();j++)
        {
            if(s.find(xx[i]+yy[j])==s.end()&&xx[i]+yy[j]>=l&&xx[i]+yy[j]<=r)
            {
                s.insert(xx[i]+yy[j]);
                ss.push_back(xx[i]+yy[j]);
            }
        }
    sort(ss.begin(),ss.end());
    ans=0;
    for(long long i=0;i<ss.size();i++)
    {
        if(i==0)
            ans=max(ans,ss[i]-l);
        else ans=max(ans,ss[i]-ss[i-1]-1);
        if(i==ss.size()-1)
            ans=max(ans,r-ss[i]);
    }
    if(ss.empty())
        ans=max(ans,r-l+1);
    printf("%I64d\n",ans);
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
long long  x,y,l,r,l1=0,l2=0,top,maxn=0;
long long  h[1000],m[1000],s[10000000];
int main()
{
    cin>>x>>y>>l>>r;
    h[0]=1;m[0]=1;

    for(int i=1;;i++)
    {    if(r/h[i-1]>=x)
         {h[i]=h[i-1]*x;l1=i;}
         else break;
    }

    for(int i=1;;i++)
    {    if(r/m[i-1]>=y)
         {m[i]=m[i-1]*y;l2=i;}
         else break;
    }
    for(int i=0;i<=l1;i++)
        for(int j=0;j<=l2;j++)
            if(h[i]+m[j]<=r&&h[i]+m[j]>=l)
        s[++top]=h[i]+m[j];
    sort(s+1,s+1+top);
    if(s[1]>l)
        maxn=s[1]-l;
    for(int i=2;i<=top;i++)
        {
            maxn=max(maxn,s[i]-s[i-1]-1);
        }
        if(top!=0)
        maxn=max(maxn,r-s[top]);
        else
            maxn=max(r-l+1,maxn);
    //   for(int i=1;i<=top;i++)
    //      cout<<s[i]<<endl;
    cout<<maxn<<endl;
    return 0;
}
/*2018-07-24 20:24:36.067*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
vector<ll> g, xp, yp;
ll x, y, l, r;

int main()
{
    cin >> x >> y >> l >> r;

    xp.push_back(1);
    yp.push_back(1);

    while (xp.back() <= 1e18 / x)
        xp.push_back(x * xp.back());

    while (yp.back() <= 1e18 / y)
        yp.push_back(y * yp.back());

    for (auto i : xp)
        for (auto j : yp)
            g.push_back(i + j);

    sort(g.begin(), g.end());
    g.erase(unique(g.begin(), g.end()), g.end());

    if (lower_bound(g.begin(), g.end(), l) == upper_bound(g.begin(), g.end(), r))
        return cout << r - l + 1 << endl, 0;

    ll mx = max(
        *lower_bound(g.begin(), g.end(), l) - l,
        r - *(upper_bound(g.begin(), g.end(), r) - 1));

    for (int i = lower_bound(g.begin(), g.end(), l) - g.begin() + 1;
         i < upper_bound(g.begin(), g.end(), r) - g.begin(); ++i)
        mx = max(g[i] - g[i - 1] - 1, mx);
    cout << mx << endl;
}

#include<bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0), cout.tie(0)

#define ll long long int
#define ld long double

#define fr first
#define se second

#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) (int)v.size()
#define sunq(v) sort(all(v));v.resize(distance(v.begin(),unique(all(v))))

#define pb push_back
#define mp make_pair

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define emla(a, val) memset(a, val, sizeof(a))
#define easm(a, b) (a + b - 1)/b

#define PI 3.141592653589793238
#define E 2.718281828459045235

ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a*b/gcd(a,b);}

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  //clock_t z = clock();
  ll x, y, l, r;
  cin >> x >> y >> l >> r;
  vector<ll>xx, yy;
  xx.pb(1);
  yy.pb(1);
  xx.pb(x);
  yy.pb(y);

  ll c = x, cc = y;
  while(c <= 1e18/x && x > 1)c *= x, xx.pb(c);
  while(cc <= 1e18/y && y > 1)cc *= y, yy.pb(cc);

  set<ll>s;
  for(auto i : xx)
    for(auto  j : yy)
      if(j + i >= l && j + i <= r)
        s.insert(i + j);
  bool fl, fr;
  fl = fr = 0;

  if(s.count(l))fl = 1;
  if(s.count(r))fr = 1;
  if(!fl)s.insert(l - 1);
  else s.insert(l);
  if(!fr)s.insert(r + 1);
  else s.insert(r);

  vector<ll>v, vv;
  for(auto i : s)v.pb(i);
  for(int i = 1; i < sz(v); i++)vv.pb(v[i] - v[i-1] - 1);

  ll ans = -1;
  ans = sz(vv) ? *max_element(all(vv)) : ans;
  cout << (ans >= 0 ? ans : 0) << endl;
  //debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
  /*
  *try tricky test cases:
    -bounds
    -integer overflow
    -small input (0, 1)
    -big input
    -random tests
  */
  return 0;
}

#include<bits/stdc++.h>
#define maxn 10000
using namespace std;
long long unl[maxn],l,r;
long long max(long long a, long long b)
{
	if (a>b) return a;
	else return b;
}
long long min(long long a, long long b)
{
	if (a<b) return a;
	else return b;
}
long long y2(long long n)
{
	if (n<=r) return n-1;
	else return r;
}
long long y1(long long n)
{
	if (n>=l) return n+1;
	else return l;
}
int main()
{
	long long x,y,mxx,mxy,tx,ty,tot=0,ans=0;
	scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
	tx=x; ty=y; mxx=1; mxy=1;
	if (x!=1)
	{
		while (r/tx>=x)
		{
			tx*=x; mxx++;
		}
	}
	if (y!=1)
	{
		while (r/ty>=y)
		{
			ty*=y; mxy++;
		}
	}
	tx=1;
	for (int i=0;i<=mxx;i++)
	{
		ty=1;
		for (int j=0;j<=mxy;j++)
		{
			if (tx+ty<=r)
			{
			tot++; unl[tot]=tx+ty;
			}
			else break;
			if (j!=mxy) ty=ty*y;
		}
		if (i!=mxx) tx*=x;
	}
	sort(unl+1,unl+tot+1);
	if (unl[1]>l) ans=max(ans,unl[1]-l);
	for (int i=1;i<tot;i++)
	{
		ans=max(ans,y2(unl[i+1])-y1(unl[i])+1);
	}
	if (unl[tot]<r) ans=max(ans,r-y1(unl[tot])+1);
	printf("%I64d\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll mod=1000000007;
ll MOD=998244353;
int inf=1000001000;
ll INF=5e18;

int main() {
    ll x,y,l,r;
    cin >> x >> y >> l >> r;
    vector<ll> a(0);
    ll c=1;
    while(c<=r){
        a.push_back(c);
        ld p=c,o=r,w=x;
        if (p>o/w) break;
        c*=x;
    }
    set<ll> s;
    s.insert(r+1);
    s.insert(l-1);
    rep(i,a.size()){
        ll p=1;
        ll e=a[i];
        while(p+e<=r){
            ld po=p,o=r,w=y,v=e;
            if (e+p<l) {
                if (po>(o-v)/w) break;
                p*=y;continue;}
            s.insert(e+p);
            if (po>(o-v)/w) break;
            p*=y;
        }
    }
    ll ans=0;
    for (auto it=s.begin();it!=s.end();++it){
        ll u=*it;
        it++;
        if (it==s.end()) break;
        ans=max(ans,*it-u-1);
        it--;
    }

    cout << ans << endl;
}

///////////////////////////////////////////////////////////
//   All rights reserved.
//   Author: wayen
///////////////////////////////////////////////////////////

#define _CRT_SECURE_NO_WARNINGS 1
#include <functional>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <streambuf>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <fstream>
#include <iomanip>
#include <istream>
#include <numeric>
#include <ostream>
#include <sstream>
#include <utility>
#include <cstdio>
#include <iosfwd>
#include <bitset>
#include <locale>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <ios>
#include <map>
#include <new>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pa;

LL _pow(LL x,LL y)
{
	if(y==0)
		return 1;
	LL ans = 1;
	while(y--)
	{
		ans = ans*x;
	}
	return ans;
}
set<LL>s;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	LL x,y,l,r;
	cin>>x>>y>>l>>r;
	LL x_max = 0,y_max = 0;
	while(1)
	{
		// cout<<r/pow(x,x_max)<<endl;
		if(r/_pow(x,x_max)<x)
			break;
		x_max++;
	}
	//cout<<x_max<<endl;
	//system("pause");
	while(1)
	{
		// cout<<r/pow(x,x_max)<<endl;
		if(r/_pow(y,y_max)<y)
			break;
		y_max++;
	}
	for(int i = 0;i<=x_max;i++)
	{
		for(int j = 0;j<=y_max;j++)
		{
			LL temp = _pow(x,i)+_pow(y,j);
			if(temp>=l&&temp<=r)
			{
				s.insert(temp);
			}
		}
	}
	set<LL>::iterator it;
	it = s.begin();
	LL ans = 0;
	if(it==s.end())
	{
		ans = r-l+1;
		cout<<ans<<endl;
		return 0;
	}
	LL temp = *it;
	ans = temp - l;
	//cout<<ans<<endl;
	int flag = 0;
	if(s.find(r)==s.end())
	{
		flag = 1;
	}
	s.insert(r);
	it++;
	for(;it != s.end(); it++)
	{
		LL emmm = (*it) - temp - 1;
		if((*it) == r)
			emmm+=flag;
		ans = max(ans,emmm);
		// cout<<(*it)<<"   "<<temp<<"   "<<ans<<endl;
		temp = (*it);
		// cout<<*it<<"     "<<ans<<endl;
	}
	cout<<ans<<endl;
	return 0;
}

//        /＼7　　　 ∠＿/
//        / │　　 ／　／
//        │　Z ＿, ＜　／　　 / `ヽ
//        │　　　　　ヽ　　 / 〉
//        Y　　　　　`　 /　　/
//        ｲ●　､　●　　⊂⊃〈　　 /
//        ()　 へ　　　　 | ＼〈
//          > ｰ ､_　 ィ　 │ ／／
//        	/ へ　　 / ﾉ＜ | ＼＼
//        	ヽ_ﾉ　　(_／　 │／／
//        		7 | ／
//        		＞―r￣￣`ｰ―＿

/*
    ____        __
   / __ \____  / /_  ____ _____ ___
  / /_/ / __ \/ __ \/ __ `/ __ `__ \
 / _, _/ /_/ / / / / /_/ / / / / / /
/_/ |_|\____/_/ /_/\__,_/_/ /_/ /_/
*/
#include<bits/stdc++.h>

typedef long long ll ;

#define pll pair<ll , ll >
#define X   first
#define Y   second
#define mp  make_pair
#define pii pair<int , int>
#define vec vector
#define vpii vector<pair<int , int>>
#define i_n   int i = 0 ; i< n ; i++
#define cn  int n ; cin>>n
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define cina int a[n] ; for(int i = 0 ; i < n ; i++) cin>>a[i]
#define pb push_back

using namespace std;
const int maxn = 1000*100+5 ;
const ll inf = 9223372036854775807 ;

ll x , y  , l , r  , px[80] , py[80] , ans ;
vec<ll> v ;

int main()
{
	migmig ;
	cin>>x>>y>>l>>r ;
	px[0] = 1 ;
	py[0] = 1 ;
	for(int i = 1 ; i <= 62 ; i ++ )
	{
		if(px[i-1] <= inf / x )
		px[i] = px[i-1] * x ;
		if(py[i-1] <= inf / y  )
		py[i] = py[i-1] * y ;
			if(i <= 4)
		{
		//	cout<<i << " : "<<px[i] << " " << py[i] << endl ;
		}
	}
	for(int i  = 0 ; i < 63 ; i ++ )
	{
		for(int j = 0 ; j < 63 ; j++  )
		{
			if(px[i] > 0 && py[j] > 0 && px[i] + py[j] > 0)
			{
				v.pb(px[i] + py[j]) ;
			}
		}
	}

	sort(v.begin() , v.end()) ;
	for(int i = 0 ; i< 10 ; i ++ )
	{
	//	cout<<v[i] << " " ;
	}
	cout<<endl ;
	for(int i = 0 ; i < v.size() - 1 ; i ++ )
	{
		//cout<<v[i] << " " ;
		if( v[i] >= l-1)
		{
			//cout<<v[i] << " " ;
			if(v[i+1] <= r+1)
			ans = max(ans  , v[i+1] - v[i] - 1 ) ;
			else
			ans = max(ans , r- v[i] ) ;

		//	cout<<v[i] - v[i-1] + 1 << " " ;
		}
	}
	if(v[v.size() - 1] < r)
	{
	    ans = max(ans , r - v[v.size() - 1]) ;
	}
	for(int i = 1 ; i < v.size() ; i ++  )
	{
		if(v[i] <= r+1 )
		{
			if(v[i-1] >= l -1 )
			{
				ans = max(ans  , v[i] - v[i - 1] - 1 ) ;
			}
			else
			{
				ans = max(ans , v[i] - l) ;
			}
		}
	}
	if(v[0] > l)
	{
	    ans = max(ans , v[0] - l) ;
	}
	bool b = true ;
	for(int i = 0 ; i < v.size() ; i ++ )
	{
		if(v[i] >= l && v[i] <= r)
		{
		//	cout<<v[i] << endl ;
			b = false ;
		}
	}
	//cout<<b << endl ;
	if(b) cout<< r - l + 1  ;
	else
	cout<<ans ;
}
//////
////////
///////////

#pragma GCC optimize(3)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define ll long long
vector<ll> powx;
vector<ll> powy;
vector<ll> ans;
int main()
{
    ll x, y, l, r;
    scanf("%lld%lld%lld%lld", &x, &y, &l, &r);
    ll tmp = 1;
    while (tmp <= 1e18/x)
    {
        powx.push_back(tmp);
        tmp *= x;
    }
    powx.push_back(tmp);
    tmp = 1;
    while (tmp <= 1e18/y)
    {
        powy.push_back(tmp);
        tmp *= y;
    }
    powy.push_back(tmp);
    for (int i = 0; i < (int)powx.size(); i++)
    {
        for (int j = 0; j < (int)powy.size(); j++)
        {
            tmp = powx[i] + powy[j];
            if (tmp >= l && tmp <= r)
            {
                ans.push_back(tmp);
            }
        }
    }
    sort(ans.begin(), ans.end());
    ll mx = 0;
    ll len = ans.size();
    if (len == 0) mx = max(mx, r-l+1);
    else if (len == r-l+1) mx = 0;
    else {
        mx = max(mx, ans[0]-l);
        mx = max(mx, r- ans[len-1]);
        for (int i = 0; i < (int)len -1; i++)
        {
            mx = max(mx, ans[i+1]-ans[i]-1);
        }
    }
    printf("%lld\n", mx);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long s[10000],ks=0;
bool cmp(long long a,long long b)
{
	return a<b;
}
int main()
{
	long long l,r,x,y;
	cin>>x>>y>>l>>r;
	long long sx=1,sy=1;
	for(int i=1;i<=64;i++)
	{
		sy=1;
		for(int j=1;j<=64;j++)
		{
			if(sy+sx<=r+10000)
			{
				s[ks++]=sy+sx;
			}
			if((double)sy*y>1e18)
				break;
			else
				sy=sy*y;
		}
		if((double)sx*x>1e18)
				break;
		else
			sx=sx*x;
	}
	s[ks++]=l;
	s[ks++]=r;
	sort(s,s+ks,cmp);
	long long ll,rr;
	for(int i=0;i<ks;i++)
	{
		if(s[i]==l)
		{
			ll=i;
			break;
		}
	}
	for(int i=ks-1;i>=0;i--)
	{
		if(s[i]==r)
		{
			rr=i;
			break;
		}
	}
	long long mm=0;
	if(rr-ll==1)
	{
		cout<<r-l+1<<endl;
		return 0;
	}
	//cout<<rr<<' '<<ll<<endl;
	for(int i=ll;i<=rr-1;i++)
	{
		//cout<<s[i]<<endl;
		if(i==ll||i==rr-1)
		{
			if(mm<=s[i+1]-s[i])
				mm=s[i+1]-s[i];
		}
		else if(mm<s[i+1]-s[i]-1)
			mm=s[i+1]-s[i]-1;
	}
	cout<<mm<<endl;
}

#include<bits/stdc++.h>
using namespace std;
vector<long long> v;
vector<long long> V;
long long fp( long long a, long long k)
{
    long long res=1;
    while(k)
    {
        if(k&1)res=res*a;
        a=a*a;
        k>>=1;
    }
    return res;
}
int main()
{
    long long x,y,l,r;
    while(~scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r))
    {
        int k1=(int)(log(r)*1.0/log(x));
        int k2=(int)(log(r)*1.0/log(y));
        v.clear();
        V.clear();
        for (int i=0; i<=k1; i++)
        {
            for (int j=0; j<=k2; j++)
            {
                V.push_back(fp(x,i)+fp(y,j));
            }
        }
        sort(V.begin(),V.end());
        v.push_back(0);
        for (int i=0; i<V.size(); i++)
        {
            if (V[i]<=r&&V[i]>=l)
            {
                v.push_back(V[i]);
            }
        }
        long long nmax=0;
        if (v.size()==1)
            nmax=r-l+1;
        else
        {
            /*for (int i=0; i<v.size(); i++)
                printf("%I64d\n",v[i]);*/

            for (int i=1; i<v.size()-1; i++)
            {
                if (nmax<v[i+1]-v[i]-1)
                {
                    nmax=v[i+1]-v[i]-1;
                }
            }
            if (r-v[v.size()-1]>nmax)
            {
                nmax=r-v[v.size()-1];
            }
            if (v[1]-l>nmax)
            {
                nmax=v[1]-l;
            }
        }
        printf("%I64d\n",nmax);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
#define fastio ios::sync_with_stdio(false);
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ln length()
#define M 1000000007

ll power(ll x, ll y)
{
	ll res = 1;
	while(y>0)
	{
		if(y&1)
			res = res*x;
		y = y>>1;
		x = x*x;
	}
	return res;
}
int main(int argc, char const *argv[])
{
	ll x,y,l,r;
	cin>>x>>y>>l>>r;
	ll a,b;
	a = (log(r)/log(x));
	b = (log(r)/log(y));
	vll v;
	rep(i,0,a)
	{
		rep(j,0,b)
		{
			ll sum = power(x,i)+power(y,j);
			if(sum>=l && sum<=r)
			{
				v.pb(sum);
			}
		}
	}
	v.pb(l-1);
	v.pb(r+1);
	ll diff=0;
	if(v.size()>0)
		sort(v.begin(),v.end());
	// rep(i,0,v.size()-1)
	// {
	// 	cout<<v.at(i)<<" ";
	// }
	if(v.size()>1)
	{
		rep(i,1,v.size()-1)
		{
			if(v.at(i)-v.at(i-1)-1 > diff)
				diff = v.at(i)-v.at(i-1)-1;
		}
	}
	cout<<diff<<"\n";
	return 0;
}

#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define mod 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define sz(a) ((int)a.size())

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

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

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<INT, INT> PII;

typedef pair<pii, int> ppi;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)

    #define lld "%I64d"

#else

    #define lld "%lld"

#endif

INT a[111], b[111];

INT arr[111111];

int main() {

#ifndef ONLINE_JUDGE

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

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

	int _time=clock();

#endif

	INT x, y, l, r;

	cin >> x >> y >> l >> r;

	int na=0, nb=0;

	INT xx=1;

	while(1) {

		a[na++]=xx;

		if(xx>floor((r+10)/x+eps)) break;

		xx*=x;

	}

	xx=1;

	while(1) {

		b[nb++]=xx;

		if(xx>floor((r+10)/y+eps)) break;

		xx*=y;

	}

	int nn=0;

	arr[nn++]=l-1, arr[nn++]=r+1;

	for(int i=0; i<na; i++) {

		for(int j=0; j<nb; j++) {

			arr[nn++]=a[i]+b[j];

		}

	}

	sort(arr, arr+nn);

	INT ans=0;

	for(int i=0; i<nn; i++) {

		if(arr[i]>r) break;

		if(arr[i]>=l-1) smax(ans, arr[i+1]-arr[i]-1);

	}

	cout << ans << endl;

#ifndef ONLINE_JUDGE

	cerr<<(int)(clock()-_time)<<endl;

#endif

	return 0;

}

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<bitset>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 10000005;
long long arr[maxn];

int main()
{
    long long x, y, l, r, i, j, ans, flag, f, s, k;
    scanf("%I64d %I64d %I64d %I64d",&x, &y, &l, &r);
//    printf("%I64d %I64d %I64d %I64d\n",x, y, l, r);
    flag = 0;
    ans = 0;
    for(i = 1; ; i = i*x)
    {
        for(j = 1; ; j = j*y)
        {
            if(i + j >= l && i + j <= r)
            {
                arr[flag++] = i + j;

            }
            k = r/j;
            if(k < y || y == 1)
            {
                break;
            }
        }
        k = r/i;
        if(k < x || x == 1)
        {
            break;
        }

    }
    if(flag == 0)
    {
        ans = r-l+1;
    }
    else
    {
        sort(arr, arr+flag);
        for(i = 1; i < flag; i++)
        {
            ans = max(ans, arr[i]-arr[i-1]-1);
        }
//        for(i = 0; i < flag; i++)
//        {
//            printf("%I64d  %I64d\n",i, arr[i]);
//        }
        f = arr[0] - l;
        s = r - arr[flag - 1];
        f = max(f, s);
        ans = max(ans, f);

    }
    printf("%I64d\n",ans);
    return 0;
}
/*2018-07-24 22:10:01.142*/

#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef unsigned long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;

ll x, y, l, r, ans;
vector<ll> v;

int main(){

	faster();

	cin >> x >> y;
	cin >> l >> r;

	for(ll q = 1; q <= r; q *= x){
		for(ll p = 1; p <= r; p *= y){
			if(l <= p + q && p + q <= r)
				v.pb(p + q);
			if(p > (r / y))
				break;
		}
		if(q > (r / x))
			break;
	}

	v.pb(l - 1);
	v.pb(r + 1);
	sort(all(v));

	for(ll i = 1; i < v.size(); ++i){
		ans = max(((v[i] - v[i - 1])), ans);
	}

	cout << ans - 1;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define inf (int) 1e9

typedef long long ll;
typedef long double ld;

const int N = 123456;
const int M = 1234567;
const int P = 400004;
const int A = 256;

ll ncr(int n, int r){

	if(n < r)
		return 0;

	if(n < 0 || r < 0)
		return 0;

	if(n == r)
		return 1;

	return ncr(n - 1, r - 1) + ncr(n - 1, r);
}

int main(int argc, char const *argv[])
{

	#ifdef Cyborg1o1
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	ll x, y, l, r;
	cin >> x >> y >> l >> r;

	vector<ll> px = {1}, py = {1};

	while(px.back() <= r / x){
		px.push_back(x * px.back());
	}

	while(py.back() <= r / y){
		py.push_back(y * py.back());
	}

	vector<ll> years;

	years.push_back(l - 1);
	years.push_back(r + 1);

	for(auto xx : px)
		for(auto yy : py)
			if(xx + yy >= l && xx + yy <= r)
				years.push_back(xx + yy);

	sort(all(years));

	// for(auto xx : years)
	// 	cout << xx << endl;

	ll mx = 0;

	for(int i = 1; i < years.size(); ++i){
		mx = max(mx, years[i] - years[i - 1] - 1);
	}

	cout << mx << endl;

	return 0;
}

#include <iostream>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <set>
#include <map>
using namespace std;
typedef long double ld;
typedef long long int ll;

struct Problem {
	void main() {
		while (read()) {
			for (auto e : solve()) {
				cout << e << ' ';
			}
			cout << endl;
		}
	}
	bool read() {
		if (!(cin >> x >> y >> l >> r))
			return false;

		return true;
	}
	vector<ll> solve() {
		set<ll> unhappy;

		vector<ll> x_pows = { 1 }, y_pows = { 1 };
		ll tx = 1, ty = 1;
		while (tx <= 1e18 / x) {
			tx *= x;
			x_pows.push_back(tx);
		}
		while (ty <= 1e18 / y) {
			ty *= y;
			y_pows.push_back(ty);
		}
		for (auto e : x_pows) for (auto q : y_pows) if (e + q <= r && e + q >= l) {
			unhappy.insert(e + q);
		}
		if (l == r && !unhappy.count(l)) return { 1 };
		if (l == r && unhappy.count(l)) return { 0 };
		if (unhappy.empty()) return {r - l + 1};
		ll beg = l, ans = 0;
		for (auto e : unhappy) {
			ans = (e == beg ? ans : max(e - beg, ans));
			beg = e + 1;
		}
		if (unhappy.size()) {
			ll end = *unhappy.rbegin();
			ans = (r == end ? ans : max(ans, r - end));
		}
		return { ans };
	}
	ll x, y, l, r;
};

int main() {
	ios::sync_with_stdio(false);
	Problem pr;
	pr.main();
	return 0;
}

#include<bits/stdc++.h>
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define inf 0x3f3f3f3f
const int N=1e5+10;
const ll M=1e18+5;
using namespace std;
const int mod = 1e9+7;
ll n,m,x,y,l,r;
ll a[N],b[N],c[N],d[N];
ll quickpow(ll a,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans*=a;
        a*=a;
        n>>=1;
    }
    return ans;
}
int main()
{
    cin>>x>>y>>l>>r;
    int mx,my;/*
    for(int i=0;i<=60;i++)
    {
      a[i]=quickpow(x,i);
      if(a[i]>=M||a[i]>r) {mx=i;break;}
    }
    for(int i=0;i<=60;i++)
    {
      b[i]=quickpow(y,i);
      if(b[i]>=M||b[i]>r) {my=i;break;}
    }*/
    a[0]=b[0]=1;
    mx=my=1;
    while(a[mx-1]<=r/x)
    {
        a[mx]=a[mx-1]*x;
        mx++;
    }
      while(b[my-1]<=r/y)
    {
        b[my]=b[my-1]*y;
        my++;
    }
    int id=-1;
    for(int i=0;i<mx;i++)
    {
        for(int j=0;j<my;j++)
        {
           if( a[i]+b[j]>=l&&a[i]+b[j]<=r)
           c[++id]=a[i]+b[j];
        }
    }
   // 1000000000000000000
    sort(c,c+id+1);

/*    for(int i=0;i<=id;i++)
    {

        c[++now]=d[i];
    }*/

 /*  for(int i=0;i<=id;i++)
        cout<<"** " <<c[i]<<endl;*/

    ll  ed,ans;
    ed=ans=0;
    /*for(int i=0;i<=id;i++)
    {
       if(c[i]>=l&&c[i]<=r)
       {
          if(c[i-1]>=l)
          ans=max(c[i]-c[i-1]-1,ans);
          else
          ans=max(c[i]-l,ans);
        //  cout<<ans<<" "<<i<<endl;
       }
       if(c[i]>r)
       {
           ed=i-1;
           break;
       }
    }
    if(c[0]>r) ans=max(ans,r-l+1);
    else if(c[ed]>=l)
    ans=max(ans,r-c[ed]);
    else if(c[ed]<l)
    ans=max(ans,r-l+1);
    cout<<ans<<endl;*/
    if(id>=0)
       {
           ans=max(c[0]-l,r-c[id]);
       //  cout<<c[0] <<endl;
       }
    else ans=r-l+1;
    for(int i=0;i<=id;i++)
    {
        ans=max(ans,c[i]-l-1);
        l=c[i];
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
ll x,y,l,r;
using namespace std;
set<ll>st;
set<ll>::iterator it;
int main()
{
	cin>>x>>y>>l>>r;
	ll t1=0,t2=0;
	ll s=1,s1=1,s2=1;
	while(1)
	{
		s+=s2-s2/y;
		if(s>=l&&s<=r)
		st.insert(s);
		if(r/s2<y)
		{
			if(r/s1<x)
			break;
			s1*=x;
			s=s1;
			s2=1;
			continue;
		}
		else
		{
			s2*=y;
		}
	}
	if(st.empty())
	{
		cout<<r-l+1<<endl;
		return 0;
	}
	ll maxl=0;
	ll pre=l-1;
	for(it=st.begin();it!=st.end();it++)
	{
		if(*it==l)
		maxl=0;
		else
		maxl=max(maxl,*it-pre-1);
		pre=*it;
	}
	it--;
	maxl=max(maxl,r-*it);
	cout<<maxl;
}

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2*acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;
const int N = 100010;
const ll last = 2ll * 1e18;

inline int in() {int x; scanf("%d",&x); return x;}
inline ll lin() {ll x; scanf("%I64d",&x); return x;}

vector < ll > X, Y, XY;

ll x, y, l, r;

int main(){
    x = lin(); y = lin(); l = lin(); r = lin();
    for(ll hand = 1ll;    ; hand *= x){
        X.pb(hand);
        if((last / hand) < x) break;
    }
    for(ll hand = 1ll;    ; hand *= y){
        Y.pb(hand);
        if((last / hand) < y) break;
    }
    for(int i = 0; i < X.size(); i++){
        for(int j = 0; j < Y.size(); j++){
            XY.pb(X[i] + Y[j]);
        }
    }
    XY.pb(l - 1ll); XY.pb(r + 1ll);
    sort(XY.begin(), XY.end());
    int st, ed;
    for(int i = 0; i < XY.size(); i++){
        if(XY[i] == l - 1ll){
            st = i;
            break;
        }
    }
    for(int i = XY.size() - 1; i >= 0; i--){
        if(XY[i] == r + 1ll){
            ed = i;
            break;
        }
    }
    ll ans = 0ll;
    for(int i = st; i < ed; i++){
        ans = max(ans, XY[i + 1] - XY[i] - 1);
    }
    printf("%I64d\n", ans);
}

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
#define siz 510105
typedef long long LL;
using namespace std;
const LL inf =  9223372036854775807;
LL x,y,L,R;
LL a[siz];
LL dx[siz],dy[siz];
void solve()
{
    LL ans = 0,cnt1=1,cnt2=1,Px=1,Py=1,len=0;
    while(Px<=1e18/x) Px*=x,cnt1++;
    while(Py<=1e18/y) Py*=y,cnt2++;
    Px = 1,Py = 1;
    for(int i=0;i<cnt1;i++)
    {
        Py = 1;
        for(int j=0;j<cnt2;j++)
        {
            LL cot = Px + Py;
            if(cot>R) break;
            if(cot<=1e18&&cot<=R&&cot>=L) a[++len] = cot;
            Py*=y;
        }
        Px*=x;
    }
    sort(a+1,a+len+1);
    if(!len) ans = R-L+1;
    else ans = max(a[1]-L,R-a[len]);
    for(int i=1;i<len;i++) ans = max(ans,a[i+1]-a[i]-1);
    cout<<ans<<endl;
}
int main()
{
    while(~scanf("%I64d %I64d %I64d %I64d",&x,&y,&L,&R))
    {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;

int main() {
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
    ll x,y,l,r;
    cin >> x >> y >> l >> r;
    int sx = 1,sy = 1;
    ll cur = x;
    while (cur < r && cur * x  > 0) {
        sx++;
        cur*=x;
    }
    if (cur > r) sx--;
    cur = y;
    while (cur < r && cur * y  > 0) {
        sy++;
        cur*=y;
    }
    if (cur > r) sy--;
    vector <ll> v;
    ll xx = 1, yy = 1;
    for (int i=0; i<=sx; i++) {
        for (int j=0; j<=sy; j++) {
            if (xx + yy >= l && xx + yy <= r) {
                v.pb(xx + yy);
            }
            if (yy <= 0) break;
            if (yy > r/y) break;
            yy*=y;
        }
        if (xx <= 0) break;
        if (xx > r/x) break;
        xx*=x;
        yy = 1;
    }
    if (v.size()){
    sort(v.begin(),v.end());
    ll ans = v[0] - l;
    ans = max(ans, r-v[v.size()-1]);
    for (int i=1; i<v.size(); i++) {
        ans = max(ans, v[i] - v[i-1]-1);
    }
    ll kek = 0;
    cout << max(ans,kek) << endl;
    } else {
      cout << r - l + 1 << endl;
    }
}

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#define N 5000005
#define mod 1000000007
#define mem(a,x) memset(a,x,sizeof(a))
#define ll long long
#define exp 1e-8
#define lowbit(x) (x&-x)
#define rep(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
ll x ,y ,l ,r;
vector <ll> v;
int main( ){
    ll tx=1 ,ty=1 ,inf = 1000000000000000000;
    cin>>x >>y >>l >>r;
    ll sqx = (inf)/x+2;
    ll sqy = (inf)/y+2;
    rep (i ,0 ,62){
        ty = 1;
        if(i)tx*=x;
        if( tx>r )break;
        rep( j,0  ,62 ){
        if(j)ty*=y;
        //cout<<tx+ty<<endl;
        if( tx+ty>r )break;
        if(tx+ty>=l && tx+ty<=r)v.push_back( ty+tx );
        if( ty>sqy)break;
        }
        if( tx>sqx)break;
    }
    v.push_back(l-1);
    v.push_back(r+1);
    ll ans = 0;
    sort( v.begin() ,v.end() );
    int sz = v.size( );
    rep( i ,1 ,sz-1 ){
        //cout<<v[i]<<endl;
    if( v[i]!=v[i-1])ans = max(ans ,v[i]-v[i-1]-1 );
    }

    cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;

class bigint {
	static const int base = 1000000000;
	static const int base_digits = 9;
	vector<int> a;
	int sign;
public:
	bigint() : sign(1) {}
	bigint(int x) {
		*this = x;
	}
	bigint(long long x) {
		*this = x;
	}
	bigint(const string &s) {
		*this = s;
	}
	bigint& operator = (int x) {
		return *this = (long long)x;
	}
	bigint& operator = (long long x) {
		sign = 1;
		a.clear();
		if (x < 0) sign = -1, x = -x;
		for (; x > 0; x /= base) {
			a.push_back(x % base);
		}
		return *this;
	}
	void trim() {
		while (!a.empty() && !a.back()) a.pop_back();
		if (a.empty()) sign = 1;
	}
	bigint& operator = (const string &s) {
		sign = 1;
		a.clear();
		int p;
		for (p = 0; p < (int)s.length() && (s[p] == '-' || s[p] == '+'); p++) {
			if (s[p] == '-') sign = -sign;
		}
		for (int i = s.length() - 1; i >= p; i -= base_digits) {
			int x = 0;
			for (int j = max(p, i - base_digits + 1); j <= i; j++) {
				x = x * 10 + s[j] - '0';
			}
			a.push_back(x);
		}
		trim();
		return *this;
	}
	bigint& operator = (const bigint &b) {
		sign = b.sign;
		a = b.a;
		return *this;
	}
	bigint operator - () const {
		bigint res = *this;
		res.sign = -sign;
		return res;
	}
	bool operator < (int x) const {
		bigint b = x;
		return *this < b;
	}
	bool operator < (long long x) const {
		bigint b = x;
		return *this < b;
	}
	bool operator < (const bigint &b) const {
		if (sign != b.sign) return sign < b.sign;
		if (a.size() != b.a.size()) return a.size() * sign < b.a.size() * b.sign;
		for (int i = a.size() - 1; i >= 0; i--) {
			if (a[i] != b.a[i]) {
				return a[i] * sign < b.a[i] * sign;
			}
		}
		return false;
	}
	bool operator > (const bigint &b) const {
		return b < *this;
	}
	bool operator <= (const bigint &b) const {
		return !(b < *this);
	}
	bool operator >= (const bigint &b) const {
		return !(*this < b);
	}
	bool operator == (const bigint &b) const {
		return !(*this < b) && !(b < *this);
	}
	bool operator != (const bigint &b) const {
		return *this < b || b < *this;
	}
	bigint operator + (int x) const {
		bigint b = x;
		return *this + b;
	}
	bigint operator + (long long x) const {
		bigint b = x;
		return *this + b;
	}
	bigint operator + (const bigint &b) const {
		if (sign == b.sign) {
			bigint res = b;
			int sz = max(a.size(), b.a.size());
			for (int i = 0, carry = 0; i < sz || carry; i++) {
				if (i == (int)res.a.size()) res.a.push_back(0);
				res.a[i] += carry + (i < (int)a.size() ? a[i] : 0);
				carry = res.a[i] >= base;
				if (carry) res.a[i] -= base;
			}
			res.trim();
			return res;
		}
		return *this - (-b);
	}
	bigint& operator += (int x) {
		return *this = *this + x;
	}
	bigint& operator += (long long x) {
		return *this = *this + x;
	}
	bigint& operator += (const bigint &b) {
		return *this = *this + b;
	}
	bigint operator - (int x) const {
		bigint b = x;
		return *this - b;
	}
	bigint operator - (long long x) const {
		bigint b = x;
		return *this - b;
	}
	bigint operator - (const bigint &b) const {
		if (sign == b.sign) {
			if (abs(*this) >= abs(b)) {
				bigint res = *this;
				for (int i = 0, carry = 0; i < (int)b.a.size() || carry; i++) {
					res.a[i] -= carry + (i < (int)b.a.size() ? b.a[i] : 0);
					carry = res.a[i] < 0;
					if (carry) res.a[i] += base;
				}
				res.trim();
				return res;
			}
			return -(b - *this);
		}
		return *this + (-b);
	}
	bigint& operator -= (int x) {
		return *this = *this - x;
	}
	bigint& operator -= (long long x) {
		return *this = *this - x;
	}
	bigint& operator -= (const bigint &b) {
		return *this = *this - b;
	}
	bigint operator * (int x) const {
		return *this * (long long)x;
	}
	bigint operator * (long long x) const {
		bigint res = *this;
		if (x < 0) res.sign = - res.sign, x = -x;
		for (int i = 0, carry = 0; i < (int)a.size() || carry; i++) {
			if (i == (int)a.size()) res.a.push_back(0);
			long long cur = res.a[i] * x + carry;
			carry = (int)(cur / base);
			res.a[i] = (int)(cur % base);
		}
		res.trim();
		return res;
	}
	static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
		vector<long long> p(max(old_digits, new_digits) + 1);
		p[0] = 1;
		for (int i = 1; i < (int)p.size(); i++) {
			p[i] = p[i - 1] * 10;
		}
		vector<int> res;
		long long cur = 0;
		int cur_digits = 0;
		for (int i = 0; i < (int)a.size(); i++) {
			cur += a[i] * p[cur_digits];
			cur_digits += old_digits;
			while (cur_digits >= new_digits) {
				res.push_back((int)(cur % p[new_digits]));
				cur /= p[new_digits];
				cur_digits -= new_digits;
			}
		}
		res.push_back((int)cur);
		while (!res.empty() && !res.back()) res.pop_back();
		return res;
	}
	static vector<long long> karatsuba_multiply(const vector<long long> &a, const vector<long long> &b) {
		int n = a.size();
		vector<long long> res(n + n);
		if (n <= 32) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					res[i + j] += a[i] * b[j];
				}
			}
			return res;
		}
		int k = n >> 1;
		vector<long long> a1(a.begin(), a.begin() + k);
		vector<long long> a2(a.begin() + k, a.end());
		vector<long long> b1(b.begin(), b.begin() + k);
		vector<long long> b2(b.begin() + k, b.end());
		vector<long long> a1b1 = karatsuba_multiply(a1, b1);
		vector<long long> a2b2 = karatsuba_multiply(a2, b2);
		for (int i = 0; i < k; i++) {
			a2[i] += a1[i];
			b2[i] += b1[i];
		}
		vector<long long> r = karatsuba_multiply(a2, b2);
		for (int i = 0; i < (int)a1b1.size(); i++) {
			r[i] -= a1b1[i];
		}
		for (int i = 0; i < (int)a2b2.size(); i++) {
			r[i] -= a2b2[i];
		}
		for (int i = 0; i < (int)r.size(); i++) {
			res[i + k] += r[i];
		}
		for (int i = 0; i < (int)a1b1.size(); i++) {
			res[i] += a1b1[i];
		}
		for (int i = 0; i < (int)a2b2.size(); i++) {
			res[i + n] += a2b2[i];
		}
		return res;
	}
	static bigint karatsuba(const bigint &a1, const bigint &b1) {
		if (a1.size() <= 8 && b1.size() <= 8) {
			long long x = a1.to_longint(), y = b1.to_longint();
			bigint res = x * y;
			return res;
		}
		int sizea = a1.size(), sizeb = b1.size();
		int m = max(sizea, sizeb) / 2;
		string as = a1.to_string(), bs = b1.to_string();
		bigint a = as.substr(0, sizea - m);
		bigint b = as.substr(sizea - m);
		bigint c = bs.substr(0, sizeb - m);
		bigint d = bs.substr(sizeb - m);
		bigint z2 = karatsuba(a, c);
		bigint z0 = karatsuba(b, d);
		bigint z1 = karatsuba(a + b, c + d) - z0 - z2;
		string z2s = z2.to_string() + string(m * 2, '0');
		z2 = z2s;
		string z1s = z1.to_string() + string(m, '0');
		z1 = z1s;
		return z2 + z1 + z0;
	}
	bigint operator * (const bigint &x) const {
		vector<int> a6 = convert_base(a, base_digits, 6);
		vector<int> b6 = convert_base(x.a, base_digits, 6);
		vector<long long> a(a6.begin(), a6.end());
		vector<long long> b(b6.begin(), b6.end());
		while (a.size() < b.size()) a.push_back(0);
		while (a.size() > b.size()) b.push_back(0);
		while (a.size() & (a.size() - 1)) {
			a.push_back(0);
			b.push_back(0);
		}
		vector<long long> c = karatsuba_multiply(a, b);
		bigint res;
		for (int i = 0, carry = 0; i < (int)c.size(); i++) {
			long long cur = c[i] + carry;
			res.a.push_back((int)(cur % 1000000));
			carry = (int)(cur / 1000000);
		}
		res.a = convert_base(res.a, 6, base_digits);
		// bigint res = karatsuba(*this, x);
		res.sign = sign * x.sign;
		res.trim();
		return res;
	}
	bigint& operator *= (int x) {
		return *this = *this * x;
	}
	bigint& operator *= (long long x) {
		return *this = *this * x;
	}
	bigint& operator *= (const bigint &b) {
		return *this = *this * b;
	}
	friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) {
		int norm = base / (b1.a.back() + 1);
		bigint a = abs(a1) * norm;
		bigint b = abs(b1) * norm;
		bigint q, r;
		q.a.resize(a.a.size());
		for (int i = a.a.size() - 1; i >= 0; i--) {
			r *= base;
			r += a.a[i];
			int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
			int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
			int d = ((long long)base * s1 + s2) / b.a.back();
			r -= b * d;
			while (r < 0) r += b, d--;
			q.a[i] = d;
		}
		q.sign = a1.sign * b1.sign;
		r.sign = a1.sign;
		q.trim();
		r.trim();
		return make_pair(q, r / norm);
	}
	bigint operator / (int x) const {
		bigint res = *this;
		if (x < 0) res.sign = - res.sign, x = -x;
		for (int i = (int)a.size() - 1, rem = 0; i >= 0; i--) {
			long long cur = res.a[i] + rem * (long long)base;
			res.a[i] = (int)(cur / x);
			rem = (int)(cur % x);
		}
		res.trim();
		return res;
	}
	bigint operator / (const bigint &b) const {
		return divmod(*this, b).first;
	}
	bigint& operator /= (const bigint &b) {
		return *this = *this / b;
	}
	int operator % (int x) {
		if (x < 0) x = -x;
		int m = 0;
		for (int i = a.size() - 1; i >= 0; i--) {
			m = (a[i] + m * (long long)base) % x;
		}
		return m * sign;
	}
	bigint operator % (const bigint &b) const {
		return divmod(*this, b).second;
	}
	int operator %= (int x) {
		return *this % x;
	}
	bigint& operator %= (const bigint &b) {
		return *this = *this % b;
	}
	bool is_zero() const {
		return a.empty() || (a.size() == 1 && !a.back());
	}
	int size() const {
		if (a.empty()) return 0;
		int res = (a.size() - 1) * base_digits;
		for (int x = a.back(); x; x /= 10) res++;
		return res;
	}
	long long to_longint() const {
		long long res = 0;
		for (int i = a.size() - 1; i >= 0; i--) {
			res = res * base + a[i];
		}
		return res * sign;
	}
	string to_string() const {
		stringstream ss;
		ss << *this;
		string s;
		ss >> s;
		return s;
	}
	friend bigint abs(const bigint &b) {
		bigint res = b;
		res.sign *= res.sign;
		return res;
	}
	friend bigint gcd(const bigint &a, const bigint &b) {
		return b.is_zero() ? a : gcd(b, a % b);
	}
	friend bigint sqrt(const bigint &a) {
		bigint g = string("1" + string(a.size() / 2, '0'));
		bigint gg = g * g;
		while (gg > a || a >= (gg + g * 2 + 1)) {
			g = (g + a / g) / 2;
			gg = g * g;
		}
		return g;
	}
	friend bigint pow(bigint a, int n) {
		return pow(a, (long long)n);
	}
	friend bigint pow(bigint a, long long n) {
		bigint res = 1;
		while (n > 0) {
			if (n & 1) res *= a;
			a *= a, n >>= 1;
		}
		return res;
	}
	friend bigint pow(bigint a, bigint &n) {
		bigint res = 1;
		while (!n.is_zero()) {
			if (n % 2) res *= a;
			a *= a, n /= 2;
		}
		return res;
	}
	friend istream& operator >> (istream &in, bigint &b) {
		string s;
		in >> s;
		b = s;
		return in;
	}
	friend ostream& operator << (ostream &out, const bigint &b) {
		if (b.sign == -1) out << '-';
		out << (b.a.empty() ? 0 : b.a.back());
		for (int i = b.a.size() - 2; i >= 0; i--) {
			out << setfill('0') << setw(base_digits) << b.a[i];
		}
		return out;
	}
};

int main() {
#ifdef LOCAL_DEFINE
	//freopen("data.in", "rt", stdin);
	//freopen("data.out", "w", stdout);
	auto _start = chrono::high_resolution_clock::now();
#endif

	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout << fixed << setprecision(15);
	bigint x, y;
	long long l, r;
	cin >> x >> y >> l >> r;
	vector<long long> year;
	for (int a = 0; a < 64; a++) {
		bigint px = pow(x, a);
		if (px.size() >= 20 || px.to_longint() > r) break;
		for (int b = 0; b < 64; b++) {
			bigint py = pow(y, b);
			if (py.size() >= 20 || py.to_longint() > r) break;
			bigint sum = px + py;
			if (sum.size() >= 20) break;
			long long tmp = sum.to_longint();
			if (l <= tmp && tmp <= r) {
				year.push_back(tmp);
			}
		}
	}
	if (year.empty()) {
		cout << r - l + 1 << '\n';
	} else {
		long long ans = 0;
		sort(year.begin(), year.end());
		year.resize(unique(year.begin(), year.end()) - year.begin());
		ans = max(ans, year.front() - l);
		for (int i = 1; i < (int)year.size(); i++) {
			ans = max(ans, year[i] - year[i - 1] - 1);
		}
		ans = max(ans, r - year.back());
		cout << ans << '\n';
	}

#ifdef LOCAL_DEFINE
	auto _end = chrono::high_resolution_clock::now();
	cerr << "elapsed time: " << chrono::duration<double, milli>(_end - _start).count() << " ms\n";
#endif
	return 0;
}

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline ll read()
{
	ll x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-')f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
ll a[65],b[65],A[4000];
int ca,cb;
int main()
{
	ll x=read(),y=read(),l=read(),r=read();
	ca=cb=0;
	a[ca]=b[cb]=1;
	if(x!=1)
		while(a[ca]<=r/x) a[ca+1]=a[ca]*x,++ca;
	if(y!=1)
		while(b[cb]<=r/y) b[cb+1]=b[cb]*y,++cb;
	int tot=0;
	for(int i=0;i<=ca;i++)
		for(int j=0;j<=cb;j++)
			A[++tot]=a[i]+b[j];
	A[++tot]=1000000000000000005LL;
	sort(A+1,A+tot+1);
	tot=unique(A+1,A+tot+1)-(A+1);
	int t=lower_bound(A+1,A+tot+1,l)-A;
	ll ans=A[t]-l;
	if(A[t]>r) ans=r-l+1;
	else
		for(int i=t;;i++)
		{
			if(A[i+1]>r) {ans=max(ans,r-A[i]);break;}
			else ans=max(ans,A[i+1]-A[i]-1);
		}
	printf("%I64d\n",ans);
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main(){
	long long x, y, l, r;
	cin >> x >> y >> l >> r;

	priority_queue<long long> Q;
	long long tx = 1, ty = 1;
	for(int a = 0; a < 64; a++){
		if(a != 0 && tx > r / x) break;
		if(a == 0) tx = 1;
		else tx *= x;
		ty = 1;
		for(int b = 0; b < 64; b++){
			if(b != 0 && ty > (r - tx) / y) break;
			if(b == 0) ty = 1;
			else ty *= y;
			if(tx + ty >= l && tx + ty <= r){
				//cout << tx + ty << endl;
				Q.push(tx + ty);
			}
		}
	}

	long long pre = r;
	if(Q.empty() || (!Q.empty() && Q.top() != r)) pre = r + 1;
	long long pos = 0;
	long long ans = 0;
	while(!Q.empty()){
		pos = Q.top(); //cout << pos << endl;
		Q.pop();
		//cout << pre << pos << endl;
		ans = max(ans, pre - pos - 1);
		pre = pos;
	}
	if(pre != l) ans = max( ans, pre - l);
	//else ans = max(ans, pre - l - 1);

	cout << ans << endl;
}

#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll l, r, ans, x, y, a, b;
set <ll> S;
set <ll> :: iterator ii;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);
    cin >> x >> y >> l >> r;
    a = 1;
    while (a <= r)
    {
        b = 1;
        while (b <= r)
        {
            S.insert(a+b);
            if (b > r/y) break;
            b *= y;
        }
        if (a > r/x) break;
        a *= x;
    }
    x = l-1;
    for (ii = S.lower_bound(l); ii != S.end() && *ii <= r; ii++) ans = max(ans, *ii-x-1), x = *ii;
    cout << max(ans, r-x);
    return 0;
}

/*input
2 2 1 1000000000000000000
*/
/*
       _==/          i     i          \==_
     /XX/            |\___/|            \XX\
   /XXXX\            |XXXXX|            /XXXX\
  |XXXXXX\_         _XXXXXXX_         _/XXXXXX|
 XXXXXXXXXXXxxxxxxxXXXXXXXXXXXxxxxxxxXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 XXXXXX/^^^^"\XXXXXXXXXXXXXXXXXXXXX/^^^^^\XXXXXX
  |XXX|       \XXX/^^\XXXXX/^^\XXX/       |XXX|
    \XX\       \X/    \XXX/    \X/       /XX/
       "\       "      \X/      "      /"

*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
//loops
#define f(i,s,n) for(ll i=(ll)s;i<(ll)n;i++)
#define rf(i,n,s) for(ll i=(ll)(n-1);i>=(ll)s;i--)
//reset
#define ms0(X) memset((X), 0, sizeof((X)))
#define ms1(X) memset((X), -1, sizeof((X)))
//IO
#define bio ios_base::sync_with_stdio;cin.tie(NULL)
//STL
#define pb push_back
#define pii pair<ll,ll>
#define vll vector<ll>
#define vpii vector<pii >
#define mll map<ll, ll>
#define mpii map<pii,ll>
#define msll map<string, ll>
#define sortv(v) sort(v.begin(),v.end())
#define F first
#define S second
//standard values
#define mod (ll)(1e9+7)
#define INF (ll)1e16
#define MAXN (ll)(1e5+5)
//comparator
bool cmp(pii a,pii b){
  if(a.F==b.F) return a.S<b.S;
  else return a.F<b.F;
}
ll exp(ll a,ll b){
  ll ans=1;
  while(b!=0){
    if(b%2==1)
      ans=ans*a;
    a=a*a;
    b/=2;
  }
  return ans;
}
/********************************************************************************************************/
vll v;
vll ::iterator lo1,lo2;
mll mp;
int main()
{
  ll cnt1 = 0, cnt2 = 0;
  ll x, y, l, r;
  scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
  ll pre = 1;
  ll final = exp(10,18);
  while(1){
    if (pre <= final/x){
      cnt1++;
      pre = pre*x;
    }
    else
      break;
  }
  pre = 1;
  //ll final = exp(10LL,18);
  while(1){
    if (pre <= final/y){
      cnt2++;
      pre = pre * y;
    }
    else
      break;
  }
  f(i,0,cnt1+1){
    f(j,0,cnt2+1){
      ll a1 = exp(x,i);
      ll a2 = exp(y,j);
      if(a1>0 && a2>0 && a1+a2 > 0 && a1+a2<=final){
        ll a = a1+a2;
        if(mp[a]==0){
          v.pb(a);
          mp[a]=1;
        }
      }
      else{
        break;
      }
    }
  }
  if(v.size()==0){
    printf("%lld\n",r-l+1);
    return 0;
  }
  sortv(v);
  ll lti=-1, rti=-1;
  f(i,0,v.size()){
    if(v[i]>=l&&lti==-1){
      lti=i;
    }
    if(v[i]<=r){
      rti=i;
    }
  }
  ll ans = 0;
  if(lti>rti){
    ans = max(ans, r-l+1);
    printf("%lld\n",ans);
    return 0;
  }
  if(lti == -1 && rti != -1){
    printf("%lld\n",r-l+1);
    return 0;
  }
  ans = max(ans, v[lti]-l);
  ans = max(ans, r-v[rti]);
  f(i,lti+1,rti+1){
    ans = max(ans, v[i]-v[i-1]-1);
  }
  printf("%lld\n",ans);
  return 0;
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
long long x,y,l,r;
long long a[100],b[100];
vector<long long> v;

int main()
{
	cin>>x>>y>>l>>r;
	int i,j,la,lb;
	a[0]=(long long)1;
	for (i=1;i<=100;i++)
	{
		if (r/a[i-1]<x)
		{
			la=i;
			break;
		}
		a[i]=a[i-1]*x;
	}
	b[0]=(long long)1;
	for (i=1;i<=100;i++)
	{
		if (r/b[i-1]<y)
		{
			lb=i;
			break;
		}
		b[i]=b[i-1]*y;
	}
	for (i=0;i<la;i++)
		for (j=0;j<lb;j++)
		{
			v.push_back(a[i]+b[j]);
		}
	v.push_back(l-1);
	v.push_back(r+1);
	sort(v.begin(),v.end());
	long long ans=0;
	for (i=0;i<v.size()-1;i++)
	{
		if (v[i]>=l-1 && v[i+1]<=r+1)
			ans=max(ans,v[i+1]-v[i]-1);
	}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define put(x) cout<<#x<<" = "<<x<<endl;
#define X first
#define Y second
#define push push_back
#define pop pop_back
#define make make_pair
using namespace std;
typedef long long num;
typedef pair<int,int> point;
const num maxn=1e5+10,overflow=1e18;

num x,y,l,r,A[maxn],t=1,ans;

int main()
{
    cin>>x>>y>>l>>r;
    num x0=1,y0;
    ans=r-l+1;
    A[0]=0;
    while(1)
	{
	    y0=1;
	    while(1)
		{
		    if(x0+y0>=l && x0+y0<=r) ans=0;
		    A[t++]=x0+y0;
		    if(y>overflow/y0) break;
		    y0*=y;
		}
	    if(x>overflow/x0) break;
	    x0*=x;
	}
    A[t++]=overflow+1;
    sort(A,A+t);
    rep(i,0,t-1)
	{
	    if(A[i]>=l)
		{
		    ans=max(ans, min(A[i+1],r+1)-A[i]-1);
		}
	    if(A[i+1]<=r)
		{
		    ans=max(ans, A[i+1]-max(A[i],l-1)-1);
		}
	}
    cout<<ans<<endl;
}

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
long long x,y,l,r,tmp,a[1000000],b[1000000],ans[1000000];
int n=-1,m=-1,cnt;
int cmp(long long a,long long b)
{
	return a<b;
}
int main()
{
	cin>>x>>y>>l>>r;
	tmp=1;
	while(tmp<=r)
	{
		a[++n]=tmp;
		if(r/tmp<x)break;
		tmp*=x;
	}
	tmp=1;
	while(tmp<=r)
	{
		b[++m]=tmp;
		if(r/tmp<y)break;
		tmp*=y;
	}
//	for(int i=0;i<=n;i++)cout<<"i="<<i<<"  pow="<<a[i]<<endl;cout<<endl;
//	for(int i=0;i<=m;i++)cout<<"i="<<i<<"  pow="<<b[i]<<endl;cout<<endl;
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=m;j++)
		{
			if(l<=a[i]+b[j]&&a[i]+b[j]<=r)
			{
				ans[++cnt]=a[i]+b[j];
			}
		}
	}
	sort(ans+1,ans+1+cnt,cmp);//small -> large
//	cout<<ans[1]<<endl;
//	for(int i=1;i<=cnt;i++)cout<<ans[i]<<endl;

	long long maxn=0;

	ans[0]=l-1;
	ans[cnt+1]=r+1;

	for(int i=0;i<=cnt;i++)
	{
//		printf("%d %d\n",ans[i+1],ans[i]
		if(ans[i+1]!=ans[i])
		{
//			cout<<"compare="<<(ans[i+1]!=ans[i])<<endl;
//			cout<<"front="<<ans[i+1]<<"  tail="<<ans[i]<<"  ff="<<ans[i+1]-ans[i]-1<<endl;
			maxn=max(maxn,ans[i+1]-ans[i]-1);
		}
	}
	cout<<maxn<<endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define FILE_IN freopen("kotlin.in", "r", stdin);
#define FILE_OUT freopen("kotlin.out", "w", stdout);

#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,b) for(int i =a;i<=b;i++)
// for(auto it : g[i].nb)

typedef long long ll;
typedef long double ld;

#define pb push_back

#define all(a) a.begin(),a.end()

#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> t3ll;

#define mt make_tuple
#define get(a,id) get<id>(a)

const long double PI = acos(-1.0l);
const ll MOD = 1e9+7;

//LONG_LONG_MAX
//-DBL_MAX

bool debug = 0;
#define printa(a) cout << #a << " = " << (a) << endl
#define prin(a) if(debug) cout << #a << " = " << (a) << endl
#define soprin(a) if(debug) cout << (a)
#define ppal(a)  if(debug) cout << #a << endl
#define prinsep if(debug) cout << "------" << endl
#define cendl if(debug) cout << endl
#define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl
#define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n"
#define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;}

const int N = 1;

int main(){
	//FILE_IN FILE_OUT
	ll x, y, l, r;
	cin >> x >> y >> l >> r;

	vector<ll> v1(1,1), v2(1,1);

	ll cur = x;
	while(1){
		v1.pb(cur);
		if(1.0l*cur*x>LLONG_MAX){
			break;
		}
		cur*=x;
	}

	cur = y;
	while(1){
		v2.pb(cur);
		if(1.0l*cur*y>LLONG_MAX){
			break;
		}
		cur*=y;
	}

	ll maxt = 0;

	set<ll> s;

	fr(i,v1.size()){
		fr(j,v2.size()){
			s.insert(v1[i]+v2[j]);
		}
	}

	ll ant = 0;
	bool tem = 0;
	for(auto it : s){
		if(it<l) continue;
		prin(it);
		if(it>r) break;
		maxt = max(maxt,it-1-max(ant,l-1));

		tem = 1;
		ant = it;
	}

	if(!tem) maxt = r-l+1;
	else{
		maxt = max(maxt,r-max(ant,l-1));
	}

	cout << maxt << endl;

	return 0;
}

#include<bits/stdc++.h>

using namespace std;

vector<long long > v;

int main()
{
          long long  int l,r,x,y,ans(0);
          cin>>x>>y>>l>>r;
          v.push_back(l-1);
          v.push_back(r+1);
          long long int i=1;
          while(1)
          {
                    long long int j=1;
                    while(1)
                    {
                              if(i+j>=l && i+j<=r)
                                        v.push_back(i+j);
                              if(r/j<y)
                                        break;
                              else
                                        j*=y;
                    }
                    if(r/i<x)
                              break;
                    else
                              i*=x;
          }
          sort(v.begin(),v.end());
          for(int i=0;i<v.size()-1;i++)
                    ans=max(ans,v[i+1]-v[i]-1);
          cout<<ans;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll x,y,l,r;
    cin >> x >> y >> l >> r;
    ll n=1,m=1;
    vector<ll> v;
    for(ll a=0;;++a)
    {
        m=1;
        for(ll b=0;;++b)
        {
            if(n+m<=r&&n+m>=l) v.push_back(n+m);
            if(r/m<y) break;
            else m*=y;
        }
        if(r/n<x) break;
        else n*=x;
    }
    sort(v.begin(),v.end());
    ll ans=0;
    if(v.size()==0) ans=r-l+1;
    else ans=max(v[0]-l,r-v[v.size()-1]);
    for(int i=1;i<v.size();i++)
    {
        ans=max(ans,v[i]-v[i-1]-1);
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(0)
#define endl "\n"
#define int long long
typedef vector<int> vi;
#define all(x) x.begin(),x.end()
#define pii              pair <int,int>
#define debug(x) cerr << '>' << #x << ':' << x << endl;
//#pragma comment(linker, "/STACK:667772160")
#define in_range(x, y, r, c) ((x >= 0 && x < r && y >= 0 && y < c))
#define rep(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1e18+99;
const int MOD=1e9+7;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
void boostIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
ll modpow(ll a,ll b) {ll res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
const int   maxx = (int)1e18+122;

int32_t main  (){
    boostIO();
    int x,y,l,r;
    cin>>x>>y>>l>>r;
    vi first,second;
    vi res;
    int cur=1;
    first.emplace_back(1);
    second.emplace_back(1);
    while(cur<=maxx/x)first.emplace_back(cur*x),cur*=x;
    cur=1;
    while(cur<=maxx/y)second.emplace_back(cur*y),cur*=y;

    for (int i = 0; i < first.size(); ++i) {
        for (int j = 0; j < second.size(); ++j) {
            if(first[i]+second[j]>=l and first[i]+second[j]<=r)
                res.emplace_back(first[i]+second[j]);
        }
    }
    res.emplace_back(l-1);
    res.emplace_back(r+1);
    sort(all(res));
    int ans=0;
    for (int k = 0; k+1 < res.size() ; ++k) {
        ans=max(ans,res[k+1]-res[k]-1);
    }
    cout<<ans;

}

#include<bits/stdc++.h>

// @author : sam000013

#define ll int
#define int long long

#define PI 3.14159265
#define pb(x) push_back(x)
#define all(v) v.begin(), v.end()
#define endl cout<<"\n"
#define dbg(x) cout<<#x<<" "<<x;endl;
#define bye endl;return
#define uni 1.000000
using namespace std;

const int mod = 1000000007;
template<typename T>

void debug(vector<T> x){
for(auto e:x){
    cout<<e<<" ";
}
cout<<"\n";
}
/*

*/
bool isOverflow(int a, int b){
    if (a == 0 || b == 0)
        return false;

    if(b > 0 && a > 18446744073709551615U / b)
        return true;

    return false;
}
void solve(){
    int x,y,l,r;
    cin>>x>>y>>l>>r;
    vector<int> powerX, powerY;
    powerX.push_back(1);powerY.push_back(1);
    powerX.push_back(x); powerY.push_back(y);
    int t1 = x, t2 = y;
    while(t1 <= 1e18){
        if(isOverflow(t1, x))
            break;
        t1 *= x;
        powerX.push_back(t1);
    }
    while(t2 <= 1e18){
        if(isOverflow(t2, y))
            break;
        t2 *= y;

        powerY.push_back(t2);
    }
    int ans = 0;
    set<int> s;
    // debug(powerX); debug(powerY);
    for(int i=0; i<powerX.size(); i++){
        for(int j=0; j<powerY.size(); j++){
            int sum = powerX[i] + powerY[j];
            if(sum > r)
                break;
            if(sum >= l)
                s.insert(sum);
        }
    }
    s.insert(l-1);
    s.insert(r+1);
    vector<int> helper;
    for(auto x:s){
        helper.push_back(x);
    }
    for(int i=0; i < helper.size() - 1; i++){
        int cur = helper[i+1] - helper[i] - 1;
        ans = max(cur, ans);
    }
    cout<<ans;endl;

}

signed main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t = 1;
    // cin>>t;
    for(int i=1; i<=t; i++){
        solve();
    }
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <numeric>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;
long long x, y;
set<long long> numbers;
void rec(long long fr, long long sc)
{
	if (fr == -1)
	{
		fr = 1;

		for (int i = 0; i < 100; i++)
		{
			rec(fr, sc);
			if ((fr > 1e9 && x > 1e9))
				break;
			long long prev = fr;
			fr =fr* x;
			if (fr/x != prev || fr > 1e18 )
				break;
		}
		return;
	}

	if (sc == -1)
	{
		sc = 1;

		for (int i = 0; i < 100; i++)
		{
			rec(fr, sc);
			if (sc > 1e9 && y > 1e9)
				break;
			long long prev = sc;
			sc =sc * y;
			if (sc / y != prev || sc >1e18 )
				break;
		}
		return;
	}
	if (sc + fr <= 1e18 && sc + fr > 0)
		numbers.insert(sc + fr);
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	long long l, r;
	cin >> x >> y;
	cin >> l >> r;
	rec(-1, -1);

	while (!numbers.empty() && *numbers.begin() < l)
		numbers.erase(numbers.begin());
	while (!numbers.empty() && *numbers.rbegin() > r)
		numbers.erase(*numbers.rbegin());

	if (numbers.empty())
	{
		cout << r - l + 1;
		return 0;
	}
	set<long long> num = numbers;
	numbers.insert(r);
	long long ans = 0;
	long long cur_l = l;
	for (auto it = numbers.begin(); it != numbers.end(); it++)
	{
		if (num.find(*it) == num.end() && num.find(cur_l) == num.end())
			ans = max(ans, *it - cur_l + 1);

		if (num.find(*it) != num.end() && num.find(cur_l) == num.end())
			ans = max(ans, *it - cur_l);

		if (num.find(*it) == num.end() && num.find(cur_l) != num.end())
			ans = max(ans, *it - cur_l);

		if (num.find(*it) != num.end() && num.find(cur_l) != num.end())
			ans = max(ans, *it - cur_l - 1);
		cur_l = *it;
	}

	cout << ans;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back

long long pow(long long a, long long b)
{
    long long res = 1;
    while(b)
    {
        if(b&1)
            res*= a;
        a*= a;
        b>>= 1;
    }
    return res;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);

    long long x, y, l, r;
    cin >> x >> y >> l >> r;

    long long inf = 1e18;
    long long a = log(inf) / log(x);
    long long b = log(inf) / log(y);

    set<long long> s;
    for (long long i = 0; i <= a; i++)
    {
        long long p1 = pow(x, i);
        for (long long j = 0; j <= b; j++)
        {
            long long p2 = pow(y, j);
            long long val = p1 + p2;
            if (val <= r and val >= l)
                s.insert(val);
        }
    }
    s.insert(l - 1);
    s.insert(r + 1);

    // for (auto x : s)
    //     cerr << x << ' ';

    long long maxlen = 0, last = *s.begin();
    auto it = s.begin();
    it++;
    for (; it != s.end(); it++)
    {
        maxlen = max(maxlen, *it - last - 1);

        // cerr << *it << "  " << last << "  " << maxlen << "\n";
        last = *it;
    }

    // maxlen = max(maxlen, r - last + 1);
    cout << maxlen << "\n";

    return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define  ll unsigned long long int

int main()
{
    ll x,y,l,r;
    //scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    cin>>x>>y>>l>>r;

    vector<ll>x_er_jonno,y_er_jonno;

    int cnt1=0,cnt2=0;

    ll tem_x=x;

    while(tem_x)
    {
        cnt1++;
        tem_x/=10;
    }
    ll tem_y=y;

    while(tem_y)
    {
        cnt2++;
        tem_y/=10;
    }

    ll val=1;
    x_er_jonno.push_back(1);
    while(val<r && x!=1)
    {
        //cout<<val<<endl;
        if(val!=1)
            x_er_jonno.push_back(val);
        ll tem_val=val;
        int cnt=0;
        while(tem_val)
        {
            cnt++;
            tem_val/=10;
        }
        if(cnt+cnt1>=20)
            break;
        val*=x;

    }
    val=1;
    y_er_jonno.push_back(1);
    while(val<r && y!=1)
    {
        if(val!=1)
            y_er_jonno.push_back(val);
        ll tem_val=val;
        int cnt=0;
        while(tem_val)
        {
            cnt++;
            tem_val/=10;
        }
        if(cnt+cnt2>=20)
            break;
        val*=y;

    }

    //cout<<x_er_jonno.size()<<" "<<y_er_jonno.size()<<endl;

    set<ll>unlucky;

    for(auto it:x_er_jonno)
    {
        //cout<<it<<" ";
        for(auto it1:y_er_jonno)
        {
            if(it+it1<=r && it+it1>=l)
                unlucky.insert(it+it1);
        }
    }
    if(unlucky.size()==0)
    {
        cout<<r-l+1<<endl;
        return 0;
    }

    ll re=0;

    auto it=unlucky.begin();
    auto it1=it;
    it1++;
    re=max(re,*it-l);
    for(; it1!=unlucky.end(); it1++,it++)
    {
        // cout<<*it<<" "<<endl;
        re=max(re,*it1-*it-1);
    }
    //cout<<r-*it<<endl;
    re=max(re,r-*it);

    //printf("%lld\n",re);
    cout<<re<<endl;

    return 0;
}

#include<bits/stdc++.h>
#define ll long long
ll x,y,l,r;
using namespace std;
set<ll>st;
set<ll>::iterator it;
int main()
{
	cin>>x>>y>>l>>r;
	ll t1=0,t2=0;
	ll s=1,s1=1,s2=1;
	while(1)
	{
		s+=s2-s2/y;
		if(s>=l&&s<=r)
		st.insert(s);
		if(r/s2<y)
		{
			if(r/s1<x)
			break;
			s1*=x;
			s=s1;
			s2=1;
			continue;
		}
		else
		{
			s2*=y;
		}
	}
	if(st.empty())
	{
		cout<<r-l+1<<endl;
		return 0;
	}
	ll maxl=0;
	ll pre=l-1;
	for(it=st.begin();it!=st.end();it++)
	{
		if(*it==l)
		maxl=0;
		else
		maxl=max(maxl,*it-pre-1);
		pre=*it;
	}
	it--;
	maxl=max(maxl,r-*it);
	cout<<maxl;
}

#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
vector <ll> v;
int main()
{
    ll x,y,l,r,i,j;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    ll tx,ty,dx=r;
    for(i=0; i<=61; i++)
    {
        if(i!=0) dx/=x;
        if(!dx) break;
        if(i==0) tx=1;
        else tx*=x;
        ll dy=r;
        for(j=0; j<=61; j++)
        {
            if(j!=0) dy/=y;
            if(!dy) break;
            if(j==0) ty=1;
            else ty*=y;
            ll k=tx+ty;
            if(k>=l&&k<=r)
                v.push_back(k);
        }
    }
    if(!v.size())
    {
        printf("%I64d\n",r-l+1);
        return 0;
    }
    ll ans=0;
    sort(v.begin(),v.end());
    for(i=0; i<v.size(); i++)
    {
        if(i==0&&v[0]!=l)
            ans=max(ans,v[i]-l);
        if(i==v.size()-1)
            ans=max(ans,r-v[i]);
        else
            ans=max(ans,v[i+1]-v[i]-1);
    }
    printf("%I64d\n",ans);
    return 0;
}
/*2018-07-24 21:24:57.863*/

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=67;
ll a[maxn];
ll b[maxn];
ll x,y,l,r;
ll sum[maxn*maxn];
ll fpow(ll x,int k)
{
	long long res=1;
    while(k)
    {
        if(k&1)res=res*x;
        x=x*x;
        k>>=1;
    }
    return res;
}
int main()
{
	cin>>x>>y>>l>>r;
	int p=floor(log(r*1.0)/log(x*1.0));
	int q=floor(log(r*1.0)/log(y*1.0));
	for(int i=0;i<=p;i++)
	{
		a[i]=fpow(x,i);
	//	cout<<a[i]<<endl;
	}
	for(int i=0;i<=q;i++)
	{
		b[i]=fpow(y,i);
	//	cout<<b[i]<<endl;
	}
	int cnt=0;
	for(int i=0;i<=p;i++)
	{
		for(int k=0;k<=q;k++)
		{
			if(a[i]+b[k]<=r&&a[i]+b[k]>=l)
			sum[cnt++]=a[i]+b[k];
		}
	}
	sort(sum,sum+cnt);
//	cnt=unique(sum,sum+cnt)-sum;
	if(cnt==0)
	{
		cout<<r-l+1<<endl;
		return 0;
	}
	ll ans=max(sum[0]-l,r-sum[cnt-1]);
	for(int i=1;i<=cnt-1;i++)
	{
		ans=max(ans,sum[i]-sum[i-1]-1);
	//	cout<<ans<<endl;
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int Size=1024;
#define lson  rt<<1
#define rson  rt<<1|1
#define rep(i,a,b)  for(i=a;b>=i;++i)
#define dep(i,a,b)  for(i=a;i>=b;--i)
typedef long long ll;
const ll maxn=1e18+100;

ll a[300],b[300];
ll sum[20000];
map<ll,int>mp;
int main()
{
    ll x,y,l,r,tot1,tot2,i,j,flag1,flag2;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    a[0]=1;
    b[0]=1;
    tot1=0;
    tot2=0;
    for(i=1;100>=i;i++)
    {
        if(double(a[i-1])*x<maxn)
        {
           a[i]=a[i-1]*x;
           tot1++;
        }
        else break;
    }
    for(i=1;100>=i;i++)
    {
        if(double(b[i-1])*y<maxn)
        {
           b[i]=b[i-1]*y;
           tot2++;
        }
        else break;
    }
    ll tot=0;
    for(i=0;tot1>=i;i++)
    {
        for(j=0;tot2>=j;j++)
        {
            if(double(a[i])+b[j]<maxn&&mp[a[i]+b[j]]==0)
            {
                sum[++tot]=a[i]+b[j];
                mp[a[i]+b[j]]++;
            }
        }
    }
    sort(sum+1,sum+tot+1);
    ll ans;
    if(sum[1]>r||sum[tot]<l)
    {
        printf("%I64d\n",r-l+1);
        return 0;
    }
    for(i=1;tot>=i;i++)
    {
        if(sum[i]>=l) break;
    }
    flag1=i;
    if(sum[flag1]>r)
    {
        printf("%I64d\n",r-l+1);
        return 0;
    }
    for(;tot>=i;i++)
    {
        if(r<sum[i]) break;
    }
    flag2=i-1;
    ans=max(sum[flag1]-l,r-sum[flag2]);
    for(i=flag1+1;flag2>=i;i++)
    {
        ans=max(ans,sum[i]-sum[i-1]-1);
    }
    printf("%I64d\n",ans);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

int main(){
	long long x,y,l,r,i,j,tmp, tmp_x=1, tmp_y=1, ans=0;
	cin>>x>>y>>l>>r;

	vector<long long>V;
	V.push_back(l-1);
	V.push_back(r+1);

	while(tmp_x<r){
		tmp_y=1;
		while(tmp_y<r){
			tmp=tmp_x+tmp_y;
			if(l<=tmp && tmp<=r) V.push_back(tmp);
			if(r/tmp_y<y) break;
			tmp_y*=y;
		}
		if(r/tmp_x<x) break;
		tmp_x*=x;
	}
	sort(V.begin(),V.end());

	// for(i=0;i<V.size();i++) cout<<V[i]<<"  ";

	for(i=1;i<V.size();i++){
		if(V[i]==V[i-1]) continue;
		ans=max(ans,V[i]-V[i-1]-1);
	}
	cout<<ans;
	return 0;
}

/* �˰���
  1. x, y�� ������ ���� �ݺ������� ���
  2. �̶�, x�� y�� ���� r���� ���� �ʵ��� ����
  3. x+y�� ���� ���Ϳ� �����ϰ� �װ��� ����
  4. ���ĵ� ������ ���� �����ؼ� ���� ū ���̸� ���̴� ���� ���� */

#include <iostream>
#include <algorithm>
using namespace std;

long long a, b, l, r, MAX = 0;
//1000000000000000000
long long v[101000];

void makev()
{
    /*
    for ( long long i = 1; i <= r / a; i *= a)
    {
        for (long long j = 1; j <= (r - i) / b; j *= b)
        {
            if ( i + j >= l)
            {
                v[MAX++] = i + j;
            }
        }
    }
    */
    long long i = 1, j = 1;
    j = 1;
    if (2 >= l) v[MAX++] = 2;
    while (j <= (r - i) / b)
    {
        j *= b;
        if (i + j >= l) v[MAX++] = i + j;
    }
    while (i <= r / a)
    {
        i *= a;
        j = 1;
        if (1 + i <= r)
        {
            if (1 + i >= l) v[MAX++] = 1 + i;
        }
        while (j <= (r - i) / b)
        {
            j *= b;
            if (i + j >= l) v[MAX++] = i + j;
        }
    }
}

int main()
{

    long long rez = 1;
    cin >> a >> b >> l >> r;
    v[MAX++] = l - 1;
    makev() ;
    sort(v, v+ MAX);
    v[MAX++] = r + 1;
    for (int i = 1; i < MAX; i++)
    {
        if (v[i] - v[i-1] > rez)
        {
            rez = v[i] - v[i-1];
        }
    }
    rez--;
    cout << rez;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long x,y,l,r;
vector<long long >v;
void fun(long long p,vector<long long> &vec)
{
	long long mul=1;
	vec.push_back(mul);
	while(1)
	{
		if(mul>2e18/p) break;

		mul*=p;
		if(mul<=r)
		{
			vec.push_back(mul);
		}
		else
		{
			break;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	int t=1;// cin>>t;
	while(t--)
	{
		cin>>x>>y>>l>>r;
		v.clear();
		v.push_back(l-1);
		v.push_back(r+1);

		vector<long long> v1,v2;

		fun(x,v1);
		fun(y,v2);

		for(int i=0;i<v1.size();i++)
		{
			for(int j=0;j<v2.size();j++)
			{
				long long s=v1[i]+v2[j];
				if(s>=l && s<=r) v.push_back(s);
			}
		}
		sort(v.begin(),v.end());

		long long ans=0;
		for(int i=1;i<v.size();i++)
		{
			ans=max(ans,v[i]-v[i-1]-1);
		}
		cout<<ans<<endl;
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

long long x,y,l,r;
long long year[100010],a[10010],b[10010];
int cnt,cnt1,cnt2;

int cmp(long long a,long long b){
	return a<b;
}

int main(){
	cin>>x>>y>>l>>r;
	a[0]=1;b[0]=1;
	cnt1=1;cnt2=1;
	while (r/x>=a[cnt1-1]){
		a[cnt1]=a[cnt1-1]*x;
		cnt1++;
	}
	while (r/y>=b[cnt2-1]){
		b[cnt2]=b[cnt2-1]*y;
		cnt2++;
	}
	year[cnt++]=l-1;
	year[cnt++]=r+1;
	for (int i=0;i<cnt1;i++){
		for (int j=0;j<cnt2;j++){
			if (a[i]+b[j]<=r&&a[i]+b[j]>=l){
				year[cnt++]=a[i]+b[j];
			}
			else {
				if (a[i]+b[j]>r) break;
			}
		}
	}
	sort(year,year+cnt,cmp);
	long long ans=0;
/*	for (int i=0;i<cnt;i++){
		printf("%lld ",year[i]);
	}
	cout<<endl;
*/	for (int i=1;i<cnt;i++){
		ans=max(year[i]-year[i-1]-1,ans);
	}

//	ans=max(year[0]-l,ans);

//	ans=max(r-year[cnt-1],ans);
	printf("%lld\n",ans);
//	printf("%lld\n",510000000000*510000000000);
	return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//
//using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

/*** Typedef ***/
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
typedef    long double   ld;

//typedef    unordered_set<ll>   usll;
//typedef    unordered_multiset<ll>   umsll;

/*** Loops ***/
#define ffit(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
#define F(a,b) for(int i = a; i < b; i++)
#define ff1(i,a,b) for(ll i = a; i < b; i++)
#define ff2(i,a,b) for(ll i = b-1; i >= a; i--)
#define ff3(i,a,b,m) for(ll i = a; i <b; i+=m)
#define ipar(ar,n) ll ar[n]; ff1(i,0,n) cin>>ar[i];
#define opar(ar,n) ff1(i,0,n) cout<<ar[i]<<" ";
#define foRev(num) for(ll i = num - 1; i >= 0; i--)
#define forIn(arr, num) for(ll i = 0; i < num; i++) sc1(arr[i]);
#define forIn1(arr, num) for(ll i = 1; i <= num; i++) sc1(arr[i]);
#define vpnt(ans) for(ll i = 0; i < ans.size(); i++) cout << ans[i] << (i + 1 < ans.size() ? ' ' : '\n');
#define apnt(arr, num) for(ll i = 0; i < num; i++) cout << arr[i] << (i + 1 < num ? ' ' : '\n');

/*** Define Values ***/

#define PI acos(-1.0)
#define eps 1e-7
#define size1 15
#define mdd 1000000007
/*** STLs ***/
#define sf(a)            scanf("%lld",&a)
#define sff(a,b)         scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)
#define cu continue
#define br break
#define rsz resize
#define ins insert
#define ft front
#define bk back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define rr return
#define mem(name, value) memset(name, value, sizeof(name))
#define MN3(n1,n2,n3) min(n1,min(n2,n3))
#define MN4(n1,n2,n3,n4) min(n1,min(n2,min(n3,n4)))
#define MX3(n1,n2,n3) max(n1,max(n2,n3))
#define MX4(n1,n2,n3,n4) max(n1,max(n2,max(n3,n4)))
#define mxar(a,n) *max_element(a,a+n)
#define mnar(a,n) *min_element(a,a+n)
#define arr_sm(a,n) accumulate(a,a+n,0)
#define vc_sm(ve) accumulate(ve.begin(),ve.end(),0)
#define mx_vc(ve) *max_element(ve.begin(),ve.end())
#define mn_vc(ve) *min_element(ve.begin(),ve.end())
//#define endl " \n"

/*** STLs ***/
typedef vector <ll> vll;
#define vvll vector<vll>
typedef set <ll> sll;
typedef multiset <ll> msll;
typedef multimap <ll,ll> mpll;
typedef queue <ll> qll;
typedef map <ll, ll> mll;
typedef pair <ll, ll> pll;
typedef vector <pair <ll, ll> > vpll;
typedef priority_queue <pair <ll, ll> > pqpll;
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int> >
#define ml unordered_map<ll,ll>
///typedef    unordered_multiset<int>   umsi;
typedef    unordered_set<ll>   us;

///vector<vector<int>> v(10, vector<int>(20,500)); 2d vector initialization. of 10 rows and 20 columns, with value 500.

/*** Sorts ***/
#define czero(n)                    __builtin_popcountll(n) // count the number of 1's in the bit representation of a number
#define cenzero(n)                  __builtin_ctzll(n) // count the number of zereos from the end of the bit representation of a number until a 1
#define countbegzero(n)             __builtin_clzxll(n)
#define checkparity(n)              __builtin_parityll(n)// checks whether the number of bits are even or odd
#define all(v) (v).begin(), (v).end()
#define rev(v) reverse(all(v))
#define srt(v) sort(all(v))
#define srtgrt(v) sort(all(v), greater<ll>())
#define prsrt greater<pair<int,int>>()
/*** Some Prints ***/
#define ip(n) ll n; cin>>n;
#define is(s) string s; cin>>s;
#define ipp(n,k) ll n; cin>>n; ll k; cin>>k;
#define ip3(n,m,k) ll n; cin>>n;ll m;cin>>m; ll k; cin>>k;
#define kl    '\n'
#define no cout << "NO" << '\n'
#define yes cout << "YES" << '\n'
#define case cout << "Case " << t++ << ": ";
#define ps(x,y)         fixed<<setprecision(y)<<x
#define inf             1e18
#define w(x)             ll x; cin>>x; while(x--)
#define go(i, a) for (auto &i : a)
#define go1(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)
//#define mk(arr,n,type) type *arr=new type[n];
#define sz(obj) (int(obj.size()))
#define ipve(vc,n) vll vc(n); ff1(i,0,n){cin>>vc[i];}
#define ipvc(vc,n) vll vc; ff1(i,0,n){ip(x);vc.pb(x);}
#define cer cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
#define depressed()                    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
//bool ans = all_of(arr, arr + 6, [](int x) {return x & 1; } );
const double pi = 3.141592653589793238460;
#define TRC
#ifdef TRC
#define trc(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trc(...)
#endif

void fun()
{
  ipp(x,y); ipp(l,r);
  ll n = 1;
  vll v,a;
   v.pb(n);
  while(n<=1e18/x)
  {
    // if(n>2e18/x)
    // {
    //   br;
    // }
    n*=x;
    //if(n>r) br;
    v.pb(n);
  }
  n = 1;
  a.pb(n);
  while(n<=1e18/y)
  {
    // if(n>1e18/y || n>r)
    // {
    //   br;
    // }
    n*=y;
    //if(n>r) br;
    a.pb(n);
  }
  //go(i,a) cout<<i<<" ";cout<<kl;
  vll b;
  ff1(i,0,sz(v))
  {
    ff1(j,0,sz(a))
    {
      if(v[i]+a[j]>=l && v[i]+a[j]<=r)
      b.pb(v[i]+a[j]);
    }
  }
  b.pb(l-1);
  b.pb(r+1);
  srt(b);
  //go(i,b) cout<<i<<" ";cout<<kl;
  ll mx = 0;
  ff1(i,1,sz(b))
  {
    mx = max(mx,b[i]-b[i-1]-1ll);
  }
  cout<<mx<<kl;
}

int main()
{
// tasklist
// taskkill /f /im mo.exe
    depressed();
  ll t = 1;
 //cin>>t;
  while(t--)
  {
     fun();
  }
  //cer;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;

int main(){
	unsigned long long x,y,l,r,i,j,tmp, tmp_x=1, tmp_y=1, ans=0;
	cin>>x>>y>>l>>r;

	vector<unsigned long long>V;
	V.push_back(l-1);
	V.push_back(r+1);

	while(tmp_x<r){
		tmp_y=1;
		while(tmp_y<r){
			tmp=tmp_x+tmp_y;
			if(l<=tmp && tmp<=r) V.push_back(tmp);
			// if(tmp_y>1000000000) break;
			tmp_y*=y;
		}
		// if(tmp_x>1000000000) break;
		tmp_x*=x;
	}
	sort(V.begin(),V.end());

	// for(i=0;i<V.size();i++) cout<<V[i]<<"  ";

	for(i=1;i<V.size();i++){
		if(V[i]==V[i-1]) continue;
		ans=max(ans,V[i]-V[i-1]-1);
	}
	cout<<ans<<endl;
	return 0;
}

/* �˰���
  1. x, y�� ������ ���� �ݺ������� ���
  2. �̶�, x�� y�� ���� r���� ���� �ʵ��� ����
  3. x+y�� ���� ���Ϳ� �����ϰ� �װ��� ����
  4. ���ĵ� ������ ���� �����ؼ� ���� ū ���̸� ���̴� ���� ���� */

#include<bits/stdc++.h>
using namespace std;

long long x,y,l,r,ans=0;
set<long long>s;
vector<long long>ls1,ls2,a;

int main()
{
	scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);		//改成I64d
	long long ct1=0,ct2=0,st1=1,st2=1,rr=r;
	while(rr>1)rr/=x,ct1++;
	for(int i=0;i<=ct1;i++)ls1.push_back(st1),st1*=x;
	rr=r;
	while(rr>1)rr/=y,ct2++;
	for(int i=0;i<=ct2;i++)ls2.push_back(st2),st2*=y;
	for(int i=ls1.size()-1;i>=0;i--)
	  for(int j=ls2.size()-1;j>=0;j--){
	  	long long t=ls1[i]+ls2[j];
	  	if(s.find(t)!=s.end()||t<l||t>r)continue;
	  	a.push_back(t);
		s.insert(t);
	  }
	sort(a.begin(),a.end());
	for(int i=a.size()-1;i>0;i--)ans=max(ans,a[i]-a[i-1]-1);
	if(a.size()==0)ans=r-l+1;
	else ans=max(ans,max(r-a[a.size()-1],a[0]-l));
	printf("%I64d",ans);
	return 0;
}

// Author - abbi_18
#include<bits/stdc++.h>
#include <stdint.h>

using 			namespace 			std;

#define 		ff 					first
#define 		ss 					second
#define 		pb 					push_back
#define 		mp 					make_pair
#define 		eps 				1e-9
#define 		inf 				1e18
#define 		PI 					3.14159265
#define 		mset(A,num,n);   	memset(A,num,n);
#define 		all(a) 				a.begin(),a.end()
#define 		allr(a) 			a.rbegin(),a.rend()
#define			nl					cout << endl
#define 		vi					vector<int>
#define 		vll					vector<ll>
#define 		pi					pair<int,int>

#define 		trace1(x)	             	cout <<#x<<": "<<x<< endl;
#define 		trace2(x, y)             	cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define 		trace3(x, y, z)          	cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define 		trace4(a, b, c, d)       	cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define 		trace5(a, b, c, d, e)    	cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int 		MOD = 				1e9+7;
const int 		N   = 				1e6+5;

typedef 		long long int 		ll;
typedef 		int64_t 			lln;

ll POWER[65];
ll aonb(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}

void precompute() {
	POWER[0]=1;
	for(int i=1;i<63;i++) POWER[i]=POWER[i-1]<<1LL;
}

int main()
{
//  freopen("A-large-practice.in","rt",stdin);
//	freopen("one.out","wt",stdout);
	std::ios_base::sync_with_stdio(false);
//	std::cout << std::setprecision(10) << std::fixed;

	ll x,y;
	ll l,r;
	ll temp;
	cin >> x >> y >> l >> r;
	ll a=x,b=y;

	vector<ll> X;
	if (a==0)
		X.pb(0);
	else
		X.pb(1);
	while(a<=r && a>1){
		X.pb(a);
		a *= x;
	}

	vector<ll> Y;
	if (b==0)
		Y.pb(0);
	else
		Y.pb(1);
	while(b<=r && b>1){
		Y.pb(b);
		b *= y;
	}

	set<ll> B;
	vector<ll> A;

	for (int i=0;i<X.size();i++){
		for (int j=0;j<Y.size();j++){
			temp = X[i]+Y[j];
			if (temp>=l && temp<=r){
				B.insert(temp);
			}
		}
	}

	set<ll>::iterator it = B.begin();
	for (;it != B.end();it++){
		A.pb(*it);
	}

	if (A.size() == 0){
		cout << r-l+1;
		return 0;
	}

//	for (int i=0;i<X.size();i++){
//		cout << X[i] << " ";
//	}
//	nl;
//	for (int i=0;i<Y.size();i++){
//		cout << Y[i] << " ";
//	}
//	nl;

	sort(all(A));

	ll ans = A[0]-l;

//	for (int i=0;i<A.size();i++){
//		cout << A[i] << " ";
//	}
//	nl;

	for (int i=1;i<A.size();i++){
		if (A[i]-A[i-1]-1 > ans){
			ans = A[i]-A[i-1]-1;
		}
	}
	ans = max(ans,r-A[A.size()-1]);

	cout << ans;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vector<int>> vvi;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;

const int dx[4]={+1, -1, 0, 0};
const int dy[4]={0, 0, +1, -1};
const int MOD = 1e9+7;

const int INF = 1e10;

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    vi ans;
    int x,y,l,r;
    cin>>x>>y>>l>>r;
    int N=1e18;
    if(x>y)
        swap(x,y);
    for(int i=1;i<r+10;i=i*x){
        for(int j=1;i+j<=r+10;j=j*y){
           // cout<<i<<" "<<j<<'\n';
            ans.pb(i+j);
            if(j<N/y){

            }else{
                break;
            }
        }
        if(i<=N/x){

        }else{
            break;
        }
    }
    sort(all(ans));
    ans.resize(distance(ans.begin(),unique(all(ans))));
    int final=0;
    int last=-1;
    int i;
    i=lower_bound(all(ans),l)-ans.begin();
    for(;i<ans.size()-1;i++){

        if(ans[i+1]<=r){
            cout<<ans[i]<<'\n';
            final=max(final,ans[i+1]-ans[i]-1);
        }else{
            last=ans[i+1];
            break;
        }
    }
    if(last!=-1)
    {last=r;
        final=max(final,last-ans[i]);}
    cout<<final;
    return 0;
}

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>

using namespace std;

#define ll long long

ll pow(ll x, int n)
{
    if (!n) return 1;
    if (n&1) return x*pow(x,n-1); else {ll a=pow(x,n/2); return a*a;}
}

int main()
{
    //freopen("d:\\input.txt","r",stdin);
    //freopen("d:\\output.txt","w",stdout);
    ll x,y,l,r;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    set <ll> s;
    for (int i=0; i<=65; i++)
    {
        ll a=pow(x,i);
        if (a<=0||a>1e18) break;
        for (int j=0; j<=65; j++)
        {
            ll b=pow(y,j);
            if (b<=0||b>1e18) break;
            ll cur=a+b;
            //printf("%I64d=2^%d+3^%d\n",cur,i,j);
            s.insert(cur);
        }
    }
    ll ans=0;
    if (l==1) ans=1;
    ll f=2;
    for (set<ll>::iterator i=s.begin()++; i!=s.end(); i++)
    {
        ll cur=*i;
        if (cur==2) continue;
        //printf("%I64d\n",cur);
        if (f>=l&&cur<=r) ans=max(ans,cur-f-1);
        if (f<l&&cur<=r) ans=max(ans,cur-l);
        if (cur>r&&f<r) if (f>l) ans=max(ans,r-f); else ans=r-l+1;
        f=cur;
    }
    if (f<r) ans=max(ans,r-f);
    printf("%I64d",ans);
}

#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;

int x, y;
long long l, r;
long long xx[100], yy[100], sum[10000];

int main() {
	ios::sync_with_stdio( false ), cin.tie(0), cout.tie(0);
	cin>> x >> y >> l >> r;
	int e1 = 0;
	long long num = 1;
	xx[e1++] = 1;
	while( num < 1e18/x ) {
		num *= x;
		xx[e1++] = num;
	}
	num = 1;
	int e2 = 0;
	yy[e2++] = 1;
	while( num < 1e18/y ) {
		num *= y;
		yy[e2++] = num;
	}
	int k = 0;
	for( int i = 0; i < e1; i++ )
		for( int j = 0; j < e2; j++ ) {
			sum[k++] = xx[i] + yy[j];
		}
		sort(sum, sum + k);
		long long ans = 0;
		int ind = 0;
		while( sum[ind] < (long long)l) ind++;
		ans = sum[ind] - l;
		for( ; sum[ind + 1] <= r; ind++ ) {
			ans = max(ans, sum[ind + 1] - sum[ind] - 1);
		}
		ans = max(ans, r - sum[ind]);
		cout<< ans << endl;
		return 0;
}

#include<bits/stdc++.h>

#define ll long long int

using namespace std;

int main(){
	ll x,y;
	cin>>x>>y;
	ll a=1,b=1;
	vector<ll> p;
	while(a<=1e18){
		b=1;
		while(b<=1e18-a){
			p.push_back(a+b);
			b*=y;
		}
		a*=x;
	}
	sort(p.begin(),p.end());

	ll l,r;
	cin>>l>>r;
	ll ci=0,cs=p.size(),c,cc;
	for(int i=0;i<100;i++){
		c=(ci+cs)/2;
		if(p[c]>l)cs=c;
		else ci=c;
	}
	ci=0;
	cs=p.size();
	for(int i=0;i<100;i++){
		cc=(ci+cs)/2;
		if(p[cc]>r)cs=cc;
		else ci=cc;
	}
	ll res=0;
	res=max(p[c]-l,res);
	for(int i=c;i<cc;i++){
		res=max(p[c+1]-p[c]-1,res);
	}
	res=max(r-p[cc],res);
	cout<<res;
}

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<string>
#include<iostream>
#include<stack>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define ll long long int
#define ull unsigned long long int
#define pi acos(-1)
#define mod 1000000007
#define inf 0x7fffffff
#define eps 1e-6
using namespace std;

ull qpow(ull x,ull y)
{
    ull res=1;
    while(y)
    {
        if(y&1)res*=x;
        y>>=1;
        x*=x;
    }
    return res;
}

map<ull,bool>mp;
ull a[64*64];
int main()
{
    ull x,y,l,r;
    int cont=1;
    scanf("%llu%llu%llu%llu",&x,&y,&l,&r);
    ull ans=0;
    for(ull i=0;i<=63; i++)
        for(ull j=0;j<=63; j++)
        {
            ull z=qpow(x,i)+qpow(y,j);
            if(z>=l&&z<=r&&!mp[z])
            {
                a[cont++]=z;
                mp[z]=true;
            }
        }
    a[0]=l,a[cont]=r+1;
    sort(a+1,a+cont);
    ull maxx=0;
    for(int i=0;i<cont;i++)
    {
        if(a[i+1]>a[i]+1)
            maxx=max(maxx,a[i+1]-a[i]-1);
    }
    printf("%llu\n",maxx);
    return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
long long int x,y,l,r,a[100000];
long long int power(long long int a,long long int b)
{
    long long int ans=1;
    for(;b;b>>=1)
    {
        if(b&1)
        {
            ans=(long long)ans*a;
        }
        a=(long long)a*a;
    }
    return ans;
}
int main()
{
    cin>>x>>y>>l>>r;
    int cnt=1;
    for(int i=0;i<=100;i++)
    {
        for(int j=0;j<=100;j++)
        {
            long long int num=power(x,i)+power(y,j);
            if(num<=r&&num>=l)
            {
                a[cnt++]=num;
            }

        }
    }
    sort(a+1,a+cnt);
    cnt--;
   // for(int i=1;i<=cnt;i++)
    //{
    //    cout<<a[i]<<endl;
    //}
    if(cnt==0)
    {
        cout<<r-l+1<<endl;
    }
    if(cnt==1)
    {
        cout<<max(r-a[1],a[1]-l);
    }
    if(cnt>1)
    {
        long long int mmax=0;
        for(int i=2;i<=cnt;i++)
        {
            long long int zj=a[i]-a[i-1]-1;
            mmax=max(zj,mmax);
        }
        mmax=max(mmax,a[1]-l);
        mmax=max(mmax,r-a[cnt]);
        cout<<mmax<<endl;
    }
}

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
using namespace std;

int main()
{
	ll x,y,l,r;
	cin >> x >> y >> l >> r;
	ll ans = 0;
	map<ll, bool> mp;
	std::vector<ll> v;
	ll x1=1, y1=1;
	while(x1 <= r)
	{
		y1 = 1;
		while(x1 + y1 <= r)
		{
			ll t = x1 + y1;
			//cout << t << endl; //
			if(t >= l && t<=r && !mp[t])
			{
				//cout << t <<endl;//
				mp[t] = 1;
				v.pb(t);
				//ans--;
			}
			y1 *= y;
			if(y1 < 0)
				break;
		}
		x1 *= x;
		if(x1 < 0)
			break;
	}
	//cout << ans << endl;
	//v.pb(l);
	//v.pb(r);
	sort(v.begin(), v.end());
	if(v.size() >= 1) {
		if(!mp[l])
			ans = v[0] - l;

		for(int i=0; i<v.size()-1; i++)
		{
			ans = max(ans, v[i+1] - v[i] - 1);
		}
		if(!mp[r])
			ans = max(ans, r - v[v.size()-1]);
	}
	cout << ans << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long x, y, l, r, pox, poy, c, sum, mx;
vector<long long> v;
map<long long,long long> mp;
int main() {
	scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
	v.push_back(l - 1);
	for(int i = 0; i < 1000; ++i){
		pox = pow(x,i);
		if(pox > r)
			break;
		for(int j = 0; j < 1000; ++j){
			poy = pow(y,j);
			sum = pox + poy;
			if(sum > r)
				break;
			if(sum >= l && sum <= r && !mp[sum])
				v.push_back(sum);
			mp[sum]++;
		}
	}
	v.push_back(r + 1);
	sort(v.begin(),v.end());
	for(int i = 0 ;i < v.size() - 1; ++i)
		mx = max(mx,v[i + 1] - v[i] - 1);
	printf("%lld",mx);
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
vector<ll> g, xp, yp;
ll x, y, l, r;

int main()
{
    cin >> x >> y >> l >> r;

    xp.push_back(1);
    yp.push_back(1);

    while (log(x) + log(xp.back()) < 63)
        xp.push_back(x * xp.back());
    while (log(y) + log(yp.back()) < 63)
        yp.push_back(y * yp.back());

    for (auto i : xp)
        for (auto j : yp)
            g.push_back(i + j);

    sort(g.begin(), g.end());
    g.erase(unique(g.begin(), g.end()), g.end());

    //cout << r - *(upper_bound(g.begin(), g.end(), r) - 1) << endl;
    ll mx = max(
        *lower_bound(g.begin(), g.end(), l) - l,
        r - *(upper_bound(g.begin(), g.end(), r) - 1));
    //cout << mx << endl;
    for (int i = lower_bound(g.begin(), g.end(), l) - g.begin() + 1;
         i < upper_bound(g.begin(), g.end(), r) - g.begin(); ++i)
        mx = max(g[i] - g[i - 1] - 1, mx);
    cout << mx << endl;
}

#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"

using namespace std;

#ifndef ONLINE_JUDGE
  #include "debug.hpp"
  #define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
   #define debug(x...)
#endif

int main(){
  #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
  #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  unsigned long long x,y,l,r;
  cin >> x >> y >> l >> r;
  unsigned long long powa = 1,powb = 1;
  set <long long> s;
  while(true){
  	powb = 1;
  	while(true){
  		if((powa + powb) >= l && (powa + powb) <= r){
  			s.insert(powa + powb);
  		}
  		if(powb <= (r/y) && ((powa + powb) <= r)){
  			powb *= y;
  		}
  		else{
  			break;
  		}
  	}
  	if(powa <= (r/x)){
  		powa *= x;
  	}
  	else{
  		break;
  	}
  }
  s.insert(l);
  s.insert(r + 1);
  long long prev = l,ans = 1;
  for(long long x : s){
  	ans = max(x - prev - 1,ans);
  	prev = x;
  }
  cout << ans;
  return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

unsigned long long max_n = 1e18;

int main() {
    unsigned long long x, y, l, r;
    cin >> x >> y >> l >> r;
    vector<unsigned long long> Xa;
    vector<unsigned long long> Yb;
    unsigned long long t = 1;
    while(t <= r){
        Xa.emplace_back(t);
        t *= x;
    }
    t = 1;
    while(t <= r){
        Yb.emplace_back(t);
        t *= y;
    }
    typedef unsigned long long ull;
    set<ull> unlucky;
    for(auto i1 : Xa){
        for(auto i2: Yb){
            if(i1 + i2 >= l && i1 + i2 <= r){
                unlucky.insert(i1 + i2);
            }
        }
    }
    unlucky.insert(r+1);
    ull left = *unlucky.begin();
    ull ans = left-l;
    bool first = true;
    for(auto d: unlucky){
        if(first){
            first = false;
            continue;
        }
        ans = max(ans, d-left-1);
        left = d;
    }
    cout << ans << endl;
    return 0;
}

#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define ll long long
ll a[10005];
int main()
{
    ll n,m;
    ll sum;
    ll x,y,l,r;
    while(~scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r))
    {
        n=1,m=1;
        ll z;
        ll temp;
        int num=1;
        while(1)
        {
            z=r-n;
            while(1)
            {
                if(r-m<n)
                    break;
                temp=n+m;
                if(temp>=l&&temp<=r)
                    a[num++]=temp;
                if(temp/y>m-1)
                    m*=y;
                else
                    break;
            }
            m=1;
            if(r/x>n-1)
                n*=x;
            else
                break;
        }
        sort(a+1,a+num);
        a[0]=l-1;
        a[num]=r+1;
//        for(int i=0;i<=num;i++)
//            printf("%I64d ",a[i]);
        puts("");
        ll ans=0;
        for(int i=1;i<num+1;i++)
            if(ans<a[i]-a[i-1])
            ans=a[i]-a[i-1];
        printf("%I64d\n",ans-1);
    }
}

#include <bits/stdc++.h>

using namespace std;
vector<long long>xx,yy,ans;
set <long long> s;
set <long long> ::iterator it;
int main()
{
    long long  x,y,l,r,res=1;
    cin>>x>>y>>l>>r;
    long long m=r-l;
    while(res<=r&&res>0)
    {
        xx.push_back(res);
        res*=x;
    }
    res=1;
    while(res<=r&&res>0)
    {
        yy.push_back(res);
        res*=y;
    }
    bool z=false;
    for(int i=0;i<xx.size();i++)
        for(int j=0;j<yy.size();j++)
        if(xx[i]+yy[j]>=l&&xx[i]+yy[j]<=r)
        s.insert(xx[i]+yy[j]);
        if(s.size()==0)
       cout<<1<<endl;
       else
       {
        long long  maxx=0;
        it=s.begin();
        long long bef=*it;
        maxx=max(maxx,bef-l);
        for(;it!=s.end();it++)
        {
            ans.push_back(*it-bef-1);
            maxx=max(maxx,*it-bef-1);
            bef=*it;
        }
        maxx=max(maxx,r-bef);
        cout<<maxx<<endl;
       }

        return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
const ll inf=(1LL<<60);
const double pi=acos(-1);
ll powmod(ll a,ll b) {ll res=1; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
inline void pcas(int ca) {printf("Case %d: ",ca);}
const int maxn=1e5+10;
ll vis[maxn];
int main()
{
    ll x,y,l,r;
    int k=0,a,b;
    cin>>x>>y>>l>>r;
    vis[k++]=x;
    vis[k++]=y;
    rep(i,0,(1e18/x)  ) {
        ll t1=powmod(x,i);
        if(t1>r||t1<=0) break;
        rep(j,0,(1e18/y) ) {
            ll t2=powmod(y,j);
            if(t2>r||t2<=0) break;
            ll temp=powmod(x,i)+powmod(y,j);
            if(temp>=l&&temp<=r)
            vis[k++]=temp;
        }
    }
    sort(vis,vis+k);
    int sz=unique(vis,vis+k)-vis;
    ll ans=0;
    rep(i,1,sz-1) {
        ans=max(vis[i]-vis[i-1]-1,ans);
    }
    ans=max(ans,vis[0]-l);
    ans=max(ans,r-vis[sz-1]);
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
const ll MOD = 1e9 + 7 ;
#define pb push_back
#define all(v) v.begin(),v.end()
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll n,m,k,l,y,x,z=0,ans=0; string s ; bool ok ;
vector < vector < ll > > g;
ll mxdp[202020];
ll vis[202020];

ll pow(ll a, ll b) {
				    ll ret = 1;
				    while (b) {
					if (b & 1) ret = ret * a;
					a = a * a;
					b >>= 1;
				    }
				    return ret;
				}

void solve()
{
ll x,y,l,r;
   cin>>x>>y>>l>>r;
   set < ll > st ;
   for(ll a=0;a<60;a++)
    {
        ll pwa=pow(x,a);

        if(pwa > r && pwa>0) break ;
        for(ll b=0;;b++)
            {
            ll pwb=pow(y,b);
           // cout<<pwb<<"\n";
            if(pwb >r || pwa+pwb>r || pwa+pwb<0) break;
            if(pwa+pwb>=l)st.insert(pwa+pwb);

            }

    }

    ans = 0 ;
    vector < ll > v ;

    for(auto u : st) v.pb(u);
    if(v.size()==0)
        {
            cout<<r-l+1;
            return;

        }
    ans=max(v[0]-l,r-v[v.size()-1]);

    for(int i=0;i<v.size()-1;i++)
        ans=max(ans,v[i+1]-v[i] - 1);

    cout<<ans<<'\n';

 }
int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("test.in","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;

     solve();

    return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll x, y, l, r, powx[64], powy[64], xlen, ylen, z[4096], zlen, ans, starti, endi;

int main() {
	xlen = 1;
	scanf("%lld%lld%lld%lld", &x, &y, &l, &r);
	powx[0] = powy[0] = 1;
	while(log10(powx[xlen-1]) + log10(x) <= pow(10, 18)) {
		powx[xlen] = powx[xlen-1]*x;
		xlen++;
	}

	ylen = 1;
	while(log10(powy[ylen-1]) + log10(y) <= pow(10, 18)) {
		powy[ylen] = powy[ylen-1]*y;
		ylen++;
	}
	zlen = 0;

	for(int i = 0; i < xlen; i++) {
		for(int j = 0; j < ylen; j++) {
			z[zlen++] = powx[i] + powy[j];
		}
	}
	sort(z, z + zlen);
	for(int i = 1; i < zlen; i++) {
		if(z[i-1] >= l && z[i-1] <= r) {
			starti = z[i-1];
			endi = (z[i] > r) ? r : z[i] - 1;
			ans = max(endi - starti, ans);
		}
		if(z[i] >= l && z[i] <= r) {
			starti = (z[i-1] < l)? l : z[i-1] + 1;
			endi = z[i];
			ans = max(endi - starti, ans);
		}
	}
	printf("%lld\n", ans);
}

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=4000+10,maxn=1234567,inf=11111;

ll ans[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll x,y,l,r,cnt=0;
    cin>>x>>y>>l>>r;
    for(int i=0;i<=100;i++)
        for(int j=0;j<=100;j++)
        {
            ll p=pow(x,i)+pow(y,j);
            if(p<0)continue;
            ans[cnt++]=p;
        }
    sort(ans,ans+cnt);
    ll k=0;
    while(ans[k]<l)k++;
    ll maxx=ans[k]-l;
    for(;k<cnt;k++)
    {
       // cout<<ans[k]<<endl;
        if(ans[k]>=r)break;
        maxx=max(maxx,ans[k]-ans[k-1]-1);
    }
    if(ans[k]==r)maxx=max(maxx,ans[k]-ans[k-1]-1);
    else if(k>=1)maxx=max(maxx,r-ans[k-1]);
    cout<<min(maxx,r-l+1)<<endl;
    return 0;
}

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#include<unordered_map>
#define ll long long
#define lowbit(x) (x)&(-x)
#define C(n,m) (n<m?0:1ll*fac[n]*invfac[n-m]%mod*invfac[m]%mod)
using namespace std;
const ll maxn = LLONG_MAX;

vector<ll> v1;
vector<ll> v2;
vector<ll> v3;

int main()
{
	ios::sync_with_stdio(false);

	ll x; ll y; ll L; ll R;
	cin >> x >> y >> L >> R;
	ll p1 = 1;
	while (1)
	{
		v1.push_back(p1);
		if (p1 <= maxn / x)
			p1 *= x;
		else
			break;
	}

	while (1) {
		v2.push_back(p1);
		if (p1 <= maxn / x)
			p1 *= y;
		else
			break;
	}
	for (ll i : v1)
		for (ll j : v2)
			v3.push_back(i + j);

	sort(v3.begin(), v3.end());
	v3.erase(unique(v3.begin(), v3.end()), v3.end());

	ll ans = -1;
	v3.push_back(L); v3.push_back(R);
	sort(v3.begin(), v3.end());
	for (int i = 0; i < v3.size(); i++)
	{
		if (i + 1 < v3.size() && L <= v3[i] && v3[i] <= R && L <= v3[i + 1] && v3[i + 1] <= R)
			ans = max(ans, v3[i + 1] - v3[i] - 1);
	}

	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int long long
typedef double ld;
typedef unsigned long long ull;
typedef pair < int, int > pii;
typedef vector < int > vi;
typedef vector < pii > vii;
typedef map < int, int > mii;
typedef unordered_map < int , bool > mib;

const int mod = 1e9+7;
const int inf = 1e17;
const ld pi = acos(-1);
const ld eps = 0.000001;

#define iosbase ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define float_write(a) cout.precision(a); cout<<fixed;
#define files_in(x) freopen(x ".in", "r", stdin); freopen(x ".out", "w", stdout);
#define files_dat(x) freopen(x ".dat", "r", stdin); freopen(x ".sol", "w", stdout);
#define files_txt(x) freopen(x ".txt", "r", stdin); freopen(x ".txt", "w", stdout);
#define filestxt freopen("output.txt", "w", stdout); freopen("input.txt", "r", stdin);
#define slater(x) (x>='a'&&x<='z')
#define blater(x) (x>='A'&&x<='Z')
#define number(x) (x>='0'&&x<='9')
#define ms(x, y) memset(x, y, sizeof(x));
#define all(a) a.begin(),a.end()
#define ppb pop_back
#define pb push_back
#define mp make_pair
#define y1 yyyyyy
#define sz size()
#define s second
#define f first

template < typename type >

inline type sqr(type value)
{
    return (value*value);
}

int x,y,a,b,ans;
vi v;
mib u;

main()
{
    cin>>x>>y>>a>>b;

    for(int xx=1 ; xx<b; xx*=x)
    {
        for(int yy=1; (b-xx)>=yy; yy*=y)
        {
            if(!u[xx+yy])
            {
                u[xx+yy]=1;
                v.pb(xx+yy);
            }
        }
    }

    sort(all(v));

    if(v[0]>a)
    {
        ans=v[0]-a-1;
    }

    for(int i=1; i<v.sz; i++)
    {
        ans=max(ans,v[i]-max(a,v[i-1])-1);
    }

    ans=max(ans,b-v[v.sz-1]);

    cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) int((a).size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
template<class T, class U> inline void add_self(T &a, U b){a += b;if (a >= mod) a -= mod;if (a < 0) a += mod;}
template<class T, class U> inline void min_self(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void max_self(T &x, U y) { if (y > x) x = y; }

#define _deb(x) cout<<x;
void _print() {cerr << "]\n";} template <typename T, typename... V>void _print(T t, V... v) {_deb(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
template <class T, class U> void print_m(const map<T,U> &m, int w=3){if(m.empty()){cout<<"Empty"<<endl; return;}for(auto x: m)cout<<"("<<x.first<<": "<<x.second<<"),"<<endl;cout<<endl;}
template<class T, class U>void debp(const pair<T, U> &pr, bool end_line=1){cout<<"{"<<pr.first<<" "<<pr.second<<"}"; cout<<(end_line ? "\n" : ", ");}
template <class T> void print_vp(const T &vp, int sep_line=0){if(vp.empty()){cout<<"Empty"<<endl; return;}if(!sep_line) cout<<"{ ";for(auto x: vp) debp(x,sep_line);if(!sep_line) cout<<"}\n";cout<<endl;}
template <typename T>void print(const T &v, bool show_index = false){int w = 2;if(show_index){for(int i=0; i<sz(v); i++)cout<<setw(w)<<i<<" ";cout<<endl;}for(auto &el: v) cout<<setw(w)<<el<<" ";cout<<endl;}
template <typename T>void print_vv(const T &vv){if(sz(vv)==0) {cout<<"Empty"<<endl; return;} int w = 3;cout<<setw(w)<<" ";for(int j=0; j<sz(*vv.begin()); j++)cout<<setw(w)<<j<<" ";cout<<endl;int i = 0;for(auto &v: vv){cout<<i++<<" {";for(auto &el: v) cout<<setw(w)<<el<<" ";cout<<"},\n";}cout<<endl;}

void make_uniq(vl &a){
    sort(all(a));
    auto pos = unique(all(a));
    a.resize(distance(a.begin(),pos));
}

const ll nax = LONG_LONG_MAX;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll x, y, l, r;
    while(cin>>x>>y>>l>>r){
        vl px{1,x}, py{1,y};
        while(px.back()<=nax/x){
            px.pb(px.back()*x);
        }
        while(py.back()<=nax/y){
            py.pb(py.back()*y);
        }

        vl a;
        forn(i,sz(px)){
            forn(j,sz(py)){
                if(px[i]<=nax-py[j]){
                    a.pb(px[i]+py[j]);
                }
            }
        }

        a.pb(0);
        make_uniq(a);

        // print(a);

        ll ans = 0;
        for(int i=1; i<sz(a); ++i){
            if((a[i-1]<=l && a[i]<=l) || (a[i-1]>=r && a[i]>=r)) continue;
            ll lt = max(l-1, a[i-1]);
            ll rt = min(r+1, a[i]);
            // deb(lt, rt);
            ll cur = rt - lt - 1;
            max_self(ans, cur);
        }
        cout<<ans<<"\n";
    }
    return 0;
}

//hell_hacker
//IT TAKES EVERYTHING and IT IS NOT LUCK
//PICK ME LAST -- COME OUT OF N^WHERE

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)

#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define yolo "debug_statement"

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

const ll inf = 1e9;
const ld eps = 1e-9;
const ld pi=acos(-1);
const ll mod=1000000007;

ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

#if !DEBUG && !ONLINE_JUDGE

    #define debug(args...)     (Debugger()) , args

    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}

        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };

    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator iii = v.begin(); iii != v.end(); ++iii)
        {
            if(!first)
                os << ", ";
            os << *iii;
            first = false;
        }
        return os << "]";
    }

    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator iii = v.begin(); iii != v.end(); ++iii)
        {
            if(!first)
                os << ", ";
            os << *iii ;
            first = false;
        }
        return os << "]";
    }

#else
    #define debug(args...)                  // Just strip off all debug tokens
#endif

int main(){
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll x, y, l, r, n, i, j;
    cin >> x >> y >> l >> r;
    vector<ll>px;
    vector<ll>py;
    i = 0;

    while(pow(x,i) > 0 && pow(x,i) <= 1e18){
        px.pb(round(pow(x,i)));
        i++;
    }
    i = 0;
    while(pow(y,i) > 0 && pow(y,i) <= 1e18){
        py.pb(round(pow(y,i)));
        i++;
    }

    vector<ll>in;
    in.pb(l-1);
    in.pb(r+1);

    fo(i,sz(px)){
        fo(j,sz(py)){
            ll sum = px[i] + py[j];
            if(sum >= l && sum <= r)
                in.pb(sum);
        }
    }

    //fo(i,sz(px))
    //    cout << px[i] << " " ;
    //cout << endl;

    sort(all(in));

    ll maxer = 0;

    for(i=1;i<sz(in);++i)
        if((in[i] - in[i-1]) > maxer){
            maxer = in[i] - in[i-1] - 1;
        }

    if(sz(in)==2)
        maxer = 0;
    cout << maxer;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define scanfd1(x) scanf("%d", &x)
#define scanfd2(x,y) scanf("%d %d", &x, &y)
#define scanfd3(x,y,z) scanf("%d %d %d", &x, &y, &z)
#define scanfd4(x,y,z,w) scanf("%d %d %d %d", &x, &y, &z, &w)

#define scanfll1(x) scanf("%lld", &x)
#define scanfll2(x,y) scanf("%lld %lld", &x, &y)
#define scanfll3(x,y,z) scanf("%lld %lld %lld", &x, &y, &z)
#define scanfll4(x,y,z,w) scanf("%lld %lld %lld %lld", &x, &y, &z, &w)

#define fr(i,n) for(int i=0;i<(n);i++)
#define frr(i,a,b) for(int i=(a);i<=(b);i++)
#define frrs(i,a,b) for(int i=(a);i>=(b);i--)
#define VAR(a,b) __typeof(b) a=b
#define frit(it,c) for(VAR(it,(c).begin());it!=(c).end();it++)

typedef long long ll;
typedef long double ld;

#define pb push_back

#define all(a) a.begin(),a.end()

#define fi first
#define se second
typedef pair<int,int> pii;

#define PI acos(-1)
#define MOD 1000000007

//LONG_LONG_MAX
//-DBL_MAX

#define prin(a) cout << #a << " = " << (a) << endl

int main(){
	ll a,b, l, r;

	cin >> a >> b >> l >> r;

	ll cura = 1, curb = 1;

	vector<ll> va, vb;

	while(cura<r){
		va.pb(cura);
		if( (cura*a)/a!=cura) break;
		cura*=a;
	}

	while(curb<r){
		vb.pb(curb);
		if( (curb*b)/b!=curb) break;
		curb*=b;
	}

	vector<ll> v;

	fr(i,va.size()){
		fr(j,vb.size()){
			ll cur = va[i]+vb[j];

			if(cur>0){
				v.pb(cur);
			}
		}
	}

	v.pb(r+1);

	sort(all(v));

	unique(all(v));

	ll maxl = 0;

	fr(i,v.size()){
		if(v[i]<l) continue;

		ll ant;
		if(i) ant = v[i-1];
		else ant = l-1;

		ll cur = min(r+1,v[i])-max(l-1,ant)-1;
		maxl = max(maxl,cur);
	}

	cout << maxl << endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 1000007; //10e6

#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

set <ll> st;
vector <ll> X;
vector <ll> Y;

int main() {
	ll x, y, l, r;
	ll sx, sy;

	scanf("%lld %lld", &x, &y);
	scanf("%lld %lld", &l, &r);

	sx = 1;
	sy = 1;
	while (sx <= r) {
		X.pb(sx);
		sx *= x;
	}

	while (sy <= r) {
		Y.pb(sy);
		sy *= y;
	}

	for (int i = 0; i < X.size(); i++) {
		for (int j = 0; j < Y.size(); j++) {
			st.insert(X[i] + Y[j]);
		}
	}
/*
	printf("SET\n");
	for (auto u : st) {
		printf("%lld ", u);
	}
	printf("\n");*/

	bool dentro = false;
	ll last = -1;
	ll maxi = 0;
	for (auto u : st) {
		if (u >= l) dentro = true;
		if (u > r) {
			maxi = max(maxi, r - last);
			break;
		}

		if (dentro) {
			if (last == -1) {
				maxi = max(maxi, u - l);
				last = u;
			}
			else {
				maxi = max(maxi, u - last - 1);
				last = u;
			}
		}
	}

	if (r > last) maxi = max(maxi, r - last);

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

	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main(){
	long long x, y, l, r;
	cin >> x >> y >> l >> r;

	priority_queue<long long> Q;
	long long tx = 1, ty = 1;
	for(int a = 0; a < 64; a++){
		if(a != 0 && tx > r / x) break;
		if(a == 0) tx = 1;
		else tx *= x;
		ty = 1;
		for(int b = 0; b < 64; b++){
			if(b != 0 && ty > (r - tx) / y) break;
			if(b == 0) ty = 1;
			else ty *= y;
			if(tx + ty >= l && tx + ty <= r) Q.push(tx + ty);
		}
	}

	long long pre = r;
	if(!Q.empty() && Q.top() != r) pre = r + 1;
	long long pos = 0;
	long long ans = 0;
	while(!Q.empty()){
		pos = Q.top(); //cout << pos << endl;
		Q.pop();
		ans = max(ans, pre - pos - 1);
		pre = pos;
	}
	if(pre != l) ans = max( ans, pre - l);

	cout << ans << endl;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Khela ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pf printf
#define sf scanf
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define pii pair<int ,int>
#define PII pair<ll ,ll>

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}
const int Max=1e9+100;
const ll  MAX=1e18+500;
const int sz=1e3+100;

int main()
{
    Khela
    ll a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,n,mx;
    char ch;
    double d;
    string str1,str2,str;
    bool bl,bl1;
    int t,cs=1;
    vector<ll> ne,de,test;
    while(cin>>x>>y>>l>>r)
    {
        ne.clear(),de.clear();
        ne.push_back(1ll),de.push_back(1ll);
        ll temp=x;
        ne.push_back(x),de.push_back(y);
        while(temp<=(MAX/x)) {
            temp*=x;
            ne.push_back(temp);
        }
        temp=y;
        while(temp<=(MAX/y)){
            temp*=y;
            de.push_back(temp);
        }

        test.clear();
        for(auto xx: ne ) for(auto yy: de) test.push_back(xx+yy);
        sort(test.begin(),test.end());
        test.erase(unique(test.begin(),test.end()),test.end());

        ll mx=0;
        bool bl=false;
        ll siz=test.size();
        for(i=0;i<siz;i++){
            if(!bl && test[i]>=l && test[i]<=r){
                bl=true;
                mx=max(mx,test[i]-l);
            }
            else if(bl && test[i]<=r){
                 mx=max(mx,test[i]-test[i-1]-1);
            }
            else if(i-1>=0 && test[i-1]<=r && test[i-1]>=l) mx=max(mx,r-test[i-1]);
        }

        cout<<mx<<endl;
    }

}

#include <bits/stdc++.h>

#define int_128 long long
using namespace std;

int_128 a[105], b[105];
vector<int_128> ve;
int_128 ans;

int main()
{
    int_128 x, y, l, r;
    cin>>x>>y>>l>>r;
    a[0] = b[0] = 1;
    int cnt1, cnt2;
    cnt1 = cnt2 = 0;
    for(int i = 1; i <= 100 && a[i-1] <= 1e18/x; i++)
    {
        a[i] = a[i-1]*x;
        cnt1++;
    }
    for(int i = 1; i <= 100 && b[i-1] <= 1e18/y ; i++)
    {
        b[i] = b[i-1]*y;
        cnt2++;
    }
    //cout<<cnt1<<" "<<cnt2<<endl;
    for(int i = 0; i <= cnt1; i++)
    {
        for(int j = 0; j <= cnt2; j++)
        {
            //cout<<1<<endl;
            if(a[i] <= 1e18-b[j]) ve.push_back(a[i]+b[j]);
        }
    }
    sort(ve.begin(), ve.end());
    auto it = unique(ve.begin(), ve.end());
    ve.erase(it, ve.end());
    //cout<<ve[0]<<endl;
    for(int i = 1; i < ve.size(); i++)
    {
        //cout<<ve[i]<<" "<<ve[i]-ve[i-1]<<endl;
        if(ve[i-1] >= l && ve[i] <= r)ans = max(ve[i] - ve[i-1]-1,ans);
    }
    ans = max(r-ve[ve.size()-1], ans);
    cout<<ans<<endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <sstream>
#include <set>
#include <cmath>
#include <map>
using namespace std;
vector <long long> a,b,c;
map<unsigned long long ,int> check;
void solve(long long x,long long y){
    long long res=1;
    while(res<1e18/x)
    {
        a.push_back(res);
        res*=x;
    }
    res=1;
    while(res<1e18/y)
    {
        b.push_back(res);
        res*=y;
    }
    for(long long i:a)
    {
        for(long long j:b)
        {
            if(check.find(i+j)==check.end()&&i+j<=1e18)
                c.push_back(i+j);
            check[i+j]++;
        }
    }
    sort(c.begin(),c.end());

}
int main()
{
    long long x,y,l,r,mi=1e18+9;
    cin>>x>>y>>l>>r;

    solve(x,y);/*
    for(long long i:a)
        cout<<i<<' ' ;
    cout<<"\n";
    for(long long i:b)
        cout<<i<<' ' ;
    for(long long i:c)
        cout<<i<<' ' ;
    cout<<"\n\n\n\n\n";*/
    //conner
    if(c[0]-l>0)
        mi=min(c[0]-l,mi);
    if(r-c[(int)c.size()-1]>0)
        mi=min(r-c[(int)c.size()-1],mi);
    for(int i=1;i<c.size();i++)
        if(c[i]-c[i-1]+1<mi)
            mi=c[i]-c[i-1]+1;
    cout<<mi;
}

#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair <ll,ll>
using namespace std;
vector <ll>v;
ll w[100003];
map <ll,ll>m1,m2;
ll x,y,a,b,c,n,m,k,l,r;
ll poww(ll x1,ll x2){
    if (x1==1)return 1;
    if (x2==1)return x1;
    if (x2==0)return 1;
    ll d=poww(x1,x2/2);
    if (x2%2==0)return d*d;
    return x1*d*d;
}
int main()
{
    cin >>x>>y>>l>>r;
    for (int i=0;i<=60;i++){
        for (int j=0;j<=60;j++){
            if (poww(x,i)+poww(y,j)>=l&&poww(x,i)+poww(y,j)<=r){v.push_back(poww(x,i)+poww(y,j));}
        }
    }
    sort (v.begin(),v.end());
    m=0;
    for (int i=1;i<v.size();i++){
        m=max(m,v[i]-v[i-1]-1);
    }
    cout <<max(max(m,r-v[v.size()-1]),v[0]-l);
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<ll,ll>mp;
int main(){
   ll x,y,l,r;
ll xp=0,yp=0;
    cin>>x>>y>>l>>r;
    ll sx=x;
    ll sy=y;
    while(sx<r){
        xp++;
        sx=pow(x,xp);
    }
    while(sy<r){
        yp++;
        sy=pow(x,yp);
    }
    ll cot=0;
    ll i,j;
    ll nsum;
    vector<ll>v;
    for(i=0;i<xp;i++){
        for(j=0;j<yp;j++){
            nsum=pow(x,i)+pow(y,j);
            if(nsum>=l&&nsum<=r&&mp[nsum]==0){
                 if(nsum>=l&&nsum<=r){
                    if(mp[nsum]==0){cot++;
                      mp[nsum]=1;
                      v.push_back(nsum);
                    }
                 }
            }
            if(nsum>r)break;
        }
    }
    if(cot==(r-l)+1)cout<<0<<endl;
    else{
         ll ans=0;
         cot=0;
         sort(v.begin(),v.end());
         ans=abs(l-v[0]);
         for(i=1;i<v.size();i++){
                ll d=v[i]-v[i-1]-1;
                if(d>ans)ans=d;
         }
         if(r-v[v.size()-1]>ans)ans=r-v[v.size()-1];
        cout<<ans<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main(){
	long long x, y, l, r;
	cin >> x >> y >> l >> r;

	priority_queue<long long> Q;
	long long tx = 1, ty = 1;
	for(int a = 0; a < 64; a++){
		if(tx > r / x) break;
		if(a == 0) tx = 1;
		else tx *= x;
		ty = 1;
		for(int b = 0; b < 64; b++){
			if(ty > (r - tx) / y) continue;
			if(b == 0) ty = 1;
			else ty *= y;
			if(tx + ty >= l && tx + ty <= r) Q.push(tx + ty);
		}
	}
	long long pre = r;
	if(Q.top() != r) pre = r + 1;
	long long pos;
	long long ans = 0;
	while(!Q.empty()){
		pos = Q.top(); //cout << pos << endl;
		Q.pop();
		ans = max(ans, pre - pos - 1);
		pre = pos;
	}
	if(pre != l) ans = max( ans, pre - l);

	cout << ans << endl;
}

#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define clears(a,x) memset(a,x,sizeof(a))
#define INF 0x3f3f3f3f
#define read(x) scanf("%d",&x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
typedef long long ll;
const int N = 1e4+5;
ll poww(ll a,ll b){
    ll ans = 1,base = a;
    while(b != 0){
        if(b & 1 != 0)
            ans *= base;
        base *= base;
        b >>= 1;
    }
    return ans;
}
ll cnt = 0;
ll a[N];

ll check(ll x,ll y){
    ll cnt = 0;
    while(1){
        if(poww(x,cnt) <= y)
            cnt++;
        else    break;
    }
    return cnt;
}

int main(){
    ll x,y,l,r;
    ll ans = 0;
    cin >> x >> y >> l >> r;
    for( int i = 0 ; i <= check(x,r) ; i++ ){
        int m = check(y,r-poww(x,i));
        for( int j = 0 ; j <= m ; j++ ){
            ll tot = poww(x,i)+poww(y,j);
            if(tot >= l && tot <= r)
                a[++cnt] = tot;
        }
    }
    sort(a+1,a+1+cnt);
    ll num = unique(a+1,a+1+cnt) - a - 1;
    ll now = l;
    for( int i = 1 ; i <= num ; i++ ){
        ans = max(ans,a[i]-now);
        now = a[i]+1;
    }
    ans = max(ans,r-a[num]);
    cout << ans << endl;
    return 0;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
unsigned long long a[700],b[700],c[40000];
unsigned long long maxx=1e18;
int main()
{
	unsigned long long x,y,l,r;
	scanf("%I64u%I64u%I64u%I64u",&x,&y,&l,&r);
	a[0]=1;b[0]=1;
	int la,lb,lc=0;
	for(int i=1;;i++)
	{
		if(maxx/a[i-1]<x)
		{
			la=i;
			break;
		}
		a[i]=a[i-1]*x;
		if(a[i]>=r)
		{
			la=i;
			break;
		}
	}
	for(int i=1;;i++)
	{
		if(maxx/b[i-1]<y)
		{
			lb=i;
			break;
		}
		b[i]=b[i-1]*y;
		if(b[i]>=r)
		{
			lb=i;
			break;
		}
	}
	for(int i=0;i<la;i++)
	    for(int j=0;j<lb;j++)
	        if(maxx-a[i]>=b[i]&&a[i]+b[j]<=r&&a[i]+b[j]>=l)
	            c[lc++]=a[i]+b[j];
	sort(c,c+lc);
	unsigned long long ans=0;
	for(int i=1;i<lc;i++)
	{
		unsigned long long a;
		a=c[i]-c[i-1];
		if(a>ans)ans=a-1;
	}
	if(lc==0)
	{
		if(r==l)ans=1;
		else ans=r-l;
	}
	else
	{
		if(c[lc-1]!=r)
    	{
    		unsigned long long a;
    		a=r-c[lc-1];
    		if(a>ans)ans=a;
    	}
    	if(c[0]!=l)
    	{
    		unsigned long long a;
    		a=c[0]-l;
    		if(a>ans)ans=a;
    	}
	}
	printf("%I64u\n",ans);
	return 0;
}

//bayemirov
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define SZ(x) ((int) (x).size())
#define F first
#define S second
#define pb push_back
#define sqr(x) (x) * (x)

void tachyon() {ios_base::sync_with_stdio(0); cin.tie(nullptr);}

ll x, y, l, r, ans;
set<ll> points;

bool inRange(ll s) {return s >= l && s <= r;}

ll bp(ll a, int b) {
    if (!b)
        return 1;
    if (b % 2)
        return bp(a, b - 1) * a;
    else
        return sqr(bp(a, b / 2));
}

void solve() {
    cin >> x >> y >> l >> r;
    int a = log(r) / log(x);
    int b = log(r) / log(y);
    for (int i = 0; i <= a; i++) {
        for (int j = 0; j <= b; j++) {
            ll s = bp(x, i) + bp(y, j);
            if (inRange(s))
                points.insert(s);
        }
    }
    if (!SZ(points))
        cout << 0, exit(0);
    if (SZ(points) == 1) {
        if (*points.begin() == l)
            cout << 0, exit(0);
        cout << 1, exit(0);
    }
    auto e = points.end();
    --e;
    ans = max(ans, max(*points.begin() - l, r - *e));
    for (set<ll>::iterator i = points.begin(); i != e; i++)
        ans = max(ans, *(++i) - *(--i) - 1);
    cout << ans;
}

int main() {
    tachyon();
    solve();
    return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
#define N 5000005
#define mod 1000000007
#define mem(a,x) memset(a,x,sizeof(a))
#define ll long long
#define exp 1e-8
#define lowbit(x) (x&-x)
#define rep(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
ll x ,y ,l ,r;
vector <ll> v;
int main( ){
    ll tx=1 ,ty=1;
    ll sqx = (1e18)/x+2;
    ll sqy = (1e18)/y+2;
    cin>>x >>y >>l >>r;
    rep (i ,0 ,62){
        ty = 1;
        if(i)tx*=x;
        if( tx>r )break;
        rep( j,0  ,62 ){
        if(j)ty*=y;
        //cout<<tx+ty<<endl;
        if( tx+ty>r )break;
        if(tx+ty>=l && tx+ty<=r)v.push_back( ty+tx );
        if( ty>sqy)break;
        }
        if( tx>sqx)break;
    }
    v.push_back(l-1);
    v.push_back(r+1);
    ll ans = 0;
    sort( v.begin() ,v.end() );
    int sz = v.size( );
    rep( i ,1 ,sz-1 ){
        //cout<<v[i]<<endl;
    if( v[i]!=v[i-1])ans = max(ans ,v[i]-v[i-1]-1 );
    }

    cout<<ans;
}

#include<bits/stdc++.h>
using namespace std;
vector<long long> v;
vector<long long> V;
int main()
{
    long long x,y,l,r;
    while(~scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r))
    {
        int k1=max((int)(log(r)*1.0/log(x)),1);
        int k2=max((int)(log(r)*1.0/log(y)),1);
        v.clear();
        V.clear();
        for (int i=0; i<=k1; i++)
        {
            for (int j=0; j<=k2; j++)
            {
                V.push_back(pow(x,i)+pow(y,j));
            }
        }
        sort(V.begin(),V.end());
        for (int i=0; i<V.size(); i++)
        {
            if (V[i]<=r&&V[i]>=l)
            {
                if (v.empty()||(v[v.size()-1]!=V[i]))
                    v.push_back(V[i]);
            }
        }
         /*for (int i=0; i<v.size(); i++)
             printf("%I64d\n",v[i]);*/
        long long nmax=0;
        for (int i=0; i<v.size()-1; i++)
        {
            if (nmax<v[i+1]-v[i]-1)
            {
                nmax=v[i+1]-v[i]-1;
            }
        }
        if (r-v[v.size()-1]>nmax)
        {
            nmax=r-v[v.size()-1];
        }
        if (v[0]-l>nmax)
        {
            nmax=v[0]-l;
        }
        printf("%I64d\n",nmax);
    }
    return 0;
}

#include<map>
#include <iostream>
#include<algorithm>

using namespace std;
map<long long,long long>mp;
long long a[100000];
long long kl[100000];
long long kt[100000];

int main()
{
    long long n,m,l,r,b=1,g=1,zx=1,xz=1;; cin>>n>>m>>l>>r; long long mx=0; mp[1]++; long long y=1,v=1; bool z=false,x=false;
    a[0]=1; kl[0]=1;
    while(1){
        if(!x)
        y*=n;
        if(y<r){
            a[zx]=y;
            zx++;
        }
        else  x=true;
        if(!z)
        v*=m;
        if(v<r){
            kl[xz]=v; xz++;
        }
        else z=true;
        if(z&&x)break;

    }long long c=0; long long we=0;
    for(int i=0;i<zx;i++){
        for(int j=0;j<xz;j++){

       if(mp[a[i]+kl[j]]==0&&a[i]+kl[j]<=r&&a[i]+kl[j]>=l){
           kt[we]=a[i]+kl[j];
           we++; mp[a[i]+kl[j]]++;

       }
        }

    }
          if(we==0&&r-l==0){
              cout<<r-l+1; return 0;
          }sort(kt,kt+we); mx=max(mx,kt[0]-l); mx=max(mx,r-kt[we-1]);
    for(int i=0;i<we;i++){
        long long mn=kt[i+1]-kt[i]-1;
        mx=max(mx,mn);

    }
        cout<<mx;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
long long GCD(long long  n1, long long n2)
{
    if (n2 != 0)
        return GCD(n2, n1 % n2);
    else
        return n1;
}
long long LCM(long long  n1, long long n2)
{
    long long m;
    m=n1*n2/GCD(n1,n2);
    return m;
}
unsigned long long choose(unsigned long long n, unsigned long long k)
{
    if (k > n)
    {
        return 0;
    }
    unsigned long long r = 1;
    for (unsigned long long d = 1; d <= k; ++d)
    {
        r *= n--;
        r /= d;
    }
    return r;
}
bool isprime(long long x)
{
    if(x==1)
        return 0;
    for(long long i=2; i*i<x; i++)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}
long long mod=1000000007;
unsigned long long pow_(unsigned long long base,unsigned long long exponent)
{
    unsigned long long result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base);
        exponent = exponent >> 1;
        base = (base * base) ;
    }
    return result;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    /*********************/
    set<unsigned long long >st;
    set<unsigned long long >::iterator it,mt;
    unsigned long long x,y,l,r,m,mx=0;
    cin>>x>>y>>l>>r;
    for(int i=0;i<=60;i++)
    {
        for(int j=0;j<=60;j++)
        {
        m=pow_(x,i)+pow_(y,j);
        if(m>r)
            break;
        if(m>=l&&m<=r)
        {
            st.insert(m);
        }
        }
    }

    for(it=++st.begin();it!=st.end();it++)
    {
        mt=it;
        mt--;
        mx=max(mx,*it-*mt);
    }
    mt++;

    if(st.find(r)==st.end())
    {
        mx=max(r-(*mt)+1,mx);
    }
    else
    {
        mx=max(r-(*mt),mx);
    }
    if(st.find(r)==st.end())
    {
        mx=max(r-(*mt)+1,mx);
    }
    else
    {
        mx=max(r-(*mt),mx);
    }
    if(st.find(l)==st.end())
    {
        mx=max(*st.begin()-l+1,mx);
    }
    else
    {
        mx=max(*st.begin()-l,mx);
    }
    cout<<mx-1<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define sc(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z);

#define scl(x) scanf("%lld",&x);
#define scl2(x,y) scanf("%lld%lld",&x,&y);
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z);

#define pb push_back
#define mp make_pair

#define M 1000000007
#define inf 99999999999999999LL	//long long inf

#define debug(x) cerr<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define debug4(x,y,z,a) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#a<<" :: "<<a<<"\n";

#define LIM 100020

ll x=1,y=1,l,r;
vector<ll>a,b,final;
ll chu = 1 ;
int main()
{
	int i,j,t;
	cin>>x>>y>>l>>r ;
	while( chu <= (1e18)/x  )
	{
		a.pb( chu ) ;
		chu = chu * x ;
	}
	a.pb(chu);
	chu = 1;
	debug("---------");
	while( chu <= (1e18)/y  )
	{
		b.pb( chu ) ;
		chu = chu * y ;
	}
	b.pb(chu);
	for(i=0;i<a.size();i++)
		for(j=0;j<b.size();j++)
			final.pb( a[i] + b[j] );
	sort( final.begin(),final.end() );
	int counter = 0;
	ll from = l , answer = 0;
	int flag = 0,flag1=0;
	for(i=0;i<final.size();i++)
	{
		if(final[i]==l)
			flag = 1;
		if(final[i]==r)
			flag1 = 1 ;
	}
	while( final[counter] <= r)
	{
		answer = max( answer , final[counter] - from - 1);
		if(from==l && flag==0)
			answer = (answer + 1);
		from = max(from,final[counter]);
		counter++;
	}
	answer = max(answer , r - from -flag1);
	printf("%lld\n",max(0LL,answer) );
	return 0;
}

#define OJ
#include <bits/stdc++.h>
#define reset(a,b) memset(a,b,sizeof(a))
#define MAX(a,b)  a>b?a:b
#define MIN(a,b)  a>b?b:a
#define INF 1e19
using namespace std;
struct cmp
{
  bool operator()(const unsigned long &a,const unsigned long &b)
  {
    return a>b;
  }
};
int main()
{
	#ifndef OJ
	freopen("abc.in","r",stdin);
	freopen("abc.out","w",stdout);
	#endif
  unsigned long int x,y,s,e,t;
  scanf("%lu%lu%lu%lu",&x,&y,&s,&e);
  int i,j,k,l,m=0;
  unsigned long int a[70],b[70];
  priority_queue<unsigned long,vector<unsigned long>,cmp> q;
  reset(a,0); reset(b,0);
  a[0]=1; b[0]=1;
  for(i=1;;i++)
  {
      if(x==1)  break;
      if(INF/a[i-1]<x)  break;
      if(a[i-1]>=e)  break;
      a[i]=a[i-1]*x;
  }
  //printf("%d\n",i);
  for(j=1;;j++)
  {
    if(y==1)  break;
    if(INF/b[j-1]<y)  break;
    if(b[j-1]>=e)  break;
    b[j]=b[j-1]*y;
  }
  //printf("%d\n",j);
  for(k=0;k<i;k++)
  {
    for(l=0;l<j;l++)
    {
      x=a[k]+b[l];
      if(x>e) continue;
      if(x<s) continue;
      q.push(x);
    }
  }
  t=0;
  q.push(s);  q.push(e);
  x=q.top();
  while(!q.empty())
  {
    y=q.top();
    q.pop();
    if(x==y)  continue;
    t=MAX(y-x,t);
    x=y;
  }
  if(x>s&&x<e)  t=MAX(t,e-x);
  if(t>0) t--;
  printf("%lu",t);
	#ifndef OJ
	fclose(stdin);
	fclose(stdout);
	#endif
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
ll x,y,l,r;
vector<ll>vec;
vector<ll>vecs;
vector<ll>vecss;
void Init()
{
    ll i,j,z,h;
    for(i=1; i<=r; i*=x)
    {
        vec.push_back(i);
    }
    for(j=1; j<=r; j*=y)
    {
        vecs.push_back(j);
    }
    for(z=0; z<vec.size(); z++)
    {
        for(h=0; h<vecs.size(); h++)
        {
            ll sum=vec[z]+vecs[h];
            vecss.push_back(sum);
        }
    }
    sort(vecss.begin(),vecss.end());

}
int main()
{
    while(~scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r))
    {
    ll begins;
    ll ends;
    vec.clear();
    vecs.clear();
    vecss.clear();
    Init();
    begins=lower_bound(vecss.begin(),vecss.end(),l)-vecss.begin();
    ends=lower_bound(vecss.begin(),vecss.end(),r)-vecss.begin();
    ll sum=-1;
    sum=max(vecss[begins]-l,sum);

    if(ends==vecss.size())
    {
        ends--;
    }
    for(ll i=begins;i<=ends;i++)
    {
        if(begins==ends)
        {
            sum=max(vecss[i]-l,sum);
        }
        else
        {
            if(i!=ends-1)
            sum=max(vecss[i+1]-vecss[i]-1,sum);
        }
    }
    sum=max(r-vecss[ends],sum);
    printf("%I64d\n",sum);
    }
    return 0;
}
/*2018-07-24 20:24:24.875*/

#include <bits/stdc++.h>

#define MOD 1000000000001000
using namespace std;

int main(){
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long x, y; cin>>x>>y;
    long long inicio, fin;
    cin>>inicio>>fin;
    vector <long long> goldenAge;
    long long e1 = 1;
    for(int i=0; i<fin; i++, e1*=x){
        if(e1>=fin) break;
        long long e2 = 1;
        for(int j=0; j<fin; j++, e2*=y){
            if(e2>=fin) break;
            long long ns = (e1 + e2);

            if( ns >= inicio && ns <= fin){
                goldenAge.push_back(ns);
            }
            if (fin/e2<y)
                break;
        }
        if (fin/e1<x)
                break;
        //if (fin/e1<1)
    }
    goldenAge.push_back(fin+1);
    sort(goldenAge.begin(), goldenAge.end());
   //goldenAge.erase(unique(goldenAge.begin(), goldenAge.end()),goldenAge.end());
    long long dif = 0;
    for(int i=0; i<goldenAge.size()-1; i++){

        dif = max(dif, goldenAge[i+1]-goldenAge[i]-1);
    }
    cout<<dif<<'\n';

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(){

 unsigned __int64 x,y,l,r;
 //scanf("%llu%llu%llu%llu",&x,&y,&l,&r);//*/
 cin>>x>>y>>l>>r;
 unsigned __int64 *sup = new unsigned __int64 [3602];

 unsigned __int64  k=0;

 for(int i=0;i<3602;i++)
    sup[i]=0;
 unsigned __int64 num1=1,num2=1;
 while(num1<=((1e18/x)+2)){
      num1*=x;
      num2=1;
      while(num2<=((1e18)/y)+2){
        num2*=y;
        sup[k++]=num1+num2;
    }
 }

 sort(sup,sup+3602);

unsigned __int64 ans=0 ,m=0;
 num1=0;num2=0;
 for(int i=0;i<3602-1;i++){
    if( sup[i+1]-sup[i]>ans && sup[i]!=0 ){ans=sup[i+1]-sup[i];num1=sup[i+1];num2=sup[i];}
    if( sup[i+1]>r )break;
 }
 if(num1>r)ans=r-num2+1;
 if(ans==0 && l==1 )ans=2; else if (ans==0){cout<<0;return 0;}
 cout<<fixed<<ans-1;
return 0;
}

/*input
2 3 1 10
*/
/*
       _==/          i     i          \==_
     /XX/            |\___/|            \XX\
   /XXXX\            |XXXXX|            /XXXX\
  |XXXXXX\_         _XXXXXXX_         _/XXXXXX|
 XXXXXXXXXXXxxxxxxxXXXXXXXXXXXxxxxxxxXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 XXXXXX/^^^^"\XXXXXXXXXXXXXXXXXXXXX/^^^^^\XXXXXX
  |XXX|       \XXX/^^\XXXXX/^^\XXX/       |XXX|
    \XX\       \X/    \XXX/    \X/       /XX/
       "\       "      \X/      "      /"

*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
//loops
#define f(i,s,n) for(ll i=(ll)s;i<(ll)n;i++)
#define rf(i,n,s) for(ll i=(ll)(n-1);i>=(ll)s;i--)
//reset
#define ms0(X) memset((X), 0, sizeof((X)))
#define ms1(X) memset((X), -1, sizeof((X)))
//IO
#define bio ios_base::sync_with_stdio;cin.tie(NULL)
//STL
#define pb push_back
#define pii pair<ll,ll>
#define vll vector<ll>
#define vpii vector<pii >
#define mll map<ll, ll>
#define mpii map<pii,ll>
#define msll map<string, ll>
#define sortv(v) sort(v.begin(),v.end())
#define F first
#define S second
//standard values
#define mod (ll)(1e9+7)
#define INF (ll)1e16
#define MAXN (ll)(1e5+5)
//comparator
bool cmp(pii a,pii b){
  if(a.F==b.F) return a.S<b.S;
  else return a.F<b.F;
}
ll exp(ll a,ll b){
  ll ans=1;
  while(b!=0){
    if(b%2==1)
      ans=ans*a;
    a=a*a;
    b/=2;
  }
  return ans;
}
/********************************************************************************************************/
vll v;
vll ::iterator lo1,lo2;
mll mp;
int main()
{
  bio;
  ll cnt1 = 0, cnt2 = 0;
  ll x, y, l, r;
  cin>>x>>y>>l>>r;
  ll pre = 1;
  ll final = exp(10,18);
  while(1){
    ll a = pre * x;
    if (a/pre == x && a< final){
      cnt1++;
      pre = a;
    }
    else
      break;
  }
  pre = 1;
  //ll final = exp(10LL,18);
  while(1){
    ll a = pre * y;
    if (a/pre == y && a < final){
      cnt2++;
      pre = a;
    }
    else
      break;
  }
  f(i,0,cnt1+1){
    f(j,0,cnt2+1){
      if(exp(x,i)+exp(y,j)>0){
        ll a = exp(x,i) + exp(y,j);
        if(mp[a]==0){
          v.pb(a);
          mp[a]=1;
        }
      }
      else{
        break;
      }
    }
  }
  //cout<<v.size()<<endl;
  sortv(v);
  lo1 = lower_bound(v.begin(), v.end(), l);
  lo2 = lower_bound(v.begin(), v.end(), r);
  ll ans = 0;
  ll rti, lti;
  if(lo1!=v.end()){
    lti = lo1 - v.begin();
  }////do an extra left comparison
  else
  {
    ans = 0;
    return 0;
  }
  if(lo2!=v.end()){
    rti = lo2 - v.begin();
    if(*lo2 > r){
      lo2--;
      rti--;
    }
  }
  else{
    rti = v.size()-1;
  }
  //cout<<(*lo1)<<" "<<(*lo2)<<endl;
  ans = max(ans, max(0LL, *lo1-l));
  ans = max(ans, max(0LL, r-*lo2));
  f(i, lti+1, rti + 1){
    ans = max(ans, v[i]-v[i-1]-1);
  }
  cout<<ans<<endl;
  return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

vector<LL> v1;

void vec_add(LL a,LL l){
	LL s=v1.size();
	LL ad=1;
	for(int i=0;i<s;i++){
		ad=1;
		while(ad+v1[i]<=l){
			v1.push_back(ad+v1[i]);
			ad*=a;
		}
	}
}

int main(){
	int n,m;
	int i,j,k;
	LL a,b,c;
	LL r,l;
	LL s=0;
	cin>>n>>m>>r>>l;
	v1.push_back(0);
	vec_add(n,l);
	vec_add(m,l);
	sort(v1.begin(),v1.end());
	v1.push_back(l+1);
	for(i=0;i<v1.size() && v1[i]<r;i++);
	a=r-1,s=0;
	for(;i<v1.size();i++){
		b=v1[i]-a-1;
		s=max(s,b);
		a=v1[i];
	}
	cout<<s<<endl;
}

#include <iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
#include<vector>
#include<cmath>
#define pa(a,b) make_pair(a,b)
#define LL long long int
using namespace std;
map<LL , bool>mp;
int main()
{
    LL x,y,l,r; cin>>x>>y>>l>>r; int k=0; LL nums[101000];
    int  numx=1; int numy=1; LL yearsx[5000]; LL yearsy[5000]; LL tempx= x,tempy=y; yearsx[0]=1; yearsy[0]=1;
    while(tempx<=1e18/x){ if(tempx>0)yearsx[numx++]=tempx; if(tempx<0)break; tempx*=x;}
    while(tempy<=1e18/y){ if(tempy>0)yearsy[numy++]=tempy; if(tempy<0)break; tempy*=y;}
    for(int i=0;i<numx;i++)
    {
        for(int j=0;j<numy;j++)
        if(!mp[yearsx[i]+yearsy[j]]&&yearsx[i]+yearsy[j]>=l&&yearsx[i]+yearsy[j]<=r){
            nums[k++]=yearsx[i]+yearsy[j];
            mp[yearsx[i]+yearsy[j]]++;
            }
    }
    sort(nums,nums+k); LL ans=nums[0]-l;
    //for(int i=0;i<k;i++)cout<<nums[i]<<" ";
    for(int i=0;i<k-1;i++)ans=max(ans,nums[i+1]-nums[i]-1);
    ans=max(ans , r-nums[k-1]);
    cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector <ll> v, w, z;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll x, y, l, r, p = 1, q = 1;
    cin >> x >> y >> l >> r;
    while( p < 1e18/x )
    {
        v.push_back(p);
        p *= x;
    }
    v.push_back(p);
    while( q < 1e18/y )
    {
        w.push_back(q);
        q *= y;
    }
    w.push_back(q);
    for( int i = 0; i < v.size() ; ++i )
    {
        for( int j = 0; j < w.size() ; ++j )
        {
            if( w[j] >= (l - v[i]) && w[j] <= (r - v[i]) )
            {
                z.push_back(v[i] + w[j]);
            }
        }
    }
    if( z.size() == 0 ) cout << (r - l) + 1;
    else{
    sort(z.begin(), z.end());
    ll ans, le, ri;
    le = z[0] - l;
    ri = r - z[z.size() - 1];
    ans = max(le, ri);
    for( ll i = 1; i < z.size(); ++i )
    {
        ll temp = (z[i] - z[i - 1]) - 1;
        ans = max(ans, temp);
    }
    if( ans == 999999999999999998 ) ans -= 1;
    cout << ans;
    }
}

#include<bits/stdc++.h>
using namespace std;

long long a[70];

long long b[70];

long long ans[4909];

int main()
{
    long long  x , y , l , r , u = 1 , v = 1 , w=0;

    cin >> x >> y >> l >> r;

    a[0] = 1;

    b[0] = 1;

    for(int i = 1 ; i<=64 ; i++)
    {
        if(a[i-1]*x<=r)
        {
            a[i] = a[i-1]*x;
            u++;
        }
        else
            break;
    }

    a[u] = a[u-1]*x;

    for(int i = 1 ; i<=64 ; i++)
    {
        if(b[i-1]*y<=r)
        {
            b[i] = b[i-1]*y;
            v++;
        }
        else
            break;
    }

    b[v] = b[v-1]*y;

    for(int i = 0 ; i<=u ; i++)
    {
        for(int j = 0 ; j<=v ; j++)
        {
            if(1ll*a[i]+1ll*b[j]>=l && 1ll*a[i]+1ll*b[j]<=r)
            {
                ans[w++] = 1ll*a[i]+1ll*b[j];
            }
        }
    }

    ans[w++] = l-1;
    ans[w++] = r+1;

    sort(ans , ans+w);

    long long fans = 0;

    for(int i = 1 ; i<w ; i++)
    {
        fans = max(fans , ans[i]-ans[i-1]-1);
    }

    cout << fans;

}

#include <bits/stdc++.h>
#define inf 1e9+7
using namespace std;

const int maxn=2e5+5;
typedef long long ll;
ll mod=1e9+7;
int main(void)
{
    ll x,y,l,r;
    map<ll,ll> mp;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    ll xm[100]={1},ym[100]={1};
    int cntx=0,cnty=0;
    while(xm[cntx]<=r&&ym[cnty]<=r)
    {
        if(xm[cntx]<=r)
            xm[++cntx]=xm[cntx-1]*x;
        if(ym[cnty]<=r)
            ym[++cnty]=ym[cnty-1]*y;
    }
    for(int i=0;i<=cntx;i++)
        for(int j=0;j<=cnty;j++)
            mp[xm[i]+ym[j]]=1;
    ll ans=0,t=0;
    for(ll i=l;i<=r;i++)
    {
        //cout<<mp[i]<<endl;
        if(mp[i]==0)
        {
            t++;
            ans=max(ans,t);
        }
        else
            t=0;
    }
    printf("%I64d\n",ans);
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<ll> v;

int cmp(ll a,ll b)
{
    return a>b;
}

int main()
{
    ll x,y,l,r;
    cin >>x >> y >>l>>r;

    ll x1,y1;

    x1 = 1LL;
    y1 = 1LL;

    v.clear();
    v.push_back(r + 1);
    v.push_back(l- 1);
    while(y1 <= r && y1 > 0)
    {
        x1 = 1LL;

        while(x1 + y1 <= r && x1 > 0 && y1 > 0)
        {
            if(x1 + y1 >= l)
                v.push_back(x1 + y1);

            cout<<x1<<"+"<<y1<<endl;

            x1 *= x;
        }
        y1 *= y;
    }

    sort(v.begin(),v.begin()+v.size(),cmp);

    ll ans = 0;
    for(int i = 0;i < v.size() - 1;i ++)
    {
        cout<<(v[i] - 1) - (v[i + 1] + 1) + 1<<endl;

        ans = max(ans,(v[i] - 1LL) - (v[i + 1] + 1LL) + 1LL);
    }

    cout<<ans<<endl;

}
/*2018-07-24 20:06:13.055*/

#include <iostream>
#include <iostream>
#include <vector>
#include <set>

using namespace std;

typedef unsigned long long UINT64;

int main(int argc, char* argv[]) {
    vector<UINT64> xs, ys;
    set<UINT64> s;
    set<UINT64>::iterator it;
    UINT64 x, y, l, r, n, ans, temp;
    int i, j;

    cin >> x >> y >> l >> r;

    n = 1;
    temp = n;
    while (n < r || n < temp) {
        xs.push_back(n);
        temp = n;
        n *= x;
    }

    n = 1;
    temp = n;
    while (n < r || n < temp) {
        ys.push_back(n);
        temp = n;
        n *= y;
    }

    for (i = 0; i < xs.size(); i++) {
        for (j = 0; j < ys.size(); j++) {
            n = xs[i] + ys[j];
            if (n < l) continue;
            if (n > r) break;
            s.insert(n);
        }
    }
    s.insert(l - 1);
    s.insert(r + 1);

    ans = 0;
    it = s.begin();
    n = *it;
    it++;
    while (it != s.end()) {
        temp = *it - n - 1;
        n = *it;
        if (ans < temp) ans = temp;
        it++;
    }

    cout << ans << "\n";

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define S second
#define F first
#define pll pair<ll,ll>
#define R return
#define SI size()
#define MEM memset
#define pb push_back
#define C continue

using namespace std;
ll x,y,l,r,sum;
vector<ll> ans;
set<ll>sc;

unsigned ll po(ll x,ll u){
    if (u==0) R 1;
    if (u==1) R x;
    ll h=po(x,u/2);
    h*=h;
    if (u%2) h*=x;
    R h;
}

int main()
{
    cin >> x >> y >> l >> r;
    for (int i=0;i<100;i++){
        for (int j=0;j<100;j++){
            sum=po(x,i)+po(y,j);
            if (sum>r) break;
            if (sum>=l&&sum<=r){
                ans.pb(sum);
                sc.insert(sum);
            }
        }
    }
    ans.pb(l-1);
    ans.pb(r+1);
    sort(ans.begin(),ans.end());
    ll maxx=0;
    for (int i=1;i<ans.SI;i++){
        maxx=max(maxx,ans[i]-ans[i-1]-2);
    }
    maxx++;
    if (sc.SI==r-l+1) cout << 0;
    else cout << maxx;
    cout  << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 20) + 5;
typedef long long ll;
set<ll> s;
int main() {
   ll x,y;
   ll l,r;
   int i,j;
   ll xx=1,yy=1;
   scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
   for(int i=0;i<60;++i)
   {
       for(int j=0;j<60;++j)
        {
            if(xx+yy>r)
                break;
            if(xx+yy>=l)
                s.insert(xx+yy);
            if(yy>yy*y||yy*y>1e18||(yy%10)+(y%10)>18)
                break;
            yy*=y*1LL;
        }
        if(xx>xx*x||xx*x>1e18||(xx%10)+(x%10)>18)
            break;
        //cout<<xx<<endl;
        xx*=x*1LL;
       // cout<<xx<<endl;
        yy=1;
   }
   ll ans=0,tmp;
   if(!s.size())
        return printf("%lld\n",r-l+1),0;
   for(auto it=s.begin();it!=s.end();++it)
   {
       // cout<<*it<<endl;
        if(it==s.begin())
        {
            ans=max(ans,*it-l);
            tmp=*it;
            continue;
        }
        ans=max(ans,*it-tmp-1);
        tmp=*it;
   }
   ans=max(ans,r-tmp);
    printf("%lld",ans);
}

//============================================================================
// Author      : Red_Phoenix
//============================================================================

#include <bits/stdc++.h>

using namespace std;

int countzero(long long n) {
	int cnt = 0;
	while (n) {
		n /= 10;
		cnt++;
	}
	return cnt;
}
int main() {
	//freopen("in.txt", "r", stdin);
	//freopen("out", "w", stdout);

	long long x, y, l, r, ans = 0, last, limitx, limity;
	cin >> x >> y >> l >> r;
	set<long long> st;
	last = l;
	limitx = (1e18 + 2) / x;
	limity = (1e18 + 2) / y;
	for (long long i = 1; i < r && i <= limitx; i *= x) {
		for (long long j = 1; j + i <= r && j <= limity; j *= y) {
			if (i + j < l)
				continue;
			st.insert(i + j);
		}
	}
	if (!st.empty()) {
		if (*(st.begin()) != l)
			last--;
		for (auto it = st.begin(); it != st.end(); it++) {
			ans = max(ans, (*it) - last - 1);
			last = *it;
		}

		if (*(st.rbegin()) != r)
			ans = max(ans, r - *(st.rbegin()));
	} else
		ans = r - l + 1;

	cout << ans << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long s[1000],ks=0;
bool cmp(long long a,long long b)
{
	return a<b;
}
int main()
{
	long long l,r,x,y;
	cin>>x>>y>>l>>r;
	long long sx=1,sy=1;
	for(int i=1;i<=64;i++)
	{
		sy=1;
		for(int j=1;j<=64;j++)
		{
			if(sy+sx<r)
			{
				s[ks++]=sy+sx;
			}
			if((double)sy*y>r)
				break;
			else
				sy=sy*y;
		}
		if((double)sx*x>r)
				break;
		sx=sx*x;
	}
	s[ks++]=l;
	s[ks++]=r;
	sort(s,s+ks,cmp);
	int ll,rr;
	for(int i=0;i<ks;i++)
	{
		if(s[i]==l)
			ll=i;
		if(s[i]==r)
			rr=i;
		//cout<<s[i]<<endl;
	}
	long long mm=0;
	for(int i=ll;i<=rr-1;i++)
	{
		if(i==ll||i==rr-1)
		{
			if(mm<s[i+1]-s[i])
				mm=s[i+1]-s[i];
		}
		else if(mm<s[i+1]-s[i]-1)
			mm=s[i+1]-s[i]-1;
	}
	cout<<mm<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e18;
vector<ll> va, vb, vc;
set<ll> s;
int main(){
    ll x, y, l, r, cnt, ans=0;
    scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
    int xc=0, yc=0, i, j;
    cnt=1;
    while(1){
        if(cnt>=r)
            break;
        va.push_back(cnt);
        if(cnt>INF/x)
            break;
        cnt=cnt*x;
    }
    cnt=1;
    while(1){
        if(cnt>=r)
            break;
        vb.push_back(cnt);
        if(cnt>INF/y)
            break;
        cnt=cnt*y;
    }
    for(i=0; i<va.size(); ++i)
        for(j=0; j<vb.size(); ++j){
            if(!s.count(va[i]+vb[j])&&va[i]+vb[j]<=r&&va[i]+vb[j]>=l)
                s.insert(va[i]+vb[j]), vc.push_back(va[i]+vb[j]);
        }
    if(!vc.size()){
        printf("%I64d\n", r-l+1);
        return 0;
    }
    sort(vc.begin(), vc.end());
    i=0;
    if(s.count(l)){
        while(s.count(l)) l++;
        if(l>=r){
            puts("0");
            return 0;
        }
        for(i=0; i<vc.size(); ++i)
            if(vc[i]>l)
                break;
    }
    ans=max(ans, vc[i++]-l);
    if(!s.count(r))
        vc.push_back(++r);
    for(; i<vc.size(); ++i){
        ans=max(ans, vc[i]-vc[i-1]-1);
    }
    printf("%I64d\n", ans);
}
/// 2 2 1 1000000000000000000

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;
typedef pair<ll,ll>pll;
ll bin_pow(ll a,ll b){
    if(b==0)return 1;
    if(b%2==0){
        ll t=bin_pow(a,b/2);
        return t*t;
    }
    else return a*bin_pow(a,b-1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll x,y,l,r,ans=0,m=1,n=1,k=-1;
    cin>>x>>y>>l>>r;
    vector<ll>c;
    for(int a=0;a<=60;a++){
        for(int b=0;b<=60;b++){
            m=bin_pow(y,b);
            n=bin_pow(x,a);
            c.push_back(m+n);
        }
    }
            sort(c.begin(),c.end());
        for(int i=0;i<c.size();i++){
                if(c[i]<l)continue;
            if(c[i]>r)break;
        if(c[i-1]<l)ans=max(ans,c[i]-1-l+1);
        else ans=max(ans,c[i]-1-(c[i-1]+1)+1);
        k=c[i];
        }
        if(k==-1)ans=r-l+1;
        else
        ans=max(ans,r-(k+1)+1);
        cout<<ans;
    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
//#include <bits/stdc++.h>
using namespace std;
long long int aa[10000000];
int main()
{
    std::ios_base::sync_with_stdio(0);
  long long int i,j,k,a,b,n,m,sum,l,r,x,y,max,ana,bana,gg,ll,kk,sos;
cin>>x>>y>>l>>r;
  i=0;
  a=0;
  b=0;
  n=1;
  long long int sss=0;
    for(i=0;i<65;i++)
    {
      for(j=0;j<65;j++)
      {
        gg=0;
        ll=0;kk=0;
            sos=0;
            a=1;
            while(a<=1000000000000000000/x && sos<i)
            {
                a=a*x;sos=sos+1;
            }
            ll=1;
            b=1;
            sos=0;
           while(b<=1000000000000000000/y && sos<j)
          {
            b=b*y;sos=sos+1;
          }
          n=a+b;
       //   if(a+b<=1000000000000000000)
        //  {n=a+b; gg=1;
         // }
          if(n<=r && n>0 && n>=l)//&& n!=9223372036854775807
            {  aa[sss]=n;
              sss=sss+1;
            }
      }
    }
  m=sss;
 // for(i=0;i<m;i++)
   // cout<<aa[i]<<" "<<endl;
  sort(aa,aa+m);
  max=0;
  for(i=0;i<m-1;i++)
  {
    //cout<<aa[i]<<endl;
    if((aa[i+1]-aa[i])>=max)
    {
        max=aa[i+1]-aa[i];
    }
  }
  if((r-aa[m-1])>=max)
    {max=r-aa[m-1];
      max=max+1;
    }
  cout<<max-1;
return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Khela ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pf printf
#define sf scanf
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define pii pair<int ,int>
#define PII pair<ll ,ll>

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}
const int Max=1e9+100;
const ll  MAX=1e18+500;
const int sz=1e3+100;

int main()
{
    Khela
    ll a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,n,mx;
    char ch;
    double d;
    string str1,str2,str;
    bool bl,bl1;
    int t,cs=1;
    vector<ll> ne,de,test;
    while(cin>>x>>y>>l>>r)
    {
        ne.clear(),de.clear();
        ne.push_back(1ll),de.push_back(1ll);
        ll temp=x;
        ne.push_back(x),de.push_back(y);
        while(temp<=(MAX/x)) {
            temp*=x;
            ne.push_back(temp);
        }
        temp=y;
        while(temp<=(MAX/y)){
            temp*=y;
            de.push_back(temp);
        }

        test.clear();
        for(auto xx: ne ) for(auto yy: de) test.push_back(xx+yy);
        sort(test.begin(),test.end());
        test.erase(unique(test.begin(),test.end()),test.end());

        ll mx=0;
        bool bl=false;
        ll siz=test.size();
        for(i=0;i<siz;i++){
            if(!bl && test[i]>=l && test[i]<=r){
                bl=true;
                mx=max(mx,test[i]-l);
            }
            else if(bl && test[i]<=r){
                 mx=max(mx,test[i]-test[i-1]-1);
            }
            else if(i-1>=0 && test[i-1]<=r && test[i-1]>=l){
                if(test[i-1]>=l) mx=max(mx,r-test[i-1]);
                else mx=max(mx,r-l+1);
            }
            else if(i==0 && test[i]>r) mx=max(mx,r-l+1);
        }

        cout<<mx<<endl;
    }

}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
typedef long long ll;
using namespace std;
int gcd(int a,int b)
{  return b==0?a:gcd(b,a%b);  }
ll power(ll a,ll b)
{
	ll ans=1;
	for(;b;b>>=1)
	{
		if(b&1) ans=(long long)ans*a;
		a=(long long)a*a;
	}
	return ans;
}
ll x,y,l,r;
ll ans,sum;
map<ll,ll> f1;
ll a[1000],b[1000];
ll vis[1000000];
int main(int argc, char const *argv[])
{
	//printf("%lld\n",power(2,0));
	cin>>x>>y>>l>>r;
	ll u=0;
	if(x>y)
        swap(x,y);
	a[u]=1;
	u++;
	for(ll i=1;;i++)
    {
        a[u]=a[u-1]*x;
    u++;
        if(((long double)(a[u-1]*x))>r)
            break;
    }
    ll s=0;
    b[s]=1;
    s++;
    for(ll i=0;;i++)
    {
        b[s]=b[s-1]*y;
        s++;
        if(((long double)(b[s-1]*y))>r)
            break;
    }
    ll kk=0;
    for(ll i=0;i<u;++i)
    {
        for(ll j=0;j<s;++j)
        {
            if(a[i]+b[j]>r)
                continue;

            if(a[i]+b[j]<l)
                continue;
            vis[kk++]=a[i]+b[j];
             f1[a[i]+b[j]]=5;
        }
    }
    ans=0;
    sum=0;
    sort(vis,vis+kk);
    for(int i=1;i<kk;++i)
    {
        ans=vis[i]-vis[i-1]-1;
        sum=max(sum,ans);
    }
    if(f1[r]!=5)
    sum=max(sum,r-vis[kk-1]);
    if(f1[l]!=5)
    sum=max(sum,vis[0]-l);
    /*for(int i=0;i<kk;++i)
    {
        printf("%lld ",vis[i]);
    }*/
    printf("%lld\n",sum);
	return 0;
}

//
// Created by Vishal Sheth on 05/06/17.
//
#include <iostream>
#include <algorithm>
typedef long long ll;
using namespace std;

ll fast_exp(ll base, ll exp)
{
    ll res=1;
    while(exp>0)
    {
        if(exp%2==1)
            res=(res*base);
        base=(base*base);
        exp/=2;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    ll x,y,r,l,curr,past,i,j;
    cin >> x >> y >> l >> r;
    ll temp;
    if(x<y) {
        temp=y;
        y=x;
        x=temp;
    }
    past=curr=-1;
    ll max=0,interval;
    bool flag=false;
    curr=0;
    ll num[100005];
    ll ind=0;
    for(i=0;fast_exp(x,i)<r;i++) {
        for(j=0;;j++) {
            curr=fast_exp(x,i) + fast_exp(y,j);
            if(curr>=l && curr<=r)
                num[ind++]=curr;
            else if(curr>r)
                break;
        }
    }
    sort(num,num+ind);
    for(i=1;i<ind;i++) {
//        interval=num[i]-num[i-1];
        if(interval>max)
            max=interval;
    }
    if(r-num[ind-1]>max)
        cout << r-num[ind-1] << endl;
    else if(num[0]-l>max)
        cout << num[0]-l << endl;
    else if(max)
        cout << max-1 << endl;
    else
        cout << "0" << endl;
    return 0;
}

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string.h>
#include <stdio.h>
#define LL long long
#define pii pair<int,int>
#define piii pair<int, pii >
#define REP(i,n) for (int (i)=0;(i)<(n);(i)++)
#define vi vector<int>
#define vpi vector< pii >
#define INF 2147483647
#define big 20160116
#define pb push_back
#define mp make_pair
using namespace std;
const int Maxk=1005;
string mul(string a,string b){
	int res[Maxk];
	memset(res,0,sizeof(res));
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	for (int i=0;i<a.length();i++){
		for (int j=0;j<b.length();j++){
			res[i+j]+=(a[i]-'0')*(b[j]-'0');
		}
	}
	for (int i=0;i<Maxk;i++){
		if (res[i]>=10) {
			res[i+1]+=(res[i]/10);
			res[i]%=10;
		}
	}
	string ret;
	bool flag=false;
	for (int i=Maxk-1;i>=0;i--){
		if (flag || res[i]){
			flag=true;
			ret=ret+(char)(res[i]+'0');
		}
	}
	if (ret=="") ret="0";
	return ret;
}
string add(string a,string b){
	if (a.length()<b.length()) swap(a,b);
	while (a.length()!=b.length()){
		b='0'+b;
	}
	for (int i=a.length()-1;i>=0;i--){
		a[i]+=(b[i]-'0');
		if (a[i]>'9' && i){
			a[i]-=10;
			a[i-1]++;
		}
	}
	if (a[0]>'9'){
		a[0]-=10;
		a='1'+a;
	}
	return a;
}

long long toiont(string s){
    long long ans=0,i=0;
    while(s[i]){
        ans*=10;
        ans+=s[i]-'0';
    }
    return ans;
}
int up(string s){
    if(s.size()>18) return 1;
    else return 0;
}
long long x[1000],y[1000],len,lenn,Q[100000],LEN;
int main(){
    long long i,j,A,B,L,R,ans,res,AA,BB;
    double ANS;
    cin>>AA>>BB>>L>>R;
    x[0]=1;y[0]=1;len=lenn=1;A=AA;B=BB;
    while(A<R){
        x[len++]=A;
        ANS=A*AA;
        A*=AA;
        if(ANS>1e18) break;
    }
    while(B<R){
        y[lenn++]=B;
        ANS=B*BB;
        B*=BB;
        if(ANS>1e18) break;
    }
    LEN=0;
    for(i=0;i<len;i++){
        for(j=0;j<lenn;j++){
            Q[LEN++]=x[i]+y[j];
        }
    }
    Q[LEN++]=R+1;
    sort(Q,Q+LEN);ans=0;
    for(i=0;i<LEN;i++){
        if(Q[i]>=L&&Q[i]<=R){
            if(L>Q[i-1]){
                res=Q[i]-L;
            }
            else res=Q[i]-Q[i-1]-1;
            ans=max(ans,res);
        }
        else if(Q[i]>R){
            if(Q[i-1]>=L&&Q[i-1]<=R){
                res=R-Q[i-1];
            }
            else if(Q[i]<L){
                res=R-L+1;
            }
            ans=max(ans,res);
            break;
        }
    }
    printf("%lld\n",ans);
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long x,y,l,r;
    cin>>x>>y>>l>>r;
    int i,j;
    long long u=1,v=1;
    long long ans=0;
    set<long long> s;
    set<long long>::iterator iter;
    for(i=0;i<=64;i++)
    {
        if(u>r)
        {
            break;
        }
        for(j=0;j<=64;j++)
        {
            if(u+v<=r)
            {
                if(u+v>=l)
                {
                    s.insert(u+v);
                }
            }
            else
            {
                break;
            }
            v*=y;
        }
        v=1;
        u*=x;
    }
    vector<long long>a;
    for(iter=s.begin();iter!=s.end();iter++)
    {
        a.push_back(*iter);
    }
    for(i=0;i<a.size();i++)
    {
        if(i==0)
        {
            ans=a[i]-l;
            continue;
        }
        if(i==a.size()-1)
        {
            ans=max(ans,r-a[i]);
            break;
        }
        ans=max(ans,a[i]-a[i-1]-1);
    }
    cout<<ans;
    return 0;
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
    ll x, y, l, r;
    cin >> x >> y >> l >> r;
    set <ll> s;
    vector <ll> v1, v2, v;
    v1.push_back(1);
    v2.push_back(1);
    for(int i = 1; ; i++) {
        ll t = v1[i - 1] * x;
        if(t >= r || t < 0)
            break;
        v1.push_back(t);
    }
    for(int i = 1; ; i++) {
        ll t = v2[i - 1] * y;
        if(t >= r || t < 0)
            break;
        v2.push_back(t);
    }
    for(unsigned i = 0; i < v1.size(); ++i) {
        for(unsigned j = 0; j < v2.size(); ++j) {
            if(v1[i] + v2[j] >= l && v1[i] + v2[j] <= r) {
                s.insert(v1[i] + v2[j]);
            }
        }
    }
    for(set <ll> :: iterator it = s.begin(); it != s.end(); ++it) {
        v.push_back(*it);
    }
    ll ans = 0;
    int siz = v.size();
    if(siz) {
        ans = max((v[0] - 1) - l + 1, ans);
        ans = max(r - (v[siz - 1] + 1) + 1, ans);
        for(int i = 1; i < siz - 1; i++) {
            ans = max((v[i] - 1) - (v[i - 1] + 1) + 1, ans);
            ans = max((v[i + 1] - 1) - (v[i] + 1) + 1, ans);
        }
    } else {
        ans = r - l + 1;
    }
    cout << ans << endl;
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long int ll;
const int max_n=1005;
const int inf=1e9+7;
typedef long long int ll;
ll x,y,l,r;
ll dat[20005];
int cnt;

int main(){
    cnt=0;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    ll a,b,left;
    for(ll i=0;;i++){
        a=(ll)pow(x,i);
        if(a>r)    break;
        left=r-a;
        for(ll j=0;;j++){
            b=(ll)pow(y,j);
            if(b>left)  break;
            a+=b;
            if(a>=l&&a<=r)
                dat[cnt++]=a;
            a-=b;
        }
    }
    dat[cnt++]=l-1;
    dat[cnt++]=r+1;
    sort(dat,dat+cnt);
    int top=1;
    for(int i=1;i<cnt;i++){
        if(dat[i]!=dat[i-1])
            dat[top++]=dat[i];
    }
    ll ans=0;
    for(int i=1;i<top;i++){
        ans=max(ans,dat[i]-dat[i-1]-1);
    }
    printf("%I64d\n",ans);
}

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <numeric>
#include <deque>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
#define el '\n'
typedef  long long  ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;

const int mod=1000000007;
//const int mod=998244353;

int main()
{   ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll t,n,m,k,q,x,a,b,c,d,y,l,r;
    cin >> x >> y >> l >> r;
    vll xx,yy,p;
    a=1;
    xx.pb(1);
    yy.pb(1);
    const ll inf=1e18;
    for(int i=0; i<100; i++){
        if(x<=inf/a){
            xx.pb(x*a);
            a*=x;
        }
        else{
            break;
        }
    }
    a=1;
    for(int i=0; i<100; i++){
        if(y<=inf/a){
            yy.pb(y*a);
            a*=y;
        }
        else{
            break;
        }
    }
//    print(xx);
//    print(yy);
    for(int i=0; i<xx.size(); i++){
        for(int j=0; j<yy.size(); j++){
            if(xx[i]+yy[i]<=inf)
                p.pb(xx[i]+yy[j]);
        }
    }
    sort(all(p));
    p.resize(distance(p.begin(),unique(all(p))));
//    print(p);
    vpll v;
    for(int i=1; i<p.size(); i++){
        if(p[i]-p[i-1]>1){
            v.eb(p[i-1]+1,p[i]-1);
        }
    }
    a=0;
    for(int i=0; i<v.size(); i++){
        if(min(v[i].se,r)>=max(v[i].fi,l)){
            a=max(min(v[i].se,r)-max(v[i].fi,l)+1,a);
        }
    }
    cout << a << el;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    long long x,y,l,r;
    cin>>x>>y>>l>>r;
    int xp=1,yp=1;
    long long a[10000]={0},b[10000];
    while(pow(x,xp)<r){
        a[xp-1]=pow(x,xp);
        xp++;
    }
    while(pow(y,yp)<=r){
        b[yp-1]=pow(y,yp);
        yp++;
    }
    vector<long long> v;
    for (int i=0;i<xp;i++){
        for (int j=0;j<yp;j++){
            if (a[i]+b[j]>=l && a[i]+b[j]<= r){
                v.push_back(a[i]+b[j]);
            }
        }
    }
    sort(v.begin(),v.end());
    long long maxi=0;
    for (int i=1;i<v.size();i++){
        if (v[i-1]+1<v[i]){
            if (maxi<v[i]-v[i-1]){
                maxi=v[i]-v[i-1]-1;
            }
        }
    }
    cout<<maxi;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll Log(ll x, ll limit)
{
	ll ret = 0;
	ll multi = 1;
	while(multi < limit)
	{
		multi *= x;
		++ret;
	}
	return ret;
}

ll Pow(ll x, ll a, ll y, ll b)
{
	ll ret_x = 1;
	ll ret_y = 1;
	for(ll i = 1; i <= a; ++i)
		ret_x *= x;
	for(ll i = 1; i <= b; ++i)
		ret_y *= y;
	return ret_x + ret_y;
}
int main()
{
	ll x, y, l, r;
	scanf("%I64d %I64d %I64d %I64d", &x, &y, &l, &r);
	ll max_a = Log(x, r);
	ll max_b = Log(y, r);
	vector<ll>val;
	for(ll i = 0; i <= max_a; ++i)
	{
		for(ll j = 0; j <= max_b; ++j)
		{
			ll ans = Pow(x, i, y, j);
			if(ans < 0)continue;
			val.push_back(ans);
		}
	}
	sort(val.begin(), val.end());
	unique(val.begin(), val.end());
	vector<ll> ret;
	for(uint32_t i = 0; i < val.size(); ++i)
	{
		if(val[i] >= l && val[i] <= r){
			ret.push_back(val[i]);
//			printf("%I64d ", val[i]);
		}
	}
//	printf("\n");
	ll result = 0;
    for(uint32_t i = 0; i < ret.size(); ++i)
    {
        if(i == 0)
        {
            result = max(result, ret[i] - l);
        }
        else if( i == ret.size()-1)
        {
            result = max(result, r - ret[i]);
        }
        else
        {
            result = max(result, ret[i] - ret[i-1] - 1);
        }
	}
	printf("%I64d\n", result);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll x,y,L,R;

int f(ll s){
ll inf = 1e18;
int ret=0;
while( inf>1ll ){
    ret++;
    inf/=s;
}
return ret+(inf==1ll);
}

ll pow(ll n,ll p){
if( p==0 )return 1ll;
if( p==1 )return n;
return pow(n,p/2)*pow(n,p/2)*(( p&1 )?n:1ll);
}

int main(){

cin>>x>>y>>L>>R;
int logX=f(x);
int logY=f(y);
set<ll>s;

for(int i=0;i<=logX;i++)
for(int j=0;j<=logY;j++){
    ll v=pow(x,i)+pow(y,j);
    if( L<=v && v<=R )
    s.insert( v );
}

if( s.size()==0 ){
    cout<<0<<endl;
    return 0;
}

ll ans=*s.begin()-L;
ll last=*s.begin();
for(auto x:s){
        ans=max(ans,x-last-1);
        last=x;
}
ans=max(ans, R-last);
cout<<ans<<endl;
return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000000000000000000
ll l , r ;
ll x,y;
ll power(ll base, ll exponent)
{
    ll result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base);
        exponent = exponent >> 1;
        base = (base * base) ;
    }
    return result;
}
set<ll>st;

int main(){
  cin>>x>>y>>l>>r;
  if(x>y){
    ll temp=x;
    x=y;
    y=temp;
  }
  ll xp[100];
  ll yp[100];
  ll xsize=0;
  ll ysize=0;
  int i=0;
  for(i=0;power(x,i)<=MAX;i++){
    xp[i]=power(x,i);
//    cout<<i<<" "<<xp[i]<<endl;
  }
  xsize=i;
  for(i=0;power(y,i)<=MAX;i++)
    yp[i]=power(y,i);
  ysize=i;
  ll k=0;
  ll arr[100000];
  for(int i=0;i<xsize;i++){
    for(int j=0;j<ysize;j++){
       if(st.find(xp[i]+yp[j])==st.end()){
      arr[k++]=xp[i]+yp[j];
      st.insert(arr[k-1]);
       }
        //cout<<i<<" "<<xp[i]<<"    "<<j<<" "<<yp[j]<<endl;
       // cout<<k<<" "<<arr[k-1]<<endl;
    }
  }
  sort(arr,arr+k);
    /*for(int i=0;i<=40;i++)
        cout<<arr[i]<<" ";
    cout<<endl;*/
  ll lindex=lower_bound(arr,arr+k,l)-arr;
  ll rindex=upper_bound(arr,arr+k,r)-arr;
  ll maxx=0;

    //cout<<lindex<<" "<<rindex<<endl;
    //if(rindex)
  for(int i=lindex+1;i<k && i<rindex;i++){
     //cout<<i<<" ";
    if(arr[i]-arr[i-1]-1 >maxx)
      maxx=arr[i]-arr[i-1]-1;
  }
    if(arr[lindex]-l >maxx)
        maxx=arr[lindex]-l;
    if(r-arr[rindex-1] > maxx)
        maxx=r-arr[rindex-1];
    //cout<<endl;
  cout<<maxx<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
ll x , y ,l,r,mx,res, m , n;
vector < ll > v ;
main()
{
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    for(ll i=0;;i++)if( ceil(pow(x,i)) > r ){n = i;break;}
    for(ll i=0;;i++)if( ceil(pow(y,i)) > r ){m = i;break;}
    for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=m;j++)
        {
           res = ll(ceil(pow(x,i))) + ll(ceil(pow(y,j)));
           v.push_back(res);
        }
    }
    sort(v.begin(),v.end());

    bool b = 0 , lef = 1 ;
    ll mxl=0;
    for(int i=0;i<v.size();i++)
    {
        if(v[i]>r)
        {

            if(r-v[i-1] > mx){mx=r-v[i-1];b=1;}
            break;
        }
        if(v[i]>=l)
        {
            if(lef){if(v[i]-l > mx){mxl=v[i]-l;}lef=0;}
            if(v[i]-v[i-1] > mx)mx = v[i]-v[i-1];
        }
    }
    cout << (b ? max(mx,mxl) : mx-1)<<endl;
}

#include<bits/stdc++.h>
#define MX 100005
using namespace std;
typedef long long ll;
const ll inf=1e18;

ll x, y, l, r;
vector< ll > vt;

ll power(ll b, ll p){
     ll i, j, k;
     ll ans=1LL, tmp;
     for(i=0; i<p; ++i){
          tmp=ans;
          ans*=b;
          if(ans/tmp!=b) return -1;
     }
     return ans;
}

ll func(ll s, ll e){
     ll i, j, k;
     ll p1, p2;
     p1=power(x, s);
     p2=power(y, e);
     if(p1==-1 || p2==-1) return -1;
     k=p1+p2;
     if(p1>inf || p2>inf) return -1;
     return k;
}

int main(){
     ll i, j, k;
     ll mx, tmpL, tmpR;
     cin >> x >> y >> l >> r;
     vt.push_back(0LL);
     for(i=0; i<65; ++i){
          for(j=0; j<65; ++j){
               if(func(i, j)!=-1) vt.push_back(func(i, j));
          }
     }
     sort(vt.begin(), vt.end());
     mx=0;
     for(i=1; i<10; ++i){
          tmpL=max(vt[i-1]+1, l);
          tmpR=min(r, vt[i]-1);
          if(tmpL<=tmpR){
               mx=max(mx, tmpR-tmpL+1);
          }
     }
     cout << mx << endl;
}

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <set>

using namespace std;

typedef unsigned long long ull;

int main() {
	ull x, y, l, r, ret;
	double a = 1., b;
	set<ull> badYears;
	cin >> x >> y >> l >> r;

	while (a < r) {
		b = 1;
		while(true) {
			auto val = a + b;
			if (val > r) {
				break;
			} else if(val >= l) {
				badYears.insert((ull)val);
			}
			b *= y;
		}
		a *= x;
	}
	badYears.insert(l - 1);
	badYears.insert(r + 1);
	ret = 0;
	ull prev;
	bool first = true;
	for (auto it = badYears.begin(); it != badYears.end(); ++it) {
		if (first) {
			prev = *it;
			first = false;
			continue;
		}
		auto val = *it - prev - 1;
		if (val > ret) {
			ret = val;
		}
		prev = *it;
	}
	cout << ret << endl;
#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N=1000070; //10e6

#define ll long long int
#define inf 0x3f3f3f3f
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ii tuple<int, int>
#define all(x) (x).begin(), (x).end()

vector<ll>v;
ll potx[N], poty[N];

int logx(ll curr, ll x){
	if(curr<x)return 1;
	return 1+logx((curr+x-1)/x, x);
}

int main(int argc, char const *argv[]){
	ll x, y, l, r;
	cin >> x >> y >> l >> r;
	ll ans=0;

	potx[0]=1;
	poty[0]=1;

	int to=logx(r, x);
	for(int i=1; i<=to; i++){
		potx[i]=potx[i-1]*x;
	}

	to=logx(r, y);
	for(int i=1; i<=to; i++){
		poty[i]=poty[i-1]*y;
	}

	v.pb(0);
	for(int i=0; i<=logx(r, x); i++){
		for(int j=0; j<=logx(r, y); j++){
			v.pb(potx[i]+poty[j]);
		}
	}

	int i=0;
	sort(all(v));
	ll prev;
	while(i+1<v.size() && v[++i]<=r) ans=max(ans, v[i]-max(l, v[i-1]+1));

	if(i-1>=0)prev=v[i-1];
	else prev=l-1;
	ans=max(ans,r-max(prev, l-1));

	printf("%lld\n", ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
vector<long long> ul;
#define MAX 1e18+5
int main(){
	long long x, y, l, r;
	cin >>x >>y >>l >>r;
	long long n2=1, n3=1;
	long long t;
	while(n2 < MAX){
		n3=1;
		while(n3+n2<MAX){
			ul.push_back(n3+n2);
			n3 *= y;
		}
		n2 *= x;
	}
	sort(ul.begin(), ul.end());
	vector<long long>:: iterator it;
	long long ans=0;
	//if(ul[0]<=r)
	//	ans = ul[0]-l;
	int i;
	for(i=0; i<ul.size(); i++){
		if(i>0 && l>ul[i-1] && l<ul[i])
			ans = max(ans, ul[i]-l);
		if(i<ul.size()-1 && ul[i]>=l && ul[i+1]<=r)
			ans = max(ans, ul[i+1]-ul[i]-1);
		if(i<ul.size()-1 &&ul[i]<=r && ul[i]>=l && ul[i+1]>r)
			ans = max(ans, r-ul[i]);
		if(i==ul.size()-1 && r >= ul.back())
			ans = max(ans, r-ul.back());
		if(ul[i]>r) break;
	}
	//cout <<r <<' '<<ul[i]

	if(r > ul[i-1])
		ans = max(ans, r-ul[i-1]);
	cout <<ans;
	return 0;
}

#include<cstdio>
#include<algorithm>
#define max_n 100010
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;

LL s[max_n];

int main()
{
	LL x,y,l,r;
	scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
	bool flag=false;
	int p=1;
	LL ans1=0,ans2=0;
	for(LL i=0;;i++)
	{
		ans1=(long long)(ceil(pow(x,i)));
		if(ans1>r) break;
		for(LL j=0;;j++)
		{
			ans2=(long long)(ceil(pow(y,j)));
			if(ans2>r) break;
			if(ans1+ans2>r) break;
			s[p++]=ans1+ans2;
		}
	}
	sort(s+1,s+p);
	LL maxn=0,ans=0;
	for(int i=1;i<p;i++)
	{
	//	printf("%lld\n",s[i]);
		if(s[i]>=l && !flag)
		{
			ans=s[i]-l;
			maxn=max(ans,maxn);
			if(i==p-1)
			{
				ans=r-s[i];
				maxn=max(ans,maxn);
				break;
			}
			flag=true;
		}
		else if(flag)
		{
			ans=s[i]-s[i-1]-1;
			maxn=max(ans,maxn);
			if(s[i]<=r && s[i+1]>r || s[i]<=r && i==p-1)
			{
				ans=r-s[i];
				maxn=max(ans,maxn);
				break;
			}
		}
	//	printf("%lld\n",ans);
		maxn=max(ans,maxn);
	}
	printf("%lld\n",maxn);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
#define fastio ios::sync_with_stdio(false);
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ln length()
#define M 1000000007

ll power(ll x, ll y)
{
	ll res = 1;
	while(y)
	{
		if(y&1)
			res = res*x;
		y = y>>1;
		x = x*x;
	}
}
int main(int argc, char const *argv[])
{
	ll x,y,l,r;
	cin>>x>>y>>l>>r;
	ll a,b;
	a = ceil(log(r)/log(x));
	b = ceil(log(r)/log(y));
	vll v;
	rep(i,0,a)
	{
		rep(j,0,b)
		{
			ll sum = power(x,i)+power(y,j);
			if(sum>=l && sum<=r)
			{
				v.pb(sum);
			}
		}
	}
	ll diff=0;
	if(v.size()>0)
		sort(v.begin(),v.end());
	// rep(i,0,v.size()-1)
	// {
	// 	cout<<v.at(i)<<" ";
	// }
	if(v.size()>1)
	{
		rep(i,1,v.size()-1)
		{
			if(v.at(i)-v.at(i-1)-1 > diff)
				diff = v.at(i)-v.at(i-1)-1;
		}
	}
	if(v.size()>0)
	{
		if(v.at(0)-l>diff)
			diff = v.at(0)-l;
		if(r-v.at(v.size()-1) > diff)
			diff = r-v.at(v.size()-1);
	}
	else
	{
		diff = r-l+1;
	}
	cout<<diff<<"\n";
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define all(x) (x).begin(), (x).end()
#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define for1(i,n) for(int i=1;i<=(int)(n);i++)
const int MOD = 1e9 + 7 ;
int mpow(int a,int b,int p=MOD){a=a%p;int res=1;while(b>0){if(b&1)res=(res*a)%p;a=(a*a)%p;b=b>>1;}return res%p;}
const int N=1e5+2;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      // freopen("output.txt","w",stdout);
      #endif
      int x,y,l,r;
      cin>>x>>y>>l>>r;
      vector<int> v;
      for(int i=0;i<=60;i++)
      {
            int o=mpow(x,i,1e18);
            if(i>0)
            {
                  if(mpow(x,i,1e18)<mpow(x,i-1,1e18)) break;
            }
            if(o>r) break;
            for(int j=0;j<=60;j++)
            {
                  int w=o+mpow(y,j,1e18);
                  if(j>0)
                  {
                        if(mpow(y,j,1e18)<mpow(y,j-1,1e18)) break;
                  }
                  if(w>=l&&w<=r)
                  {
                        v.pb(w);
                  }
                  if(w>r) break;
            }
      }
      v.pb(r+1);
      v.pb(l-1);
      sort(all(v));
      int ans=0;
      for(int i=1;i<v.size();i++)
      {
            ans=max(ans,v[i]-v[i-1]-1);
      }
      cout<<ans;
}
// I never lose. I either win or I learn

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
map<ll,ll> mp;
vector<ll> v;
ll x,y,l,r;
int main()
{
	//n=x^a+y^b
	while(cin>>x>>y>>l>>r)
	{
		v.clear();
		for(ll a=0;a<=63;a++)
		{
			for(ll b=0;b<=63;b++)
			{
				ll n=pow(x,a)+pow(y,b);
				if(n>0&&n>=l&&n<=r)
					v.push_back(n);
			}
		}
		sort(v.begin(),v.end());
		v.erase(unique(v.begin(),v.end()),v.end());
		ll ans=0;

		for(int i=0;i<v.size();i++)
		{
			//cout<<v[i]<<' ';
			ll res=0;
			if(i==0)
				res=v[i]-l;
			if(i==v.size()-1)
				res=max(res,r-v[i]);
			else
				res=v[i]-v[i-1]-1;
			ans=max(ans,res);
		}
		cout<<ans<<endl;
	}
	return 0;
}

#include <iostream>
#include <set>

using namespace std;

set<long long> st;

int main()
{
          long long  l,r,x,y,xx,yy,ans(0);
          cin>>x>>y>>l>>r;
          xx=yy=1;
          st.insert(l);
          st.insert(r);
          while(xx < 1000000000000000018/x)
          {
                    yy=1;
                    while(yy+xx<1000000000000000018/y)
                    {
                              st.insert(xx+yy);
                              yy*=y;
                    }
                    xx*=x;
          }
          set<long long int> :: iterator it = st.begin();
          set<long long int> :: iterator itt ;
          while(*it<=l)
                    it++;
          itt=it;
          itt++;
          while(*it!=r)
          {
                    if(*itt - *it >ans)
                              ans=*(itt) - *it - (*itt==r ? 0 : 1);
                    it++;
                    itt++;
          }
          cout<<ans;
}

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
   long long int  x,y,l,r;
    while(cin>>x>>y>>l>>r)
    {
         long long int tmp1=r,tmp2=r;
        int  num1=0,num2=0;
         long long int a1[10005],a2[10005];
         long long int temp1=x,temp2=y;
        while(tmp1)
        {
            tmp1/=x;
            a1[num1]=temp1;
            temp1*=x;
            num1++;
        }

        //for(int i=0;i<num1;i++)
          //  cout<<a1[i]<<" ";
           // cout<<endl;
        while(tmp2)
        {
            tmp2/=y;
            a2[num2]=temp2;
            temp2*=y;
            num2++;

        }

       // for(int i=0;i<num2;i++)
          // cout<<a2[i]<<" ";
            //cout<<endl;
        int num=0;
         long long int res[1005];
        for(int i=0;i<num1;i++)
            for(int j=0;j<num2;j++)
        {
            if(a1[i]+a2[j]>=l&&a1[i]+a2[j]<=r)
                res[num++]=a1[i]+a2[j];
        }

       num+=2;
       res[num-2]=l-1;
       res[num-1]=r+1;
       sort(res,res+num);
//       for(int i=0;i<num;i++)
//        cout<<res[i]<<" ";
//       cout<<endl;
      long long int Max=0;
      for(int i=1;i<num;i++)
        Max=max(Max,res[i]-res[i-1]-1);
       cout<<Max<<endl;

    }
}

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
long long int aa[10000000];
int main()
{
    std::ios_base::sync_with_stdio(0);
  long long int i,j,k,a,b,n,m,sum,l,r,x,y,max;
cin>>x>>y>>l>>r;
  i=0;
  a=0;
  b=0;
  n=1;
  long long int sss=0;
    for(i=0;i<60;i++)
    {
      for(j=0;j<60;j++)
      {
          n=pow(x,i)+pow(y,j);
          if(n<=r && n>0 && n>=l && n<=9223372036854775807)
            {  aa[sss]=n;
              sss=sss+1;
            }
      }
    }
  m=sss;
 // for(i=0;i<m;i++)
   // cout<<aa[i]<<" "<<endl;
  sort(aa,aa+m);
  max=0;
  for(i=0;i<m-1;i++)
  {
    //cout<<aa[i]<<endl;
    if((aa[i+1]-aa[i])>=max)
    {
        max=aa[i+1]-aa[i];
    }
  }
  if((r-aa[m-1])>=max)
    {max=r-aa[m-1];
      max=max+1;
    }
  cout<<max-1;
return 0;
}

#include <iostream>
#include <set>

using namespace std;

set<long long int> stx,sty,st;
set<long long int> :: iterator it;
set<long long int> :: iterator itt ;
int main()
{
          long long  l,r,x,y,xx,yy,ans(0);
          cin>>x>>y>>l>>r;
          xx=yy=1;
          r++;
          while(xx <= 1e18/x)
          {
                    stx.insert(xx);
                    xx*=x;
          }
          while(yy <= 1e18/y)
          {
                    sty.insert(yy);
                    yy*=y;
          }
          for(it = stx.begin();it!=stx.end();it++)
          {
                    for(itt = sty.begin();itt!=sty.end();itt++)
                    {
                              st.insert(*it+*itt);
                              cout<<*it+*itt<<endl;
                    }
          }
          it=st.begin();
          while(*it<l)
                    it++;
          itt=it;
          itt++;
          while(*it<r && itt!=st.end())
          {
                              ans=max(ans,min(*(itt),r) - *(it) - 1) ;
                    it++;
                    itt++;
          }
          cout<<ans;
}

#include<iostream>
#include<cstdio>
#include<set>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
long long x,y,l,r;
vector<long long>k;
int main()
{
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&l,&r);
    long long i=1;
    long long j=1;
    while(i<r)
    {
        j=1;
        while(i+j<=r)
        {
            if(i+j>=l)
            {
                k.push_back(i+j);
            }
            j*=y;
        }
        i*=x;
    }
    sort(k.begin(),k.end());
    long long s=0;
    if(!k.empty()&&k[k.size()-1]<r)
    {
        k.push_back(r+1);
    }
    if(!k.empty())
    {
    for(int f=1;f<k.size();f++)
    {
        s=max(s,k[f]-k[f-1]-1);
    }
    }
    else
    {
        s=l-r+1;
    }
    printf("%I64d\n",s);
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=67;
ll a[maxn];
ll b[maxn];
ll x,y,l,r;
ll sum[maxn*maxn];
int main()
{
	cin>>x>>y>>l>>r;
	ll p=log(r)/log(x);
	ll q=log(r)/log(y);
	for(int i=0;i<=p;i++)
	{
		a[i]=pow(x,i);
	}
	for(int i=0;i<=q;i++)
	{
		b[i]=pow(y,i);
	}
	int cnt=0;
	for(int i=0;i<=p;i++)
	{
		for(int k=0;k<=q;k++)
		{
			if(a[i]+b[k]<=r&&a[i]+b[k]>=l)
			sum[cnt++]=a[i]+b[k];
		}
	}
	sort(sum,sum+cnt);
//	cnt=unique(sum,sum+cnt)-sum;
	if(cnt==0)
	{
		cout<<r-l+1<<endl;
		return 0;
	}
	ll ans=max(sum[0]-l,r-sum[cnt-1]);
	for(int i=1;i<=cnt-1;i++)
	{
		ans=max(ans,sum[i]-sum[i-1]-1);
	//	cout<<ans<<endl;
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=10000+10;
const double eps=1e-6;
const double INF=1e18;

double pp(ll a,ll b)
{
	if(b==0) return 0;
	double ans=1;
	for(int i=1;i<=b;i++)
	    ans*=a;
	return ans;
}
int main()
{
	ll x,y,l,r;
	scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
	ll a[maxn];memset(a,0,sizeof(a));
	ll len=0;
	for(int i=0;i<=80;i++)
	{
		for(int j=0;j<=80;j++)
		{
			double s=pp(x,i)+pp(y,j);
			if((s+eps)<(double)l)
			    continue;
			if((s-eps)>r)
			    break;
			else
			    a[len++]=(int)s;
		}
	}
	sort(a,a+len);

	ll max=a[0]-(l-1);
	for(int i=1;i<len;i++)
	{
		ll d=a[i]-a[i-1];
		//printf("%lld\n",b[i]);/**/
		//printf("%lld\n",d);/**/
		if(d>max)
		    max=d;
	}
	ll d=r+1-a[len-1];
	if(d>max)
	    max=d;
	printf("%lld\n",max-1);
	return 0;
}

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
#define siz 110105
typedef long long int LL;
using namespace std;
LL x,y,L,R;
LL a[siz];
map<LL,int>G;
LL Pow(LL v,int t)
{
    LL p = 1;
    for(int i=1; i<=t; i++) p*=v;
    return p;
}
void solve()
{
    LL ans  = 0;
    int len1,len2,cot=0;
    for(int i=0; i<=64; i++) if(Pow(x,i)/x>R) len1=i, i = 100;
    for(int i=0; i<=64; i++) if(Pow(y,i)/y>R) len2=i, i = 100;
    for(int i=0; i<=len1; i++)
    {
        LL Px,Py,sum;
        for(int j=0; j<=len2; j++)
        {
            Px = Pow(x,i) , Py = Pow(y,j);
            if(Px>R-Py) continue;
            sum = Px+Py;
            if(G.count(sum)) continue;
            G[sum]++;
            a[++cot] = sum;
        }
    }
    sort(a+1,a+cot+1);
    //for(int i=1;i<=cot;i++) cout<<a[i]<<" ";
   // cout<<endl;
    if(a[1] != L) ans = max(ans,a[1] - L);
    if(a[cot] != R) ans = max(ans,R - a[cot]);
    for(int i=1; i<cot; i++)
    {
        ans = max(ans,a[i+1]-a[i]-1);
    }
    cout<<ans<<endl;
    //printf("%I64d\n",ans);
}
int main()
{
    while(~scanf("%I64d %I64d %I64d %I64d",&x,&y,&L,&R))
    {
        G.clear();
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define ld long double
#define f first
#define s second
#define PI acos(-1)
#define CS ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
double EPS = 1e-8;
int main() {
	ll x,y,l,r;
    cin>>x>>y>>l>>r;
    ll a=1;
    if(l==r&&l==1){
        cout<<1<<endl;
        return 0;
    }
    vector<ll>vp;
    set<ll>st;
    for(int i=0;i<64;i++){
            ll b=1;
        for(int j=0;j<64;j++){
            double t=a+b;
           // cout<<log2(t)<<" "<<log2(r)<<endl;
          // cout<<i<<" "<<j<<" "<<a<<" "<<b<<" "<<a+b<<endl;
            if(log(t)>log(r+10))
                break;
            vp.push_back(a+b);
            st.insert(a+b);
            t=1.0*b*y;
            if(log(t)>log(r+10))
                break;
            b*=y;
        }
        double t=a*1.0*x;
        if(log(t)>log(r+10))
            break;
            a*=x;
	}
    sort(vp.begin(),vp.end());
    ll len=0;
    bool cx=1;
    bool badl=0;
    for(int i=0;i<vp.size();i++){
       // cout<<vp[i]<<endl;
        if(vp[i]<=l){
            continue;
        }
        ll x=max(vp[i-1],l);
        if(st.count(x))
            x++;
        ll en=min(vp[i],r);
        if(st.count(en))
            en--;

        len=max(len,en-x+1);
        //cout<<x<<" "<<vp[i]<<" "<<len<<endl;
    }
    cout<<len<<endl;

	return 0;
}

#include "bits/stdc++.h"
using namespace std;
const int MAXN=1000,OO=0x3f3f3f3f;
typedef long long ll;
ll fp(ll x,int p){
    if(p==0) return 1;
    ll sq=pow(x,p/2);
    sq*=sq;
    if(p%2){sq*=x;}
    return sq;
}
vector<ll>vc;
int main(){
 ll x,y,r,l; cin>>x>>y>>l>>r;
    for (int i = 0; i < 62; ++i) {
        for(int j=0;j<62;j++){
            ll f=fp(x,i),s=fp(y,j);
            if((f>r)||(s>r)||(f+s>r))
                break;
            if(f+s>=l)
                vc.push_back(f+s);
        }
    }
    sort(vc.begin(),vc.end());
    ll ans=0;
    if(!vc.empty()&&l!=vc[0])ans=1;
    for(int i=0;i+1<vc.size();i++)
        ans=max(ans,vc[i+1]-vc[i]-1);
    if(!vc.empty())
    ans=max(ans,r-vc.back());
    cout<<ans;
}

#include <iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
#include<vector>
#include<cmath>
#define pa(a,b) make_pair(a,b)
#define LL  long long int
using namespace std;
map<LL,bool>mp;
LL nums[101000];
int main()
{
    LL x,y,l,r; cin>>x>>y>>l>>r;
    int k=0; LL nums[101000];
    int  numx=1; int numy=1;
    LL yearsx[5000]; LL yearsy[5000];
    LL tempx= x,tempy=y; yearsx[0]=1; yearsy[0]=1;
    while(tempx<2e18){ if(tempx>0)yearsx[numx++]=tempx; if(tempx<0)break; tempx*=x;}
    while(tempy<2e18){ if(tempy>0)yearsy[numy++]=tempy; if(tempy<0)break; tempy*=y;}
    for(int i=0;i<numx;i++)
    {
        for(int j=0;j<numy;j++)
        if(!mp[yearsx[i]+yearsy[j]]&&yearsx[i]+yearsy[j]>=l&&yearsx[i]+yearsy[j]<=r){
            nums[k++]=yearsx[i]+yearsy[j];
            mp[yearsx[i]+yearsy[j]]=1;
            }
    }
    if(k==0)return cout<<0,0 ;
    sort(nums,nums+k); LL ans=nums[0]-l;
    //for(int i=0;i<k;i++)cout<<nums[i]<<" "; !mp[yearsx[i]+yearsy[j]]&&
    for(int i=0;i<k-1;i++)ans=max(ans,nums[i+1]-nums[i]-1);
    ans=max(ans , r-nums[k-1]);
    cout<<ans;
}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define pp pair<ll,ll>
#define inf 1e9
using namespace std;
ll siev[1000005];
ll phi[1000005];

inline void Scan_f(ll &a){char c = 0;while(c<33)c = getc(stdin);a = 0;while(c>33){a = a*10 + c - '0';c = getc(stdin);}}
void sieve(){for(ll i=2;i<=1e6;i++){if(siev[i]==0){for(ll j=i;j<=1e6;j+=i)siev[j]=i,phi[j]=phi[j]*(i-1)/i;}}}
ll gcd(ll a,ll b){if(a<b)swap(a,b);while(b!=0){ll temp=b;b=a%b;a=temp;}return a;}
ll power(ll x, ll y,ll z){if (y == 0)return 1;ll p = power(x, y/2,z) % z;p = (p * p) % z;return (y%2 == 0)? p : (x * p) % z;}
ll modInverse(ll a,ll m){return power(a, phi[m]-1,m);}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    /*for(ll i=1;i<=1e6;i++)
    phi[i]=i;
    phi[0]=0;
    sieve();*/
    ll x,y,l,r;
    cin>>x>>y>>l>>r;

    ll first[100],second[100];
    first[0]=second[0]=1;

        int sf=1,ss=1;

        while(x*first[sf-1]<=r)
        {
        first[sf]=x*first[sf-1];
        sf++;
        }

        while(y*second[ss-1]<=r)
        {
        second[ss]=y*second[ss-1];
        ss++;
        }

    vector<ll> v;
    for(int i=0;i<sf;i++)
    {
        for(int j=0;j<ss;j++)
        {
            if(first[i]+second[j]>=l&&first[i]+second[j]<=r)
            v.pb(first[i]+second[j]);
        }
    }

    sort(v.begin(),v.end());

    ll ans=0;
    for(int i=1;i<v.size();i++)
        ans=max(ans,v[i]-v[i-1]-1);

    if(v.size()!=0)
    ans=max(v[0]-l,r-v[v.size()-1]);
    cout<<max(ans,0LL)<<"\n";

    return 0;
}

#include<bits/stdc++.h>
#define N 1000005
#define LL long long
using namespace std;
LL x,y,l,r,num=0,ans=0,u=5000;
LL a[N];
LL ksm(LL a,LL b)
{
	LL sum=1;
	while(b)
	{
		if(b&1)
		{
			sum=(sum*a);
		}
		b/=2;
		a=(a*a);
	}
	return sum;
}
map<LL,bool>vis;
int main()
{
	scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
	for(LL i=0;i<=min(u,(r/x+1));i++)
	{
		for(int j=0;j<=min(u,(r/y+1));j++)
		{
			LL k=ksm(x,i)+ksm(y,j);
			if(k>r)
			{
				break;
			}
			if(k>=l&&k<=r&&!vis[k])
			{
				// cout<<k<<" "<<i<<" "<<j<<endl;
				num++;
				a[num]=k;
				vis[k]=1;
			}
		}
	}
	sort(a+1,a+num+1);
	// cout<<num<<endl;
	// for(int i=1;i<=num;i++)cout<<a[i]<<" ";
	// 	cout<<endl;
	if(l!=a[1])
	{
		ans=max(ans,a[1]-l);
	}
	if(r!=a[num])
	{
		ans=max(ans,r-a[num]);
	}
	// ans=max(a[1]-l-1,ans);
	// ans=max(r-a[num]-1,ans);
	for(int i=2;i<=num;i++)
	{
		ans=max(ans,a[i]-a[i-1]-1);
	}
	printf("%lld",ans);
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long

/*void swap(ll *a, ll *b){
    ll temp;
    temp = *a;
    *a = *b;
    *b = temp;
}*/

int main(){
    ll x,y,l,r;
    cin >> x>>y>>l>>r;
    ll i=0,j=0,res_x=1,res_y=1,arr_x[60],arr_y[60],limit_x,limit_y;
    while(res_x/x < (1e18/x)){
        arr_x[i++] = res_x;
        res_x *= x;
        //cout << "arr_x[" << i-1 << "] : " << arr_x[i-1] << "\n";
    }
    limit_x = i-1;
    //cout << "limit_x : " << limit_x << "\n";
    while(res_y/y < (1e18/y)){
        arr_y[j++] = res_y;
        res_y *= y;
        //cout << "arr_y[" << j-1 << "] : " << arr_y[j-1] << "\n";
    }
    limit_y = j-1;
    //cout << "limit_y : " << limit_y << "\n";

    ll array1[3600],k=0;
    for(i=0; i<=limit_x; i++){
        for(j=0; j<=limit_y; j++){
            array1[k++] = arr_x[i] + arr_y[j];
            //cout << "array[" << (k-1) << "] : " << array[k-1] << "\n";
        }
    }
    //cout << "\n";

    sort(array1,array1+k);
    /*for(i=0; i<k-1; i++){
        for(j=i+1; j<k; j++){
            if(array1[i] > array1[j])
            swap(&array[i], &array1[j]);
        }
    }*/

    /*for(i=0; i<k; i++){
        cout << "array1[" << i << "] : " << array1[i] << "\n";
    }*/

    array1[k] = 1e19;
    ll fin=0,flagl=0,flagr=0;
    for(i=0; i<=k; i++){
        if(array1[i]>=l && array1[i]<=r){
            if(flagl==0){
                fin = array1[i]-l;
                flagl=1;
            }else{
                if(array1[i]-array1[i-1] > fin){
                    fin = array1[i]-array1[i-1]-1;
                }
            }
        }
        if(array1[i]>r && flagr==0){
            if(flagl==0)
                fin = (r-l)+1;
            else{
                if(r-array1[i-1] > fin)
                    fin = r-array1[i-1];
            }
            flagr=1;
            break;
        }
    }
    cout << fin;

    /*i=0;j=k-1;
    while(array[i]<l){
        i++;
    }
    while(array[j]>r){
        j--;
    }
    //cout << "i : " << i << "\n";
    //cout << "j : " << j << "\n";
    ll fin=0,z;
    for(z=i; z<=j; z++){
        if(z==i){
                if(array[z]==l)
                    fin = 1;
                else
                    fin = array[z]-l;
        }else{
            if(array[z]-array[z-1] > fin)
                fin = array[z]-array[z-1];
        }
    }
    if(l==r){
        for(ll k1=0; k1<=k; k1++){
            if(array[k1]==l){
                cout << 0;
                break;
            }
            else{
                cout << 1;
                break;
            }
        }
    }else{
        if(r-array[z-1] > fin){
            fin = r-array[z-1];
            cout << fin;
        }else
            cout << fin-1;
    }*/

    return 0;
}