Unlucky year in Berland is such a year that its number n can be represented as n = x^{a} + y^{b}, 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 = 2^0 + 3^1, 17 = 2^3 + 3^2 = 2^4 + 3^0) 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.


-----Input-----

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


-----Output-----

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.


-----Examples-----
Input
2 3 1 10

Output
1

Input
3 5 10 22

Output
8

Input
2 3 3 5

Output
0



-----Note-----

In 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].

Difficulty: interview

Starter code:

Solutions:
x,y,l,r=list(map(int,input().split()))
b=set()
a=0
b.add(l-1)
b.add(r+1)
for i in range(100):
  xx=x**i
  if xx>r: break
  for j in range(100):
    rr=xx+(y**j)
    if rr>r: break
    if rr>=l:
      b.add(rr)
b=sorted(list(b))
for i in range(1,len(b)):
  a=max(a,b[i]-b[i-1]-1)
print(a)


x, y, l, r = list(map(int, input().split()))
xs = [x ** i for i in range(61) if x ** i <= 10 ** 18]
ys = [y ** i for i in range(61) if y ** i <= 10 ** 18]
ps = set()
for i in xs:
	for j in ys:
		if l <= i + j <= r:
			ps.add(i + j)
ps.add(l - 1)
ps.add(r + 1)
ps = sorted(list(ps))
ans = 0
for i in range(1, len(ps)):
	ans = max(ans, ps[i] - ps[i - 1] - 1)
print(ans)




x, y, l, r = map(int, input().split())
data = []
tx = 1
while (tx < r + 3):
    ty = 1
    while (ty < r + 3):
        data.append(tx + ty)
        ty *= y
    tx *= x
data = sorted(set(data))
data = [elem for elem in data if l <= elem and elem <= r]
data = [l - 1] + data + [r + 1] 
ans = 0
for i in range(1, len(data)):
    ans = max(ans, data[i] - data[i - 1] - 1)
print(ans)

a,b,c,d = list(map(int, input().split(' ')))

MXV = 10 ** 18

apows, bpows = [], []
aa, bb = 1, 1
while aa <= MXV:
    apows.append(aa)
    aa *= a
while bb <= MXV:
    bpows.append(bb)
    bb *= b

vals = set()
for i in range(len(apows)):
    for j in range(len(bpows)):
        vals.add(apows[i] + bpows[j])

vlist = list(vals)
vlist.sort()
ans = 0
last = c-1 
for v in vlist:
    if c <= v and v <= d:
        ans = max(ans, v - last - 1)
        last = v
ans = max(ans, d-last)

print(ans)


x, y, l, r = map(int, input().split())
v = [l - 1, r + 1]
for a in range(0, 60):
	if x ** a > r:
		break
	for b in range(0, 60):
		if y ** b > r:
			break
		if l <= x ** a + y ** b <= r:
			v += [x ** a + y ** b]
v.sort()
ans = 0
for i in range(len(v) - 1):
	ans = max(ans, v[i + 1] - v[i] - 1)
print(ans)

x, y, l, r = list(map(int, input().split()))

xs = []
now = 1
while True:
    if now > r:
        break
    xs.append(now)
    now *= x

ys = []
now = 1
while True:
    if now > r:
        break
    ys.append(now)
    now *= y

ress = [x + y for x in xs for y in ys]
ress = [i for i in ress if l <= i <= r]
ress.append(l - 1)
ress.append(r + 1)
ress = list(set(ress))
ress = sorted(ress)

lst = -1
ans = 0
for i in ress:
    if lst != -1:
        ans = max(ans, i - lst - 1)
    lst = i

print(ans)


x, y, l, r = list(map(int, input().split()))
a = []
for i in range(70):
	for j in range(70):
		cur = x ** i + y ** j
		if cur > r:
			continue
		if cur <= r and cur >= l:
			a.append(cur)
a.sort()
ans = 0
last = l - 1
for it in a:
	ans = max(ans, it - last - 1)
	last = it
ans = max(ans, r - last)
print(ans)



R=lambda:list(map(int,input().strip().split()))
[x,y,l,r]=R()
a=list()
b=list()
cur=1
while(cur<r):
    a.append(cur) 
    cur*=x 
cur=1
while(cur<r):
    b.append(cur) 
    cur*=y 
    
s=set()
s.add(l-1)
s.add(r+1)
for p in a:
    for q in b:
        s.add(p+q) 
s=list(s)
s.sort()
ml=0
for i in range(len(s)-1):
    if s[i]>=l-1 and s[i+1]<=r+1:
        ml=max(ml,s[i+1]-s[i]-1)
print(ml)

x,y,l,r = list(map(int,input().split()))
a = []
f = 1
while f < r:
	g = 1
	while f + g <= r:
		if f + g >= l:
			a.append(f + g)
		g *= y
	f *= x
a += [l-1,r+1]
a.sort()
m = 1
for i in range(1,len(a)):
	if a[i-1] != a[i]:
		a[m] = a[i]
		m += 1
ans = 0
for i in range(1,m):
	ans = max(ans, a[i] - a[i-1] - 1)
print(ans)


x, y, l, r = list(map(int, input().split()))
rx = 0
while x**rx < r:
    rx += 1
ry = 0
while y**ry < r:
    ry += 1

arr = [l - 1, r + 1]
for i in range(rx):
    for j in range(ry):
        tmp = x**i + y**j
        if l <= tmp <= r:
            arr.append(tmp)
arr = list(sorted(set(arr)))

ans = 0
for i in range(1, len(arr)):
    ans = max(ans, arr[i] - arr[i - 1] - 1)
print(ans)


x,y,l,r = list(map(int, input().split()))
xx = 1
a = [0, 1e20]
for i in range(60):
    yy = 1
    while xx + yy <= r:
        a.append(xx+yy)
        yy *= y
    xx *= x
a.sort()
ans = 0
for i in range(len(a)-1):
    ll = max(a[i]+1, l)
    rr = min(a[i+1]-1, r)
    ans = max(ans, rr - ll+1)
    #~ print(i, ans, a[i], a[i+1])
print(ans)


x,y,l,r = list(map(int, input().split()))
lx = []
ly = []
for i in range(65):
    if x**i > r: break
    lx.append(x**i)
for i in range(65):
    if y**i > r: break
    ly.append(y**i)

ans = set()
for i in lx:
    for j in ly:
        if l <= i+j <= r:
             ans.add(i+j)

ans.add(l-1)
ans.add(r+1)
ans = sorted(list(ans))
anslen = 0
for i in range(1, len(ans)):
    anslen = max(anslen, ans[i]-ans[i-1]-1)

#print(ans)
print(anslen)


arr = [int(x) for x in input().split()]
a = arr[0]
b = arr[1]
l = arr[2]
r = arr[3]

pivots = [];

pivots.append(l - 1);
for aTimes in range(65):
    for bTimes in range(65):
        now = a ** aTimes + b**bTimes;
        if now < l:
            continue;
        if now > r:
            break;
        pivots.append(now);
pivots.append(r + 1);

pivots.sort();
ans = 0;
for i in range(len(pivots)):
    if i == 0:
        continue;
    ans = max(pivots[i] - pivots[i - 1] - 1,ans);

print(ans);

x, y, l, r = [int(x) for x in input().split()]
p_x = [1]
p_y = [1]
for i in range(1, 61):
  p_x.append(p_x[len(p_x) - 1] * x)
  p_y.append(p_y[len(p_y) - 1] * y)
possible = set([0])
for i in range(61):
  for j in range(61):
    possible.add(p_x[i] + p_y[j])
a = []
for x in possible:
  a.append(x)
a.sort()
ans = 0
for i in range(1, len(a)):
  l1 = max(a[i - 1], l - 1)
  r1 = min(a[i], r + 1)
  ans = max(ans, r1 - l1 - 1)
print(ans)

x,y,l,r = list(map(int, input().split()))
bad = []
for i in range(61):
    for j in range(61):
        cr = x ** i + y ** j
        if l <= cr <= r: 
            bad.append(x ** i + y ** j)
bad = [l] + bad + [r]
bad.sort()
mx = 0
if len(bad) == 2:
    print(max(mx, bad[1] - bad[0] + 1))
    return
for i in range(len(bad) - 1):
    cr =  bad[i + 1] - bad[i] - 1
    if i == 0 or i == len(bad) - 2:
        cr += 1
    mx = max(mx, cr)
print(mx)


x, y, l, r = map(int, input().split(' '))
a = []
for i in range(65):
    for j in range(65):
        t = x**i + y**j
        if t <= r and t >= l:
            a.append(t)
a.sort()
if len(a) == 0:
    print(r - l + 1)
    return
ans = max(a[0] - l, r - a[-1])
for i in range(1, len(a)):
    ans = max(ans, a[i]-a[i-1]-1)

print(ans)

x,y,l,r = list(map(int, input().split()))

unl = []
for i in range(0, 70):
    for j in range(0, 70):
        xx = x**i + y**j
        if ( xx>=l and xx<=r):
            unl.append(xx)

unl.sort()

if (len(unl) == 0) :
    print(r - l + 1)
    return

diff = []
if (unl[0] != l) :
    diff.append(unl[0] - l)

if ( unl[-1] != r) :
    diff.append(r - unl[-1])

for i in range(1,len(unl)):
    d = (unl[i] - unl[i-1] -1)
    if ( d > 0):
        diff.append(d)


diff.sort();

if (len(diff) == 0) :
    print(0)

else :
    print(diff[-1])





[x, y, l, r] = list(map(int, input().rstrip().split()))
data = [l]

lbad = False
rbad = False
for i in range(0, 61):
    for j in range(0, 61):
        num = x ** i + y ** j
        if num == l:
            lbad = True
        if num == r:
            rbad = True
        if l <= num <= r:
            data.append(num)
        elif num > r:
            break
data.sort()
max_time = 0
data.append(r if rbad else r + 1)
data[0] = (l if lbad else l -1)
for i in range(0, len(data) - 1):
    max_time = max(data[i + 1] - data[i] - 1, max_time)
print(max_time)


x, y, l, r = list(map(int, input().split()))
degree_x = []
degree_y = []
i = 0
while x ** i <= r:
    degree_x.append(x ** i)
    i += 1
i = 0
while y ** i <= r:
    degree_y.append(y ** i)
    i += 1
sad_years = []
for i in degree_x:
    for j in degree_y:
        sad_years.append(i + j)
sad_years.sort()
res = 0
i = 0
while i < len(sad_years):
    if sad_years[i] < l or sad_years[i] > r:
        sad_years.pop(i)
    else:
        i += 1
res = 0
sad_years = [l - 1] + sad_years + [r + 1]
for i in range(len(sad_years) - 1):
    res = max(res, sad_years[i + 1] - sad_years[i] - 1)
print(res)


x,y,l,r=list(map(int,input().split()))
A=[l-1]
for i in range(100):
	if x**i>r:
		break
	for j in range(100):
		if y**j>r:
			break
		t=x**i+y**j
		if t>=l and t<=r:
			A.append(t)
A.append(r+1)
A.sort()
ans=0
for i in range(len(A)-1):
	ans=max(ans,A[i+1]-A[i]-1)
print(ans)



x, y, l, r = list(map(int, input().split()))


def gen_list(var):
    cur = 1
    while cur <= r:
        yield cur
        cur *= var

x_list = list(gen_list(x))
# print(x_list)
y_list = list(gen_list(y))
# print(y_list)

numbers = [l-1, r+1]
for _x in x_list:
    for _y in y_list:
        n = _x + _y
        if n < l or n > r:
            continue
        numbers.append(n)

numbers = sorted(set(numbers))
# print(numbers)

if len(numbers) < 2:
    print('0')
    return

max_happy = max([numbers[i+1]-numbers[i]-1 for i in range(len(numbers) - 1)], default=0)

print(max_happy)


x,y,l,r=list(map(int,input().split()))

xx=[]
yy=[]
p=1
xx.append(l-1);
while p<=r:
    q=1
    while p+q<=r:
        if p+q>=l and p+q<=r:
            xx.append(p+q)
        q*=y
    p*=x
xx.append(r+1)
xx.sort()

for i in range(0,len(xx)-1):
    yy.append(xx[i+1]-xx[i]-1)

print(max(yy))


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield(number)   

input_parser = parser()

def get_word():
    nonlocal input_parser
    return next(input_parser)

def get_number():
    data = get_word()
    try:
        return int(data)
    except ValueError:
        return float(data)


x = get_number()
y = get_number()
l = get_number()
r = get_number()
n1 = 1;
a = list()
a.append(l - 1)
for i in range(0, 300):
    if n1 > r:
        break
    n2 = 1 
    for j in range(0, 300):
        if n1 + n2 > r:
            break
        if n1 + n2 >= l and n1 + n2 <= r:
            a.append(n1 + n2)
        n2 = n2 * y
    n1 = n1 * x
    
a.append(r + 1)
a.sort()
ans = 0
for i in range(0, len(a) - 1):
    ans = max(ans, a[i + 1] - a[i] - 1)
print(ans)

from sys import stdin
import re
import math

def readInt(count=1):
    m = re.match('\s*' + ('([+-]?\d+)\s*' * count), stdin.readline())
    if m is not None:
        ret = []
        for i in range(1, m.lastindex + 1):
            ret.append(int(m.group(i)))
        return ret
    return None

x, y, l, r = readInt(4)

max = 0
unhappyYears = [l-1]
xa = 1
while xa <= r:
    yb = 1
    while True:
        val = xa + yb
        if val > r:
            break
        if val >= l and val not in unhappyYears:
            unhappyYears.append(val)
        yb *= y
    xa *= x
unhappyYears.sort()
unhappyYears.append(r+1)

for i in range(len(unhappyYears)-1):
    cur = unhappyYears[i+1] - unhappyYears[i] - 1
    if cur > max:
        max = cur
print(max)

def power(a,p):
	if p==0:
		return 1
	lol=power(a,p//2)
	lol=lol*lol
	if p%2==1:
		return a*lol
	else:
		return lol

x,y,l,r=map(int,input().split())

i=0
d={}

while 1:
	k=power(x,i)
	j=0
	if k>r:
		break
	while 1:
		lol=power(y,j)
		if k+lol>r:
			break
		d[k+lol]=1
		j+=1
	i+=1
last=l-1
d[r+1]=1
arr=[]
for k in d.keys():
	arr.append(k)
ans=0
arr.sort()
for k in arr:
	#print(k)
	if k>=l:
		ans=max(ans,k-last-1)
		last=k
		if last>=r:
			break
print(ans)