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 <stdio.h>
#include <stdlib.h>
#include<limits.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        if (LLONG_MAX/p<x)
            p=r;
        else p=p*x;
        br++;
    }
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        if (LLONG_MAX/p1<y)
            p1=r;
        else p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%lld",r-l+1);
    else if (br2==r-l+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    long long x;
    return 0;
}

#include <stdio.h>
typedef long long ll;
#define MAXN 65
void insertSort(ll* arr, int N) {
    for (int i = 1; i < N; ++i) {
        int j = i - 1;
        ll v = arr[i];
        for ( ; j >= 0 && v < arr[j]; --j)
            arr[j + 1] = arr[j];
        arr[j + 1] = v;
    }
}
ll a1[MAXN], a2[MAXN], s[MAXN * MAXN];
int main() {
    ll x, y, l, r;
    scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
    int N = 0;
    int sz1 = 0, sz2 = 0;
    ll t;
    for (t = 1; t <= r / x; t *= x)
        a1[sz1++] = t;
    a1[sz1++] = t;
    for (t = 1; t <= r / y; t *= y)
        a2[sz2++] = t;
    a2[sz2++] = t;
    for (int i = 0; i < sz1; ++i)
        for (int j = 0; j < sz2; ++j)
            if (a1[i] + a2[j] >= l && a1[i] + a2[j] <= r)
                s[N++] = a1[i] + a2[j];
    s[N++] = l - 1;
    s[N++] = r + 1;
    insertSort(s, N);
    ll ans = 0;
    for (int i = 1; i < N; ++i)
        if (ans < s[i] - s[i - 1] - 1) ans = s[i] - s[i - 1] - 1;
    printf("%I64d\n", ans);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
	ll i, j, k;
	ll n1 = m - l + 1;
	ll n2 =  r - m;

	/* create temp arrays */
	ll L[n1], R[n2];

	/* Copy data to temp arrays L[] and R[] */
	for (i = 0; i < n1; i++)
		L[i] = arr[l + i];
	for (j = 0; j < n2; j++)
		R[j] = arr[m + 1+ j];

	/* Merge the temp arrays back llo arr[l..r]*/
	i = 0; // Initial index of first subarray
	j = 0; // Initial index of second subarray
	k = l; // Initial index of merged subarray
	while (i < n1 && j < n2)
	{
		if (L[i] <= R[j])
		{
			arr[k] = L[i];
			i++;
		}
		else
		{
			arr[k] = R[j];
			j++;
		}
		k++;
	}

	/* Copy the remaining elements of L[], if there
	   are any */
	while (i < n1)
	{
		arr[k] = L[i];
		i++;
		k++;
	}

	/* Copy the remaining elements of R[], if there
	   are any */
	while (j < n2)
	{
		arr[k] = R[j];
		j++;
		k++;
	}
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
	if (l < r)
	{
		// Same as (l+r)/2, but avoids overflow for
		// large l and h
		ll m = l+(r-l)/2;

		// Sort first and second halves
		mergeSort(arr, l, m);
		mergeSort(arr, m+1, r);

		merge(arr, l, m, r);
	}
}

ll power(ll x, ll y)
{
	if (y == 0)
		return 1;
	else if (y%2 == 0)
		return power(x, y/2)*power(x, y/2);
	else
		return x*power(x, y/2)*power(x, y/2);
}

ll kitna(ll a)
{
	ll count = 1;
	ll temp = a;
	while(temp<1000000000000000000&&(1000000000000000000/temp>a))
	{
	temp*=a ;
	count++;
	}
	return count;
}

int main(ll argc, char const *argv[])
{
	ll T;
	//   scanf("%lld",&T);
	T = 1;
	// printf("%lld %lld %lld",MIN,MAX,max);
//	  printf("%lld\n",kitna(510000000000));
	ll x,y,l,r ;
//	printf("%lld\n",kitna(2));
	scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
	ll o = l;
	ll xcountl =  -1;
	while(o)
	{
		xcountl++;
		o = o/x ;
	}

	ll p = r;
	ll xcountr = 0;
	while(p)
	{
		xcountr++;
		p = p/x;
	}
	ll e = r;
	ll ycountr = 0;
	while(e)
	{
		ycountr++;
		e = e/y;
	}
	ll qw = l;
	ll ycountl = -1;
	while(qw)
	{
		ycountl++;
		qw = qw/y;
	}

//	printf("%lld %lld\n",xcountl,xcountr);
	// printf("%lldchck",power(x,2));
	ll totalcount = 0;
	ll A[1000000];
	ll idx = 0;
	for(ll s=xcountl;s<xcountr;s++)
	{
		ll u = 0;
		while(power(y,u)+power(x,s)<l)
		{
			u++;
		}

		while(power(y,u)+power(x,s)<=r&&u<=kitna(y))
		{
			//   printf(">kya%lld?",A[idx]);

			A[idx] = power(y,u)+power(x,s) ;
//			printf("n%lldn",A[idx]);

			if(A[idx]>MAX)
				MAX = A[idx] ;
			if(A[idx]<MIN)
				MIN = A[idx] ;
			idx++;
			totalcount++;
			u++;
		}
//		printf("u = %lld\n",u);
	}
	for(ll s=ycountl;s<ycountr;s++)
	{
		ll u = 0;
		while(power(x,u)+power(y,s)<l)
		{
			u++;
		}
		while(power(x,u)+power(y,s)<=r&&u<xcountl&&u<=kitna(x))
		{
			//  printf("ERd  %lld %lld\n",u,s);

			A[idx] = power(x,u)+power(y,s) ;

			if(A[idx]>MAX)
				MAX = A[idx] ;
			if(A[idx]<MIN)
				MIN = A[idx] ;
			idx++;
			totalcount++;
			u++;
		}
	}
//	for(ll g=0;g<idx;g++)
//		printf("|%lld|",A[g]);

	//       printf("\n|%lld %lld|",MIN,MAX);
	mergeSort(A,0,idx-1);
	//   for(ll g=0;g<idx;g++)
	// printf("|%lld|",A[g]);

	//  printf("\n|%lld %lld|",MIN,MAX);
	for(ll sd = 1;sd<idx;sd++)
	{
		if(A[sd]-A[sd-1]-1>max&&A[sd]>0&&A[sd-1]>0)
			max = A[sd]-A[sd-1]-1 ;
		//  if(max<0)max=0;
	}
	ll finalanswer = 0;
	ll left = MIN-l;
	if(left<0)left = 0;
	ll right = r-MAX;
	if(right<0)right = 0;
//	printf("l %lld r %lld %lld|",left,right,max);
	if(left>right)
	{
		if(left>max)
		{
			finalanswer = left;
		}
		else
		{
			finalanswer = max ;
		}
	}
	else
	{
		if(right>max)
		{
			finalanswer = right;
		}
		else
		{
			finalanswer = max ;
		}
	}
	//  printf("<%lld>",totalcount);
	if(totalcount==0)
	{
		finalanswer = r-l+1 ;
	}
	if(l==1)
	{
		if(r-l+1-totalcount==0)
			printf("0\n");
		else
		{printf("%lld\n",finalanswer);}
	}

	else{
		if(r-l+1-totalcount==0)
			printf("0\n");
		else
		{ printf("%lld\n",finalanswer);}
	}

	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long long int check=1000000000000000000;
void merge(long long int arr_1[],long long int arr_2[],int size,long long int arr_3[])
    {
    int i=0,j=0,k=0,size_i=(size+1)/2,size_j=size-size_i;
    while(k<size)
        {
        if((arr_1[i]>=arr_2[j]||i==size_i)&&j!=size_j)
            arr_3[k++]=arr_2[j++];
        else
            arr_3[k++]=arr_1[i++];
    }
}
void merge_sort(long long int a[],int start,int end,long long int a_1[])
    {
    	if(end<start)
    	return;
    int size=end-start+1;
    if(start==end)
        a_1[0]=a[start];
    else
        {
        int mid=(start+end)/2;
        long long int arr_1[mid-start+1],arr_2[end-mid];
        merge_sort(a,start,mid,arr_1);
        merge_sort(a,mid+1,end,arr_2);
        merge(arr_1,arr_2,size,a_1);
    }
}
int main()
{
long long int a[4000];
long long int x_1,y_1,l,r,dig_x,dig_y;
scanf("%I64d%I64d%I64d%I64d",&x_1,&y_1,&l,&r);
long long int x[63],y[63];
x[0]=1;
y[0]=1;
int i=0;
for(i=1;i<63;i++)
{
if(check/x_1<x[i-1])
{
break;
}
x[i]=x[i-1]*x_1;
}
dig_x=i;
for(i=1;i<63;i++)
{
if(check/y_1<y[i-1])
break;
y[i]=y[i-1]*y_1;
}
dig_y=i;
int k=0;
for(int i=0;i<dig_x;i++)
{
	for(int j=0;j<dig_y;j++)
	{
		if(r-y[j]>=x[i])
		{
		a[k++]=x[i]+y[j];
		//printf("%lld ",a[k-1]);
		}
	}
}
//printf("\n");
long long int a_1[k];
merge_sort(a,0,k-1,a_1);
//for(int i=0;i<k;i++)
//printf("%lld ",a_1[i]);
//printf("\n");
int j=0;
while(a_1[j]<l)
j++;
long long int ans=0,prev=l;
if(a_1[j]!=l)
prev=l-1;
while(a_1[j]<=r&&j<k)
{
	if(a_1[j]-prev-1>ans)
	ans=a_1[j]-prev-1;
	//printf("%lld %lld %lld\n",a_1[j],prev,ans);
	prev=a_1[j];
	j++;
}
if(r-prev>ans)
ans=r-prev;
printf("%I64d",ans);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MOD 1000000007
#define ll long long

ll cmpfunc(const void *a, const void *b){
    return (*(ll*)a - *(ll*)b);
}

int max(int a,int b){
    return (a>b)?a:b;
}

ll power(ll x,ll y){
    ll ans=1;
    for(ll i=0;i<y;i++) ans*=x;
    return ans;
}

int main(void){
    ll x,y,l,r;
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    ll b,d;
    ll i,j;
    //a=floor(1.0*log(l)/log(x));
    b=(ll)floor(1.0*log(r)/log(x));
    //printf("b=%lld\n",b);
    //c=floor(1.0*log(l)/log(y));
    d=(ll)floor(1.0*log(r)/log(y));
    //printf("d=%lld\n",d);

    //a<=b,c<=d
    ll un[10000]; //stores unlucky numbers
    int count=0; //number of unlucky numbers
    for(i=0;i<=b;i++){
        for(j=0;j<=d;j++){
            if(power(x,i)>r-power(y,j)) continue;
            ll sum=(ll)power(x,i)+(ll)power(y,j);
            if(sum>=l){
                            //printf("sum=%lld\n",sum);
                un[count]=sum;
                count++;
            }
        }
    }
    //printf("count=%lld\n",count);
    if(count==0){
        printf("%lld\n",r-l+1);
        return 0;
    }
    ll unlucky[count];
    for(i=0;i<count;i++) unlucky[i]=un[i];
    ll c,d1,t;
  for (c = 1 ; c <= count - 1; c++) {
    d1 = c;

    while ( d1 > 0 && unlucky[d1] < unlucky[d1-1]) {
      t          = unlucky[d1];
      unlucky[d1]   = unlucky[d1-1];
      unlucky[d1-1] = t;

      d1--;
    }
  }

    //for(i=0;i<count;i++) printf("%lld ",unlucky[i]);
    //putchar('\n');
    ll max=0;
    for(i=0;i<count-1;i++){
        if(unlucky[i+1]-unlucky[i]-1>max) max=unlucky[i+1]-unlucky[i]-1;
    }
    //printf("%lld %lld\n",max,r-unlucky[count-1]);
    if(r-unlucky[count-1]>max) max=r-unlucky[count-1];
    if(unlucky[0]-l>max) max=unlucky[0]-l;

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

    return 0;
}

#include<stdio.h>

typedef unsigned long long int lli;

void msort(lli a[], int s, int e)
{
    if(e-s!=0)
    {
        msort(a, s, (s+e)/2);
        msort(a, (s+e)/2 + 1, e);
    }
    else
        return;
    lli b[e-s+1];
    int  i=s, j=(s+e)/2 + 1, n;
    for(n=0; i<=(s+e)/2 && j<=e;n++)
    {
        if(a[i]<=a[j])
            b[n]=a[i++];
        else
            b[n]=a[j++];
    }
    if(i<=(s+e)/2)
        for(;i<=(s+e)/2;i++, n++)
            b[n] = a[i];
    else
        for(;j<=e;j++,n++)
            b[n]=a[j];
    for(i=0;i<n;i++, s++)
        a[s] = b[i];
}

int checklog10(lli a, lli b)
{
    lli i=0,j,k;
    while(a!=0)
    {
        a/=10;
        i++;
    }
    while(b!=0)
    {
        b/=10;
        i++;
    }
    if(i>19)
        return 1;
    return 0;
}

int main()
{
    lli i,j,ele;
    lli x,y,l,r;
    lli a[100000], b[100000];
    scanf("%llu %llu %llu %llu", &x, &y, &l, &r);
    if(y<x)
    {
        y = x+y;
        x = y - x;
        y = y - x;
    }

    int ct = 0, ind=0;
    i=1;
    while(i<r)
    {
        a[ind++] = i+1;
        if(checklog10(i, x)==1)
            break;
        i*=x;
    }
/*
    for(i=0;i<ind;i++)
        printf("%llu ", a[i]);
    putchar('\n');
  */
    int eind;
    eind = ind-1;

    j=1;

    while(j<r)
    {
        if(checklog10(j,y)==1)
            break;
        j*=y;
        ct=0;
        ele=0;
        while(ele<=r && ct<=eind)
        {
            ele = j+a[ct++]-1;

            if(ele>r)
            {
                break;
            }
            a[ind++] = ele;

    //        printf("%llu ", ele);

        }
  //      putchar('\n');
    }

    ct=0;
    for(i=0;i<ind;i++)
        if(a[i]>=l && a[i]<=r)
            b[ct++] = a[i];

    b[ct++] = l-1;
    b[ct++] = r+1;

    msort(b, 0, ct-1);
/*
    for(i=0;i<ct;i++)
      printf("%llu ", b[i]);
   putchar('\n');
*/
    lli  mxdif = 0;
    for(i=0;i<ct-1;i++)
    {
        if(b[i+1]-b[i]>mxdif)
        {
            mxdif = b[i+1]-b[i];
     //       printf("%lld %lld %lld\n",b[i], b[i+1], i);
        }
    }
    printf("%llu\n", mxdif-1);

    return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>

#define MAX 1000010
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

void fft(long long n, long long* ar){
    long long i, j, x, g = n, flag = 0;
    while (((g != 1) || flag) && n > 1){
        flag = 0;
        if (g != 1) g *= 0.77425;

        for (i = 0; (i + g) < n; i++){
            if (ar[i] > ar[i + g]){
                flag = 1;
                x = ar[i], ar[i] = ar[i + g], ar[i + g] = x;
            }
        }
    }
}

long long n, x, y, l, r, a, b, A[MAX], B[MAX], ar[MAX];

long long generate(long long b, long long* ar){
    long double p = 1.0;
    long long i, j, x = 1, len = 0;

    while (p < 2e18 && x <= (1LL << 60)){
        ar[len++] = x;
        p *= b, x *= b;
    }
    return len;
}

int main(){
    long long i, j, k, u, v, w, res, prev;

    while (scanf("%lld %lld %lld %lld", &x, &y, &l, &r) != EOF){
        a = generate(x, A);
        b = generate(y, B);

        clr(ar);
        n = 0, res = 0, prev = 1;
        for (i = 0; i < a; i++){
            for (j = 0; j < b; j++){
                ar[n++] = A[i] + B[j];
            }
        }
        ar[n++] = 0, ar[n++] = 1LL << 61;
        fft(n, ar);

        for (i = 1; i < n; i++){
            u = ar[i - 1] + 1, v = ar[i] - 1;
            if (u <= v){
                if (u < l) u = l;
                if (v > r) v = r;
                if ((v - u + 1) > res) res = v - u + 1;
            }
        }

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

#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
	long long ia = *(long long *) a;
	long long ib = *(long long *) b;

	return ia == ib ? 0 : ia < ib ? -1 : 1;
}

int main() {
	static long long aa[100000];
	long long x, y, l, r, a, b, max;
	int k, i;

	scanf("%lld%lld%lld%lld", &x, &y, &l, &r);
	k = 0;
	for (a = 1; ; a *= x) {
		for (b = 1; ; b *= y) {
			if (a + b >= l && a + b <= r)
				aa[k++] = a + b;
			if ((double) b * y >= 1e18)
				break;
		}
		if ((double) a * x >= 1e18)
			break;
	}
	aa[k++] = l - 1;
	aa[k++] = r + 1;
	qsort(aa, k, sizeof *aa, compare);
	max = 0;
	for (i = 1; i < k; i++)
		if (max < aa[i] - aa[i - 1] - 1)
			max = aa[i] - aa[i - 1] - 1;
	printf("%lld\n", max);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        p=p*x;
        br++;
    };
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%i",l-r+1);
    else if (br2==l-r+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            //printf("%lld-%lld=%lld\n",C[i],C[i-1],praz);
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
    }
    printf("%lld",raz);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        p=p*x;
        br++;
    };
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%i",l-r+1);
    else if (br2==l-r+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        if (LLONG_MAX/p<x)
            p=r;
        else p=p*x;
        br++;
    }
    for(i=0;i<br;i++)
        printf("%lld\n",A[i]);
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        if (LLONG_MAX/p1<y)
            p1=r;
        else p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%lld",r-l+1);
    else if (br2==r-l+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    long long x;
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        p=p*x;
        br++;
    };
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%lld",r-l+1);
    else if (br2==r-l+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    long long x;
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        p=p*x;
        br++;
    };
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%lld",r-l+1);
    else if (br2==l-r+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        p=p*x;
        br++;
    };
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%i",r-l+1);
    else if (br2==l-r+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    return 0;
}

#include <stdio.h>
typedef long long ll;
#define MAXN 65
void insertSort(ll* arr, int N) {
    for (int i = 1; i < N; ++i) {
        int j = i - 1;
        ll v = arr[i];
        for ( ; j >= 0 && v < arr[j]; --j)
            arr[j + 1] = arr[j];
        arr[j + 1] = v;
    }
}
ll a1[MAXN], a2[MAXN], s[MAXN * MAXN];
int main() {
    ll x, y, l, r;
    scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
    int N = 0;
    int sz1 = 0, sz2 = 0;
    ll t;
    for (t = 1; t <= r / x; t *= x)
        a1[sz1++] = t;
    a1[sz1++] = t;
    for (t = 1; t <= r / y; t *= y)
        a2[sz2++] = t;
    a2[sz1++] = t;
    for (int i = 0; i < sz1; ++i)
        for (int j = 0; j < sz2; ++j)
            if (a1[i] + a2[j] >= l && a1[i] + a2[j] <= r)
                s[N++] = a1[i] + a2[j];
    s[N++] = l - 1;
    s[N++] = r + 1;
    insertSort(s, N);
    ll ans = 0;
    for (int i = 1; i < N; ++i)
        if (ans < s[i] - s[i - 1] - 1) ans = s[i] - s[i - 1] - 1;
    printf("%I64d\n", ans);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
    for (ll i = 0; i < T; i++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }

        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }
        ll e = r;
        ll ycountr = 0;
        while(e)
        {
            ycountr++;
            e = e/y;
        }
        ll qw = l;
        ll ycountl = -1;
        while(qw)
        {
            ycountl++;
            qw = qw/y;
        }

   //     printf("%lld %lld\n",xcountl,ycountl);

        ll totalcount = 0;
        ll A[1000000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
        for(ll s=ycountl;s<ycountr;s++)
        {
            ll u = 0;
            while(power(x,u)+power(y,s)<l)
            {
                u++;
            }
            while(power(x,u)+power(y,s)<=r&&u<xcountl)
            {
                A[idx] = power(x,u)+power(y,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

*/
 //       printf("\n|%lld %lld|",MIN,MAX);
        mergeSort(A,0,idx-1);
    //          for(ll g=0;g<idx;g++)
 //       printf("|%lld|",A[g]);

       // printf("\n|%lld %lld|",MIN,MAX);
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max)
            max = A[sd]-A[sd-1]-1 ;
          //  if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l;
        if(left<0)left = 0;
        ll right = r-MAX;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
        //  printf("<%lld>",totalcount);
        if(totalcount==0)
        {
            finalanswer = r-l+1 ;
        }
        if(l==1)
        {
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        {printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
    for (ll i = 0; i < T; i++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }
        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }
        ll e = r;
        ll ycountr = 0;
        while(e)
        {
            ycountr++;
            e = e/y;
        }
        ll qw = l;
        ll ycountl = -1;
        while(qw)
        {
            ycountl++;
            qw = qw/y;
        }

    //    printf("%lld %lld\n",xcountl,ycountl);

        ll totalcount = 0;
        ll A[10000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
        for(ll s=ycountl;s<ycountr;s++)
        {
            ll u = 0;
            while(power(x,u)+power(y,s)<l)
            {
                u++;
            }
            while(power(x,u)+power(y,s)<=r&&u<xcountl)
            {
                A[idx] = power(x,u)+power(y,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/

        mergeSort(A,0,idx-1);
         /*       for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max)
            max = A[sd]-A[sd-1]-1 ;
          //  if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l;
        if(left<0)left = 0;
        ll right = r-MAX;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
    //    printf("<%lld>",totalcount);
        if(totalcount==0)
        {
            finalanswer = r-l+1 ;
        }
        if(l==1)
        {
     //   totalcount++;
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
       { printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
    for (ll i = 0; i < T; i++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }
        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }

       // prllf("%lld\n",xcountl);

        ll totalcount = 0;
        ll A[10000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/

        mergeSort(A,0,idx-1);
      /*          for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max)
            max = A[sd]-A[sd-1]-1 ;
            if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l-1;
        if(left<0)left = 0;
        ll right = r-MAX-1;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
        if(l==1)
        {
        totalcount++;
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
       { printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
    for (ll i = 0; i < T; i++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }
        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }

       // prllf("%lld\n",xcountl);

        ll totalcount = 0;
        ll A[10000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/

        mergeSort(A,0,idx-1);
      /*          for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max)
            max = A[sd]-A[sd-1]-1 ;
            if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l;
        if(left<0)left = 0;
        ll right = r-MAX;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
        if(l==1)
        {
        totalcount++;
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
       { printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
   // printf("%lld %lld %lld",MIN,MAX,max);
    for (ll tqw = 0; tqw < T; tqw++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }

        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }
        ll e = r;
        ll ycountr = 0;
        while(e)
        {
            ycountr++;
            e = e/y;
        }
        ll qw = l;
        ll ycountl = -1;
        while(qw)
        {
            ycountl++;
            qw = qw/y;
        }

         // printf("%lld %lld\n",xcountl,xcountr);

        ll totalcount = 0;
        ll A[1000000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
        for(ll s=ycountl;s<ycountr;s++)
        {
            ll u = 0;
            while(power(x,u)+power(y,s)<l)
            {
                u++;
            }
            while(power(x,u)+power(y,s)<=r&&u<xcountl)
            {
                A[idx] = power(x,u)+power(y,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

*/
 //       printf("\n|%lld %lld|",MIN,MAX);
        mergeSort(A,0,idx-1);
        /* for(ll g=0;g<idx;g++)
      printf("|%lld|",A[g]);*/

       // printf("\n|%lld %lld|",MIN,MAX);
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max&&A[sd]>0&&A[sd-1]>0)
            max = A[sd]-A[sd-1]-1 ;
          //  if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l;
        if(left<0)left = 0;
        ll right = r-MAX;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
        //  printf("<%lld>",totalcount);
        if(totalcount==0)
        {
            finalanswer = r-l+1 ;
        }
        if(l==1)
        {
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        {printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
   // printf("%lld %lld %lld",MIN,MAX,max);
    for (ll tqw = 0; tqw < T; tqw++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }

        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }
        ll e = r;
        ll ycountr = 0;
        while(e)
        {
            ycountr++;
            e = e/y;
        }
        ll qw = l;
        ll ycountl = -1;
        while(qw)
        {
            ycountl++;
            qw = qw/y;
        }

    //    printf("%lld %lld\n",xcountl,xcountr);

        ll totalcount = 0;
        ll A[1000000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
        for(ll s=ycountl;s<ycountr;s++)
        {
            ll u = 0;
            while(power(x,u)+power(y,s)<l)
            {
                u++;
            }
            while(power(x,u)+power(y,s)<=r&&u<xcountl)
            {
                A[idx] = power(x,u)+power(y,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

*/
 //       printf("\n|%lld %lld|",MIN,MAX);
        mergeSort(A,0,idx-1);
    //          for(ll g=0;g<idx;g++)
 //       printf("|%lld|",A[g]);

       // printf("\n|%lld %lld|",MIN,MAX);
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max)
            max = A[sd]-A[sd-1]-1 ;
          //  if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l;
        if(left<0)left = 0;
        ll right = r-MAX;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
        //  printf("<%lld>",totalcount);
        if(totalcount==0)
        {
            finalanswer = r-l+1 ;
        }
        if(l==1)
        {
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        {printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
    ll i, j, k;
    ll n1 = m - l + 1;
    ll n2 =  r - m;

    /* create temp arrays */
    ll L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back llo arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        ll m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main(ll argc, char const *argv[])
{
    ll T;
 //   scanf("%lld",&T);
    T = 1;
    for (ll i = 0; i < T; i++)
    {
        ll x,y,l,r ;
        scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
        ll o = l;
        ll xcountl =  -1;
        while(o)
        {
            xcountl++;
            o = o/x ;
        }
        ll p = r;
        ll xcountr = 0;
        while(p)
        {
            xcountr++;
            p = p/x;
        }

       // prllf("%lld\n",xcountl);

        ll totalcount = 0;
        ll A[10000];
        ll idx = 0;
        for(ll s=xcountl;s<xcountr;s++)
        {
            ll u = 0;
            while(power(y,u)+power(x,s)<l)
            {
                u++;
            }
            while(power(y,u)+power(x,s)<=r)
            {
                A[idx] = power(y,u)+power(x,s) ;
                if(A[idx]>MAX)
                MAX = A[idx] ;
                if(A[idx]<MIN)
                MIN = A[idx] ;
                idx++;
                totalcount++;
                u++;
            }
        }
      /*  for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/

        mergeSort(A,0,idx-1);
      /*          for(ll g=0;g<idx;g++)
        printf("|%lld|",A[g]);

        printf("\n|%lld %lld|",MIN,MAX);*/
        for(ll sd = 1;sd<idx;sd++)
        {
            if(A[sd]-A[sd-1]-1>max)
            max = A[sd]-A[sd-1]-2 ;
            if(max<0)max=0;
        }
        ll finalanswer = 0;
        ll left = MIN-l-1;
        if(left<0)left = 0;
        ll right = r-MAX-1;
        if(right<0)right = 0;
      /*  printf("l %lld r %lld %lld|",left,right,max);*/
        if(left>right)
        {
            if(left>max)
            {
                finalanswer = left;
            }
            else
            {
                finalanswer = max ;
            }
        }
        else
        {
            if(right>max)
            {
                finalanswer = right;
            }
            else
            {
                finalanswer = max ;
            }
        }
        if(l==1)
        {
        totalcount++;
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
       { printf("%lld\n",finalanswer);}
        }

        else{
        if(r-l+1-totalcount==0)
        printf("0\n");
        else
        { printf("%lld\n",finalanswer);}
        }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MOD 1000000007
#define ll long long

ll cmpfunc(const void *a, const void *b){
    return (*(ll*)a - *(ll*)b);
}

int max(int a,int b){
    return (a>b)?a:b;
}

ll power(ll x,ll y){
    ll ans=1;
    for(ll i=0;i<y;i++) ans*=x;
    return ans;
}

int main(void){
    ll x,y,l,r;
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    ll b,d;
    ll i,j;
    if(x>y){
        ll ans=x;
        x=y;
        y=ans;
    }
    //a=floor(1.0*log(l)/log(x));
    b=(ll)floor(1.0*log(r)/log(x))+1;
    printf("b=%lld\n",b);
    //c=floor(1.0*log(l)/log(y));
    d=(ll)floor(1.0*log(r)/log(y))+1;
    printf("d=%lld\n",d);

    //a<=b,c<=d
    ll un[10000]; //stores unlucky numbers
    int count=0; //number of unlucky numbers
    for(i=0;i<=b;i++){
        for(j=0;j<=d;j++){
            if(power(x,i)>r-power(y,j)) continue;
            ll sum=(ll)power(x,i)+(ll)power(y,j);
            printf("sum=%lld\n",sum);
            if(sum>=l){
                un[count]=sum;
                count++;
            }
        }
    }
    printf("count=%lld\n",count);
    if(count==0){
        printf("%lld\n",r-l+1);
        return 0;
    }
    ll unlucky[count];
    for(i=0;i<count;i++) unlucky[i]=un[i];
    ll c,d1,t;
  for (c = 1 ; c <= count - 1; c++) {
    d1 = c;

    while ( d1 > 0 && unlucky[d1] < unlucky[d1-1]) {
      t          = unlucky[d1];
      unlucky[d1]   = unlucky[d1-1];
      unlucky[d1-1] = t;

      d1--;
    }
  }

    for(i=0;i<count;i++) printf("%lld ",unlucky[i]);
    putchar('\n');
    ll max=0;
    for(i=0;i<count-1;i++){
        if(unlucky[i+1]-unlucky[i]-1>max) max=unlucky[i+1]-unlucky[i]-1;
    }
    if(r-unlucky[count-1]>max) max=r-unlucky[count-1];
    if(unlucky[0]-l>max) max=unlucky[0]-l;

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

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MOD 1000000007
#define ll long long

ll cmpfunc(const void *a, const void *b){
    return (*(ll*)a - *(ll*)b);
}

int max(int a,int b){
    return (a>b)?a:b;
}

ll power(ll x,ll y){
    ll ans=1;
    for(ll i=0;i<y;i++) ans*=x;
    return ans;
}

int main(void){
    ll x,y,l,r;
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    ll b,d;
    ll i,j;
    //a=floor(1.0*log(l)/log(x));
    b=(ll)floor(1.0*log(r)/log(x))+1;
    //printf("b=%lld\n",b);
    //c=floor(1.0*log(l)/log(y));
    d=(ll)floor(1.0*log(r)/log(y))+1;
    //printf("d=%lld\n",d);

    //a<=b,c<=d
    ll un[10000]; //stores unlucky numbers
    int count=0; //number of unlucky numbers
    for(i=0;i<=b;i++){
        for(j=0;j<=d;j++){
            if(power(x,i)>r-power(y,j)) continue;
            ll sum=(ll)power(x,i)+(ll)power(y,j);
            if(sum>=l){
                            //printf("sum=%lld\n",sum);
                un[count]=sum;
                count++;
            }
        }
    }
    //printf("count=%lld\n",count);
    if(count==0){
        printf("%lld\n",r-l+1);
        return 0;
    }
    ll unlucky[count];
    for(i=0;i<count;i++) unlucky[i]=un[i];
    ll c,d1,t;
  for (c = 1 ; c <= count - 1; c++) {
    d1 = c;

    while ( d1 > 0 && unlucky[d1] < unlucky[d1-1]) {
      t          = unlucky[d1];
      unlucky[d1]   = unlucky[d1-1];
      unlucky[d1-1] = t;

      d1--;
    }
  }

    //for(i=0;i<count;i++) printf("%lld ",unlucky[i]);
    //putchar('\n');
    ll max=0;
    for(i=0;i<count-1;i++){
        if(unlucky[i+1]-unlucky[i]-1>max) max=unlucky[i+1]-unlucky[i]-1;
    }
    //printf("%lld %lld\n",max,r-unlucky[count-1]);
    if(r-unlucky[count-1]>max) max=r-unlucky[count-1];
    if(unlucky[0]-l>max) max=unlucky[0]-l;

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

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MOD 1000000007
#define ll long long

ll cmpfunc(const void *a, const void *b){
    return (*(ll*)a - *(ll*)b);
}

int max(int a,int b){
    return (a>b)?a:b;
}

ll power(ll x,ll y){
    ll ans=1;
    for(ll i=0;i<y;i++) ans*=x;
    return ans;
}

int main(void){
    ll x,y,l,r;
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    ll b,d;
    ll i,j;
    if(x>y){
        ll ans=x;
        x=y;
        y=ans;
    }
    //a=floor(1.0*log(l)/log(x));
    b=(ll)floor(1.0*log(r)/log(x))+1;
    //printf("b=%lld\n",b);
    //c=floor(1.0*log(l)/log(y));
    d=(ll)floor(1.0*log(r)/log(y))+1;
    //printf("d=%lld\n",d);

    //a<=b,c<=d
    ll un[10000]; //stores unlucky numbers
    int count=0; //number of unlucky numbers
    for(i=0;i<=b;i++){
        for(j=0;j<=d;j++){
            if(power(x,i)>r-power(y,j)) continue;
            ll sum=(ll)power(x,i)+(ll)power(y,j);
            //printf("sum=%lld\n",sum);
            if(sum>=l){
                un[count]=sum;
                count++;
            }
        }
    }
    //printf("count=%lld\n",count);
    if(count==0){
        printf("%lld\n",r-l+1);
        return 0;
    }
    ll unlucky[count];
    for(i=0;i<count;i++) unlucky[i]=un[i];
    ll c,d1,t;
  for (c = 1 ; c <= count - 1; c++) {
    d1 = c;

    while ( d1 > 0 && unlucky[d1] < unlucky[d1-1]) {
      t          = unlucky[d1];
      unlucky[d1]   = unlucky[d1-1];
      unlucky[d1-1] = t;

      d1--;
    }
  }

    //for(i=0;i<count;i++) printf("%lld ",unlucky[i]);
    //putchar('\n');
    ll max=0;
    for(i=0;i<count-1;i++){
        if(unlucky[i+1]-unlucky[i]-1>max) max=unlucky[i+1]-unlucky[i]-1;
    }
    if(r-unlucky[count-1]>max) max=r-unlucky[count-1];
    if(unlucky[0]-l>max) max=unlucky[0]-l;

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

    return 0;
}

#include<stdio.h>

typedef long long int lli;

void msort(lli a[], int s, int e)
{
    if(e-s!=0)
    {
        msort(a, s, (s+e)/2);
        msort(a, (s+e)/2 + 1, e);
    }
    else
        return;
    lli b[e-s+1];
    int  i=s, j=(s+e)/2 + 1, n;
    for(n=0; i<=(s+e)/2 && j<=e;n++)
    {
        if(a[i]<=a[j])
            b[n]=a[i++];
        else
            b[n]=a[j++];
    }
    if(i<=(s+e)/2)
        for(;i<=(s+e)/2;i++, n++)
            b[n] = a[i];
    else
        for(;j<=e;j++,n++)
            b[n]=a[j];
    for(i=0;i<n;i++, s++)
        a[s] = b[i];
}

int main()
{
    lli i,j,ele;
    lli x,y,l,r;
    lli a[100000], b[100000];
    scanf("%lld %lld %lld %lld", &x, &y, &l, &r);
    if(y<x)
    {
        y = x+y;
        x = y - x;
        y = y - x;
    }

    int ct = 0, ind=0;
    i=1;
    while(i<r)
    {
        a[ind++] = i+1;
        i*=x;
    }

    int sind,eind;
    sind=0;
    eind = ind-1;

    j=1;

    while(j<r)
    {
        j*=y;
        ct=0;
        ele=0;
        while(ele<=r && ct<=eind)
        {
            ele = j+a[ct++]-1;

            if(ele>r)
            {
                break;
            }
            a[ind++] = ele;
        }
    }

    ct=0;
    for(i=0;i<ind;i++)
        if(a[i]>=l && a[i]<=r)
            b[ct++] = a[i];

    b[ct++] = l-1;
    b[ct++] = r+1;

    msort(b, 0, ct-1);

    //for(i=0;i<ct;i++)
      //  printf("%lld ", b[i]);
   // putchar('\n');

    lli  mxdif = 0;
    for(i=0;i<ct-1;i++)
    {
        if(b[i+1]-b[i]>mxdif)
        {
            mxdif = b[i+1]-b[i];
     //       printf("%lld %lld %lld\n",b[i], b[i+1], i);
        }
    }
    printf("%lld\n", mxdif-1);

    return 0;
}

#include<stdio.h>

typedef long long int lli;

void msort(lli a[], int s, int e)
{
    if(e-s!=0)
    {
        msort(a, s, (s+e)/2);
        msort(a, (s+e)/2 + 1, e);
    }
    else
        return;
    lli b[e-s+1];
    int  i=s, j=(s+e)/2 + 1, n;
    for(n=0; i<=(s+e)/2 && j<=e;n++)
    {
        if(a[i]<=a[j])
            b[n]=a[i++];
        else
            b[n]=a[j++];
    }
    if(i<=(s+e)/2)
        for(;i<=(s+e)/2;i++, n++)
            b[n] = a[i];
    else
        for(;j<=e;j++,n++)
            b[n]=a[j];
    for(i=0;i<n;i++, s++)
        a[s] = b[i];
}

int main()
{
    lli i,j,ele;
    lli x,y,l,r;
    lli a[100000], b[100000];
    scanf("%lld %lld %lld %lld", &x, &y, &l, &r);
    if(y<x)
    {
        y = x+y;
        x = y - x;
        y = y - x;
    }

    int ct = 0, ind=0;
    i=1;
    while(i<r)
    {
        a[ind++] = i+1;
        i*=x;
    }

    int sind,eind;
    sind=0;
    eind = ind-1;

    j=1;

    while(j<r)
    {
        j*=y;
        ct=0;
        ele=0;
        while(ele<=r && ct<=eind)
        {
            ele = j+a[ct++]-1;

            if(ele>r)
            {
                break;
            }
            a[ind++] = ele;
        }
    }

    ct=0;
    for(i=0;i<ind;i++)
        if(a[i]>=l && a[i]<=r)
            b[ct++] = a[i];

    b[ct++] = l-1;
    b[ct++] = r-1;

    msort(b, 0, ct-1);

    //for(i=0;i<ct;i++)
      //  printf("%lld ", b[i]);
   // putchar('\n');

    lli  mxdif = 0;
    for(i=0;i<ct-1;i++)
    {
        if(b[i+1]-b[i]>mxdif)
        {
            mxdif = b[i+1]-b[i];
     //       printf("%lld %lld %lld\n",b[i], b[i+1], i);
        }
    }
    printf("%lld\n", mxdif-1);

    return 0;
}

#include<stdio.h>

typedef unsigned long long int lli;

void msort(lli a[], int s, int e)
{
    if(e-s!=0)
    {
        msort(a, s, (s+e)/2);
        msort(a, (s+e)/2 + 1, e);
    }
    else
        return;
    lli b[e-s+1];
    int  i=s, j=(s+e)/2 + 1, n;
    for(n=0; i<=(s+e)/2 && j<=e;n++)
    {
        if(a[i]<=a[j])
            b[n]=a[i++];
        else
            b[n]=a[j++];
    }
    if(i<=(s+e)/2)
        for(;i<=(s+e)/2;i++, n++)
            b[n] = a[i];
    else
        for(;j<=e;j++,n++)
            b[n]=a[j];
    for(i=0;i<n;i++, s++)
        a[s] = b[i];
}

int checklog10(lli a, lli b)
{
    lli i=0,j,k;
    while(a!=0)
    {
        a/=10;
        i++;
    }
    while(b!=0)
    {
        b/=10;
        i++;
    }
    if(i>18)
        return 1;
    return 0;
}

int main()
{
    lli i,j,ele;
    lli x,y,l,r;
    lli a[100000], b[100000];
    scanf("%llu %llu %llu %llu", &x, &y, &l, &r);
    if(y<x)
    {
        y = x+y;
        x = y - x;
        y = y - x;
    }

    int ct = 0, ind=0;
    i=1;
    while(i<r)
    {
        a[ind++] = i+1;
        if(checklog10(i, x)==1)
            break;
        i*=x;
    }
/*
    for(i=0;i<ind;i++)
        printf("%llu ", a[i]);
    putchar('\n');
  */
    int eind;
    eind = ind-1;

    j=1;

    while(j<r)
    {
        if(checklog10(j,y)==1)
            break;
        j*=y;
        ct=0;
        ele=0;
        while(ele<=r && ct<=eind)
        {
            ele = j+a[ct++]-1;

            if(ele>r)
            {
                break;
            }
            a[ind++] = ele;

    //        printf("%llu ", ele);

        }
  //      putchar('\n');
    }

    ct=0;
    for(i=0;i<ind;i++)
        if(a[i]>=l && a[i]<=r)
            b[ct++] = a[i];

    b[ct++] = l-1;
    b[ct++] = r+1;

    msort(b, 0, ct-1);
/*
    for(i=0;i<ct;i++)
      printf("%llu ", b[i]);
   putchar('\n');
*/
    lli  mxdif = 0;
    for(i=0;i<ct-1;i++)
    {
        if(b[i+1]-b[i]>mxdif)
        {
            mxdif = b[i+1]-b[i];
     //       printf("%lld %lld %lld\n",b[i], b[i+1], i);
        }
    }
    printf("%llu\n", mxdif-1);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MOD 1000000007
#define ll long long

ll cmpfunc(const void *a, const void *b){
    return (*(ll*)a - *(ll*)b);
}

ll cmp(const void *a, const void *b){
    return (*(ll*)b - *(ll*)a);
}

int max(int a,int b){
    return (a>b)?a:b;
}

int main(void){
    ll x,y,l,r;
    scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
    ll a,b,c,d;
    ll i,j;
    if(x>y){
        ll ans=x;
        x=y;
        y=ans;
    }
    //a=floor(1.0*log(l)/log(x));
    b=floor(1.0*log(r)/log(x));
    //c=floor(1.0*log(l)/log(y));
    d=floor(1.0*log(r)/log(y));

    //a<=b,c<=d
    ll unlucky[10000]; //stores unlucky numbers
    int count=0; //number of unlucky numbers
    for(i=0;i<=b;i++){
        for(j=0;j<=d;j++){
            ll sum=pow(x,i)+pow(y,j);
            if(sum>=l && sum<=r){
                unlucky[count]=sum;
                count++;
            }
        }
    }
    //printf("count=%lld\n",count);
    if(count==0){
        printf("%lld\n",r-l+1);
        return 0;
    }
    qsort(unlucky,count,sizeof(ll),cmpfunc);
    ll max=0;
    for(i=0;i<count-1;i++){
        if(unlucky[i+1]-unlucky[i]-1>max) max=unlucky[i+1]-unlucky[i]-1;
    }
    if(r-unlucky[count-1]>max) max=r-unlucky[count-1];
    if(unlucky[0]-l>max) max=unlucky[0]-l;

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

    return 0;
}