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

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Mathematics

Complete search


Solutions:
(x, y, l, r) = map(int, input().split())
t = []
a = 1
while a <= r:
	b = 1
	while a + b <= r:
		if a + b >= l:
			t.append(a + b)
		b *= y
	a *= x
t.sort()
print(max((y - x for (x, y) in zip([l - 1] + t, t + [r + 1]))) - 1)


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


(x, y, l, r) = map(int, input().split())
xi = [1] * 62
yi = [1] * 62
a = 0
b = 0
while 1000000000000000000 > xi[a]:
	xi[a + 1] = x ** a
	a += 1
while 1000000000000000000 > yi[b]:
	yi[b + 1] = y ** b
	b += 1
a += 1
b += 1
n = a * b
timei = [0] * n
for i in range(a):
	for j in range(b):
		timei[i * b + j] = xi[i] + yi[j]
timei.sort()
i = 0
while n > i and l > timei[i]:
	i += 1
ans = min(timei[i] - 1, r) - l + 1
last_p = timei[i]
while n > i and r > timei[i]:
	ans = max(ans, timei[i] - last_p - 1)
	last_p = timei[i]
	i += 1
ans = max(ans, min(timei[i] - 1, r) - last_p)
print(ans)


(x, y, l, r) = map(int, input().split())
s = set()
px = 1
py = 1
while px <= r:
	while py <= r:
		if l <= px + py and px + py <= r:
			s.add(px + py)
		py = py * y
	py = 1
	px = px * x
s.add(l - 1)
s.add(r + 1)
t = []
for x in s:
	t.append(x)
t.sort()
ans = 0
for i in range(1, len(t)):
	ans = max(ans, t[i] - t[i - 1] - 1)
print(ans)


(x, y, l, r) = 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(anslen)


(x, y, l, r) = 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)


def sol(x, y, l, r):
	myset = set()
	_x = 1
	v = []
	while _x <= 1000000000000000000:
		_y = 1
		while _x + _y <= 1000000000000000000:
			if _x + _y >= l and _x + _y <= r:
				myset.add(_x + _y)
			_y *= y
		_x *= x
	for s in myset:
		v.append(s)
	v.append(l - 1)
	v.append(r + 1)
	v.sort()
	ans = 0
	for i in range(1, len(v)):
		ans = max(ans, v[i] - v[i - 1] - 1)
	return ans
(x, y, l, r) = input().split()
print(sol(int(x), int(y), int(l), int(r)))


def main():
	(a, b, c, d) = [int(x) for x in input().split()]
	d += 1
	c -= 1
	ans = 0
	nums = []
	for i in range(70):
		for j in range(70):
			nums.append(a ** i + b ** j)
	nums.append(0)
	nums.sort()
	n = len(nums)
	for i in range(1, n):
		p = max(c, nums[i - 1])
		q = min(d, nums[i])
		ans = max(ans, q - p - 1)
	print(ans)
main()


import math
(x, y, l, r) = input().strip().split(' ')
(x, y, l, r) = (int(x), int(y), int(l), int(r))
list1 = []
a = int(math.log(r, x))
b = int(math.log(r, y))
for i in range(0, a + 1):
	for j in range(0, b + 1):
		f = x ** i + y ** j
		if f >= l and f <= r:
			list1.append(f)
if len(list1) == 0:
	print(r - l + 1)
	exit()
list1.sort()
list2 = [list1[0] - l, r - list1[-1]]
for i in range(len(list1) - 1):
	list2.append(list1[i + 1] - list1[i] - 1)
print(max((max(list2), 0)))


import sys
import math

def is_unlucky(year, x, y):
	max_a = int(math.log(year, x))
	max_b = int(math.log(year, y))
	for a in range(max_a + 1):
		for b in range(max_b + 1):
			if x ** a + y ** b == year:
				return True
	return False

def main():
	(x, y, l, r) = list(map(int, input().split()))
	unlucky = []
	p = 1
	unlucky.append(l - 1)
	while True:
		if p > r:
			break
		q = 1
		while True:
			if p + q > r:
				break
			if p + q >= l:
				unlucky.append(p + q)
			q *= y
		p *= x
	unlucky.append(r + 1)
	unlucky = sorted(unlucky)
	max_interval = 0
	for i in range(len(unlucky) - 1):
		max_interval = max(max_interval, unlucky[i + 1] - unlucky[i] - 1)
	print(max_interval)
main()


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:
	if k >= l:
		ans = max(ans, k - last - 1)
		last = k
		if last >= r:
			break
print(ans)


(x, y, l, r) = map(int, input().split())
ul = [l - 1, r + 1]
i = 1
while i < r:
	j = 1
	while i + j < l:
		j *= y
	while i + j <= r:
		ul.append(i + j)
		j *= y
	i *= x
ul.sort()
maxd = max((ul[i] - ul[i - 1] for i in range(1, len(ul))))
print(maxd - 1)


(x, y, l, r) = 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)


import math
(x, y, l, r) = list(map(int, input().strip().split(' ')))
maxa = int(math.log(max(r - 1, 1), x))
maxb = int(math.log(max(r - 1, 1), y))
temp = [l - 1]
for i in range(maxa + 1):
	for j in range(maxb + 1):
		haha = x ** i + y ** j
		if haha >= l and haha <= r:
			temp += [x ** i + y ** j]
temp = sorted(temp)
temp += [r + 1]
MAX = 0
for i in range(0, len(temp) - 1):
	length = temp[i + 1] - temp[i] - 1
	if length > MAX:
		MAX = length
print(MAX)


(x, y, l, r) = map(int, input().split())
a = [0]
for i in range(70):
	for j in range(70):
		a.append(x ** i + y ** j)
a.sort()
ans = 0
for i in range(len(a) - 1):
	if a[i + 1] <= r:
		r1 = a[i + 1] - 1
	else:
		r1 = r
	if a[i] >= l:
		l1 = a[i] + 1
	else:
		l1 = l
	if ans <= r1 - l1:
		ans = r1 - l1 + 1
print(ans)


(x, y, l, r) = map(int, input().split())
xx = 1
a = [0, 1e+20]
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(ans)


(x, y, l, r) = map(int, input().split())
ll = []
for i in range(0, 75):
	for j in range(0, 75):
		if l <= pow(x, i) + pow(y, j) <= r:
			ll.append(pow(x, i) + pow(y, j))
ll = sorted(set(ll))
maxx = -1
if len(ll) == 0:
	print(r - l + 1)
else:
	maxx = max(maxx, ll[0] - l)
	for i in range(1, len(ll)):
		maxx = max(ll[i] - ll[i - 1] - 1, maxx)
	maxx = max(maxx, r - ll[-1])
	print(maxx)


import sys
from itertools import product
(x, y, l, r) = map(int, input().split())
(xp, yp) = ([], [])
for i in range(100):
	xp.append(x ** i)
	if x ** i >= r:
		break
for i in range(100):
	yp.append(y ** i)
	if y ** i >= r:
		break
unlucky = set()
for (xa, yb) in product(xp, yp):
	unlucky.add(xa + yb)
unlucky = sorted(unlucky)
ans = 1 if l == 1 else 0
for (sl, sr) in zip(unlucky, unlucky[1:]):
	ans = max(ans, min(r, sr - 1) - max(l, sl + 1) + 1)
print(ans)


import math
import sys
a = list(map(int, input().split()))
x = a[0]
y = a[1]
l = a[2]
r = a[3]
v = []
for i in range(0, 61):
	for j in range(0, 61):
		if x ** i + y ** j >= l and x ** i + y ** j <= r:
			v.append(x ** i + y ** j)
maxi = 0
v.sort()
if len(v) >= 2:
	for i in range(1, len(v)):
		maxi = max(maxi, v[i] - v[i - 1] - 1)
if len(v) >= 1 and (not v.count(l)):
	maxi = max(maxi, v[0] - l)
if len(v) >= 1 and (not v.count(r)):
	maxi = max(maxi, r - v[len(v) - 1])
if len(v) == 0 and (not v.count(l)) and (not v.count(r)):
	maxi = max(maxi, r - l + 1)
print(maxi)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=2 ** 33, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(x, y, l, r) = map(int, input().split())
li = []
li1 = []
t = 1
while t <= r:
	li.append(t)
	t *= x
t = 1
while t <= r:
	li1.append(t)
	t *= y
tot = [l - 1]
for i in li:
	for j in li1:
		if l <= i + j <= r:
			tot.append(i + j)
tot.append(r + 1)
tot = list(set(tot))
tot.sort()
ans = 0
for i in range(1, len(tot)):
	ans = max(ans, tot[i] - tot[i - 1] - 1)
print(ans)


def inRange(a, b, l, r):
	return a >= l and b <= r
(x, y, l, r) = [int(x) for x in input().split()]
(xp, yp) = ([1], [1])
(cx, cy) = (x, y)
while True:
	xp.append(cx)
	if cx > r:
		break
	cx = cx * x
while True:
	yp.append(cy)
	if cy > r:
		break
	cy = cy * y
st = []
for i in range(len(xp)):
	for j in range(len(yp)):
		st.append(xp[i] + yp[j])
tp = [l - 1, r + 1]
for i in st:
	if i >= l and i <= r:
		tp.append(i)
tp.sort()
ans = 0
for i in range(len(tp) - 1):
	ans = max(ans, tp[i + 1] - tp[i] - 1)
print(ans)


max_n = int(1e+18)
(x, y, l, r) = map(int, str(input()).split(' '))
Xa = []
Yb = []
t = 1
while t <= r:
	Xa.append(t)
	t *= x
t = 1
while t <= r:
	Yb.append(t)
	t *= y
unlucky = []
for i1 in Xa:
	for i2 in Yb:
		if i1 + i2 >= l and i1 + i2 <= r:
			unlucky.append(i1 + i2)
unlucky.append(r + 1)
unlucky = sorted(list(set(unlucky)))
left = l - 1
ans = 0
for d in unlucky:
	ans = max(ans, d - left - 1)
	left = d
print(ans)


from math import log, pow
import enum
(x, y, l, r) = [int(x) for x in input().split()]
s3 = [0]
for b in range(60):
	for c in range(60):
		s3.append(x ** b + y ** c)
s3.sort()
ans = 0
for i in range(len(s3) - 1):
	ans = max(ans, min(r, s3[i + 1] - 1) - max(l, s3[i] + 1) + 1)
print(ans)


(x, y, l, r) = [int(i) for i in input().split()]
i = j = a = b = 0
numx = []
numy = []
while a <= r:
	a = x ** i
	i += 1
	numx.append(a)
while b <= r:
	b = y ** j
	j += 1
	numy.append(b)
nums = set()
for i1 in numx:
	for j1 in numy:
		s = i1 + j1
		if s > r:
			break
		nums.add(s)
nums = list(nums)
init = l
ans = 0
nums = sorted(nums)
le = len(nums)
if le == 0:
	ans = r - l + 1
for it in range(len(nums)):
	if it == 0:
		if nums[it] >= l and nums[it] <= r:
			ans = min(nums[it], r) - l
		elif nums[it] > r:
			ans = r - l + 1
			break
	elif nums[it] >= l and nums[it] <= r:
		if nums[it - 1] < l:
			ans = max(ans, min(nums[it], r) - l)
		else:
			ans = max(ans, min(nums[it], r) - nums[it - 1] - 1)
try:
	if nums[le - 1] < l:
		ans = r - l + 1
	elif nums[le - 1] >= l:
		ans = max(ans, r - nums[le - 1])
except:
	pass
print(ans)


(x, y, l, r) = map(int, input().split())
current = 1
(a, b, v) = ([], [], [])
while current <= 1e+18:
	a.append(current)
	current *= x
current = 1
while current <= 1e+18:
	b.append(current)
	current *= y
for A in a:
	for B in b:
		if A + B >= l and A + B <= r:
			v.append(A + B)
v.sort()
record = 0
for i in range(1, len(v)):
	record = max(record, v[i] - v[i - 1] - 1)
if v:
	record = max(record, v[0] - l)
	record = max(record, r - v[-1])
	print(record)
else:
	print(r - l + 1)


import math

def f():
	global arr
	(x, y, l, r) = list(map(int, input().split()))
	arr = []
	for a in range(int(math.log(r, x)) + 1):
		for b in range(int(math.log(r, y)) + 1):
			if l <= x ** a + y ** b <= r:
				arr.append(x ** a + y ** b)
	arr.sort()
	if arr == []:
		print(r - l + 1)
		return
	m = max(arr[0] - l, r - arr[-1])
	for i in range(1, len(arr)):
		m = max(m, arr[i] - arr[i - 1] - 1)
	print(m)
f()


(x, y, l, r) = map(int, input().split())
one = []
two = []
eh = 1
oh = 1
ok = []
f1 = True
f2 = True
while eh <= r:
	one.append(eh)
	eh = eh * x
while oh <= r:
	two.append(oh)
	oh = oh * y
for itr in one:
	for it in two:
		okk = itr + it
		if okk == l:
			f1 = False
		if okk == r:
			f2 = False
		if okk > l and okk < r:
			ok.append(okk)
ans = 0
ok.sort()
for i in range(1, len(ok)):
	ans = max(ans, ok[i] - ok[i - 1] - 1)
if len(ok) != 0:
	if f1 == True:
		ans = max(ans, ok[0] - l)
	else:
		ans = max(ans, ok[0] - l - 1)
	if f2 == True:
		ans = max(ans, r - ok[len(ok) - 1])
	else:
		ans = max(ans, r - ok[len(ok) - 1] - 1)
elif f1 and f2:
	ans = r - l + 1
elif f1 or f2:
	ans = r - l
print(ans)


(x, y, l, r) = map(int, input().split())
yrs = set()
yrs.add(l - 1)
yrs.add(r + 1)
for a in range(0, 70):
	for b in range(0, 70):
		n = x ** a + y ** b
		if n >= l and n <= r:
			yrs.add(n)
yrs = sorted(yrs)
mx = 0
for i in range(1, len(yrs)):
	mx = max(mx, yrs[i] - yrs[i - 1] - 1)
print(mx)


def main():
	(x, y, l, r) = readIntArr()
	xs = []
	ys = []
	(x2, y2) = (1, 1)
	while True:
		xs.append(x2)
		if x2 > 10 ** 18:
			break
		x2 *= x
	while True:
		ys.append(y2)
		if y2 > 10 ** 18:
			break
		y2 *= y
	nums = set()
	for x2 in xs:
		for y2 in ys:
			nums.add(x2 + y2)
	nums2 = list(sorted(nums))
	nums2 = [0] + nums2
	ans = 0
	n = len(nums2)
	for i in range(n - 1):
		lower = nums2[i] + 1
		lower = max(lower, l)
		upper = nums2[i + 1] - 1
		upper = min(upper, r)
		ans = max(ans, upper - lower + 1)
	print(ans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]
inf = float('inf')
MOD = 10 ** 9 + 7
main()


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

def get_word():
	global 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)


(x, y, l, r) = map(int, input().split())
sum = 1
xlist = [1]
ylist = [1]
while sum <= 1e+18 / x:
	sum = sum * x
	xlist.append(sum)
sum = 1
s = [l - 1]
while sum <= 1e+18 / y:
	sum = sum * y
	ylist.append(sum)
for i in range(len(xlist)):
	for j in range(len(ylist)):
		x = xlist[i]
		y = ylist[j]
		if x + y >= l and x + y <= r:
			s += [x + y]
s += [r + 1]
s.sort()
ans = 0
for i in range(1, len(s)):
	ans = max(ans, s[i] - s[i - 1] - 1)
print(ans)


(x, y, l, r) = map(int, input().split())
p = set()
p.add(l - 1)
p.add(r + 1)
for i in range(0, 80):
	s = x ** i
	if s > r:
		break
	for j in range(0, 80):
		t = y ** j
		if s + t > r:
			break
		elif s + t >= l:
			p.add(s + t)
p = sorted(list(p))
ans = 0
for i in range(1, len(p)):
	ans = max(ans, p[i] - p[i - 1] - 1)
print(ans)


from collections import Counter
LGA = 0
[x, y, l, r] = [int(z) for z in input().split()]
UNLY = Counter([])
for a in range(60):
	for b in range(60):
		num = x ** a + y ** b
		if num >= l and num <= r:
			UNLY[num] = 1
if UNLY == Counter([]):
	print(r - l + 1)
	exit(0)
UNLY = sorted(UNLY.keys())
for i in range(0, len(UNLY) + 1):
	if i == 0 or i == len(UNLY):
		if i == 0:
			l = UNLY[i] - l
		if i == len(UNLY):
			l = max(r - UNLY[i - 1], l)
	else:
		l = UNLY[i] - UNLY[i - 1] - 1
	if l > LGA:
		LGA = l
print(LGA)


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]

def fast_power2(x, y):
	if y == 0:
		return 1
	temp = fast_power2(x, int(y / 2))
	if y % 2 == 0:
		return temp * temp
	else:
		return x * temp * temp
(x, y, l, r) = MAP()
nums = []
(l2, r2) = (0, 0)
for i in range(63):
	for j in range(63):
		v = fast_power2(x, i) + fast_power2(y, j)
		if v > r:
			break
		if v >= l and v <= r:
			nums.append(v)
		if v == l:
			l2 = 1
		if v == r:
			r2 = 1
if l2 == 0:
	nums.append(l - 1)
if r2 == 0:
	nums.append(r + 1)
nums.sort()
mx = 0
for i in range(len(nums) - 1):
	mx = max(mx, nums[i + 1] - nums[i] - 1)
print(mx)


(x, y, l, r) = input().split()
x = int(x)
y = int(y)
l = int(l)
r = int(r)
xa = 1
li = []
for i in range(0, 64):
	yb = 1
	for j in range(0, 64):
		if xa + yb > r:
			break
		if not xa + yb in li:
			li.append(xa + yb)
		yb = yb * y
	xa = xa * x
li.sort()
now = l - 1
ans = 0
for i in li:
	if i >= now and i <= r:
		ans = max(ans, i - now - 1)
		now = i
ans = max(ans, r - now)
print(ans)


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
(x, y, l, r) = map(int, input().split())
(px, py) = ([1], [1])
while px[-1] <= r:
	px.append(px[-1] * x)
while py[-1] <= r:
	py.append(py[-1] * y)
u = [0]
for pxi in px:
	for pyi in py:
		u.append(pxi + pyi)
u.sort()
ans = 0
for i in range(1, len(u)):
	if l > u[i - 1] and r < u[i]:
		ans = max(ans, r - l + 1)
	elif l <= u[i - 1] and r < u[i]:
		ans = max(ans, r - u[i - 1])
	elif l > u[i - 1] and r >= u[i]:
		ans = max(ans, u[i] - 1 - l + 1)
	elif l <= u[i - 1] and r >= u[i]:
		ans = max(ans, u[i] - 1 - u[i - 1])
print(ans)


from itertools import product

def main():
	(x, y, l, r) = list(map(int, input().split(' ')))
	nums1 = []
	nums2 = []
	a = 1
	while a <= r:
		nums1.append(a)
		a *= x
	b = 1
	while b <= r:
		nums2.append(b)
		b *= y
	nums = (num1 + num2 for (num1, num2) in product(nums1, nums2) if l <= num1 + num2 <= r)
	nums = [l - 1] + list(set(nums)) + [r + 1]
	nums.sort()
	gap = 0
	for i in range(len(nums) - 1):
		gap = max(gap, nums[i + 1] - nums[i] - 1)
	print(gap)
main()


(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)


Inf = int(1000000000.0)
[x, y, l, r] = list(map(int, input().split()))
s = [1, x]
v = x
while v * x <= r:
	if v * x // x != v:
		break
	v *= x
	s.append(v)
t = [1, y]
v = y
while v * y <= r:
	if v * y // y != v:
		break
	v *= y
	t.append(v)
q = list()
for v in s:
	for u in t:
		w = v + u
		if l <= w and w <= r:
			q.append(w)
if len(q) == 0:
	print(r - l + 1)
	exit(0)
q.sort()
res = q[0] - l
for i in range(1, len(q)):
	res = max(res, q[i] - q[i - 1] - 1)
res = max(res, r - q[len(q) - 1])
print(res)


(x, y, l, r) = map(int, input().split())
bad_years = [l - 1, r + 1]
(i, j) = (0, 0)
year = 0
while x ** i <= r:
	j = 0
	while x ** i + y ** j <= r:
		year = x ** i + y ** j
		if year >= l:
			if year <= r:
				bad_years.append(year)
		j += 1
	i += 1
bad_years.sort()
max_gold = 0
for k in range(len(bad_years) - 1):
	max_gold = max(bad_years[k + 1] - bad_years[k], max_gold)
print(max(max_gold - 1, 0))


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)


arri = lambda : [int(s) for s in input().split()]
(x, y, l, r) = arri()
t = [l - 1, r + 1]
xx = 1
while xx < r:
	yy = 1
	while yy < r:
		if xx + yy >= l and xx + yy <= r and (xx + yy not in t):
			t.append(xx + yy)
		yy *= y
	xx *= x
t.sort()
mx = 0
for i in range(1, len(t)):
	mx = max(mx, t[i] - t[i - 1])
print(mx - 1)


(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) = map(int, input().split())
lm = 0
rm = 0
ll = []
for i in range(64):
	for j in range(64):
		v = x ** i + y ** j
		if v >= l and v <= r:
			ll.append(v)
		if v == l:
			lm = 1
		if v == r:
			rm = 1
ll.sort()
s = len(ll)
ans = 0
for i in range(s - 1):
	ans = max(ans, ll[i + 1] - ll[i] - 1)
if lm == 0:
	if len(ll) > 0:
		ans = max(ans, ll[0] - l)
if rm == 0:
	if len(ll) > 0:
		ans = max(ans, r - ll[s - 1])
if len(ll) == 0:
	ans = r - l + 1
print(ans)


n = str(input())
li = n.split(' ')
x = int(li[0])
y = int(li[1])
l = int(li[2])
r = int(li[3])
cnt = 0
xs = []
ys = []
while True:
	tmp = x ** cnt
	if tmp < 1e+18:
		xs.append(tmp)
		cnt += 1
	else:
		break
cnt = 0
while True:
	tmp = y ** cnt
	if tmp < 1e+18:
		ys.append(tmp)
		cnt += 1
	else:
		break
years = set()
for i in xs:
	for j in ys:
		years.add(i + j)
accept = [l - 1, r + 1]
for i in years:
	if l <= i <= r:
		accept.append(i)
accept.append(r + 1)
mx = -1000000
accept.sort()
for i in range(1, len(accept)):
	if accept[i] - accept[i - 1] - 1 > mx:
		mx = accept[i] - accept[i - 1] - 1
print(mx)


(x, y, l, r) = [int(i) for i in input().split()]
M = int(1e+18)
i = 1
ul = []
while i <= M:
	j = 1
	while i + j <= M:
		ul.append(i + j)
		j *= y
	i *= x
ul = [0] + sorted(ul) + [M + 1]
n = len(ul)
mx = 0
for i in range(1, n):
	cur = min(ul[i], r + 1) - max(l - 1, ul[i - 1]) - 1
	mx = max(mx, cur)
print(mx)


import sys

def solve():
	(x, y, l, r) = map(int, input().split())
	if x < y:
		(x, y) = (y, x)
	made = {l - 1, r + 1}
	for a in range(64):
		for b in range(64):
			t = x ** a + y ** b
			if l <= t <= r:
				made.add(t)
			elif t > r:
				break
	made = sorted(made)
	ans = 0
	for i in range(len(made) - 1):
		ans = max(ans, made[i + 1] - made[i] - 1)
	print(ans)
solve()


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from time import time
from itertools import permutations as per
from heapq import heapify, heappush, heappop, heappushpop
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\r\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
(x, y, l, r) = R()
a = {l - 1, r + 1}
p = []
q = []
v = 1
while v <= r:
	p += (v,)
	if x == 1:
		break
	v *= x
v = 1
while v <= r:
	q += (v,)
	if y == 1:
		break
	v *= y
for i in p:
	for j in q:
		if i + j > r:
			break
		if i + j >= l:
			a.add(i + j)
ans = 0
a = sorted(a)
for i in range(1, len(a)):
	ans = max(ans, a[i] - a[i - 1] - 1)
print(ans)


[x, y, l, r] = [int(x) for x in input().split()]
bd = []
cx = 1
while cx <= 10 ** 18:
	cy = 1
	while cy <= 10 ** 18:
		if cx + cy >= l and cx + cy <= r:
			bd += [cx + cy]
		cy *= y
	cx *= x
bd += [l - 1, r + 1]
bd.sort()
res = 0
for i in range(1, len(bd)):
	if res < bd[i] - bd[i - 1] - 1:
		res = bd[i] - bd[i - 1] - 1
print(res)


(x, y, l, r) = map(int, input().split())
X = 1
st = set()
while X <= r:
	Y = 1
	while X + Y <= r:
		if X + Y >= l:
			st.add(X + Y)
		Y *= y
	X *= x
bad = sorted([x for x in st])
prev = l
ans = 0
for x in bad:
	ans = max(ans, x - prev)
	prev = x + 1
ans = max(ans, r - prev + 1)
print(ans)


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


import math
(x, y, l, r) = map(int, input().split())
n = int(math.log(r, min(x, y))) + 2
d = [0] * (n * n)
for i in range(n):
	for j in range(n):
		d[i * n + j] = x ** i + y ** j
d.sort()
i = 0
while d[i] < l:
	i += 1
answer = min(r + 1, d[i]) - l
while d[i + 1] <= r:
	answer = max(answer, d[i + 1] - d[i] - 1)
	i += 1
answer = max(answer, r - max(d[i], l - 1))
print(answer)


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


(x, y, l, r) = map(int, input().split())
se = set()
nowX = 1
while nowX <= r:
	nowY = 1
	while nowX + nowY <= r:
		se.add(nowX + nowY)
		nowY *= y
	nowX *= x
a = []
for el in se:
	a.append(el)
a.sort()
ans = 0
while len(a) > 0 and a[0] < l:
	del a[0]
while len(a) > 0 and a[-1] > r:
	del a[-1]
if len(a) > 0:
	if a[0] >= l:
		ans = max(ans, a[0] - l)
	for i in range(0, len(a) - 1):
		if a[i] >= l and a[i + 1] <= r:
			ans = max(ans, a[i + 1] - a[i] - 1)
	if a[-1] <= r:
		ans = max(ans, r - a[-1])
else:
	ans = r - l + 1
print(ans)


(x, y, l, r) = map(int, input().split())
n1 = 1
n2 = 1
number = []
number.append(l - 1)
while n1 <= r:
	n2 = 1
	while n2 <= r:
		if n1 + n2 <= r and n1 + n2 >= l:
			number.append(n1 + n2)
		n2 *= y
	n1 *= x
number.append(r + 1)
number.sort()
ans = 0
for i in range(1, len(number)):
	ans = max(ans, number[i] - number[i - 1] - 1)
print(ans)


(x, y, l, r) = map(int, input().split())
pre = []
pre.append(0)
lim = 10 ** 20
for i in range(0, 65):
	for j in range(0, 65):
		sm = pow(x, i) + pow(y, j)
		pre.append(sm)
		if sm > lim:
			break
pre.sort()
ans = 0
for i in range(len(pre)):
	if pre[i] > r and pre[i - 1] < l:
		ans = r - l + 1
		break
	if pre[i] >= l and pre[i] <= r:
		if pre[i - 1] >= l:
			ans = max(ans, pre[i] - pre[i - 1] - 1)
		else:
			ans = max(ans, pre[i] - l)
	if pre[i - 1] >= l and pre[i] > r:
		ans = max(ans, r - pre[i - 1])
print(ans)


(x, y, l, r) = map(int, input().split())
xs = [1]
ys = [1]
while xs[-1] * x <= r:
	xs.append(xs[-1] * x)
while ys[-1] * y <= r:
	ys.append(ys[-1] * y)
zs = [x + y for x in xs for y in ys]
zs.extend([l - 1, r + 1])
zs.sort()
zs = list(filter(lambda z: z <= r + 1 and z >= l - 1, zs))
print(max([zs[i + 1] - zs[i] - 1 for i in range(len(zs) - 1)]))


(x, y, l, r) = map(int, input().split(' '))
arr_x = [1]
while True:
	if arr_x[-1] * x <= r:
		arr_x.append(arr_x[-1] * x)
	else:
		break
arr_y = [1]
while True:
	if arr_y[-1] * y <= r:
		arr_y.append(arr_y[-1] * y)
	else:
		break
L = [i + j for i in arr_x for j in arr_y if i + j >= l and i + j <= r]
L = sorted(list(set(L)))
ans = 0
curr = l - 1
for i in L:
	ans = max(ans, i - curr - 1)
	curr = i
ans = max(ans, r + 1 - curr - 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())

def gen_list(var):
	cur = 1
	while cur <= r:
		yield cur
		cur *= var
x_list = list(gen_list(x))
y_list = list(gen_list(y))
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))
if len(numbers) < 2:
	print('0')
	exit()
max_happy = max([numbers[i + 1] - numbers[i] - 1 for i in range(len(numbers) - 1)], default=0)
print(max_happy)


def __init__(x, y, l, r):
	n = lambda a, b: x ** a + y ** b
	bad = set()
	for a in range(60):
		for b in range(60):
			year = n(a, b)
			if l <= year <= r:
				bad.add(year)
			elif year > r + 1:
				break
	record = 0
	bad = list(bad)
	bad.append(r + 1)
	bad.append(l - 1)
	bad.sort()
	if len(bad) > 2:
		for i in range(len(bad) - 1):
			score = bad[i + 1] - bad[i]
			if score > record:
				record = score - 1
		return record
	else:
		return r - l + 1
__test__ = 0
if __test__:
	tests = [[1, [2, 3, 1, 10]], [8, [3, 5, 10, 22]], [0, [2, 3, 3, 5]], [1, [2, 2, 1, 10]], [213568, [2, 2, 1, 1000000]], [144115188075855871, [2, 2, 1, 1000000000000000000]], [206415, [2, 3, 1, 1000000]], [261485717957290893, [2, 3, 1, 1000000000000000000]], [933334, [12345, 54321, 1, 1000000]], [976614248345331214, [54321, 12345, 1, 1000000000000000000]], [188286357653, [2, 3, 100000000, 1000000000000]], [0, [2, 14, 732028847861235712, 732028847861235712]], [1, [14, 2, 732028847861235713, 732028847861235713]], [1, [3, 2, 6, 7]]]
	for i in range(len(tests)):
		print(i + 1, end='\t')
		print(tests[i][0] == __init__(*tests[i][1]), end='\t')
		print(tests[i][0], '<<', __init__(*tests[i][1]))
	exit()
print(__init__(*map(int, input().split())))


(x, y, l, r) = map(int, input().split())
a = []
for k in range(70):
	for b in range(70):
		a.append(x ** k + y ** b)
a = sorted(a)
ans = 0

def inter(a, b, c, d):
	if c > d:
		return 0
	if max(a, c) > min(b, d):
		return 0
	return min(b, d) - max(a, c) + 1
ans = inter(l, r, 1, a[0] - 1)
for i in range(len(a) - 1):
	ll = a[i] + 1
	rr = a[i + 1] - 1
	ans = max(ans, inter(l, r, ll, rr))
print(ans)


import math
tmp = input().split()
dict = {}
arr = []
x = int(tmp[0])
y = int(tmp[1])
l = int(tmp[2])
r = int(tmp[3])
x1 = int(math.log(10, x) * 18) + 2
x2 = int(math.log(10, y) * 18) + 2
for i in range(x1):
	for j in range(x2):
		pr = x ** i + y ** j
		if pr <= 1e+18:
			try:
				tt = dict[pr]
			except:
				dict[pr] = 1
				arr.append(pr)
		else:
			break
arr.sort()
c = 0
for i in range(len(arr)):
	if arr[i] >= l and arr[i] <= r:
		c += 1
		pos2 = i
	if arr[i] >= r:
		break
if c == 0:
	print(int(r - l + 1))
else:
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] >= l:
			pos1 = i
		if arr[i] <= l:
			break
	maxi = 0
	for i in range(pos1, pos2, 1):
		if maxi < arr[i + 1] - arr[i] - 1:
			maxi = arr[i + 1] - arr[i] - 1
	maxi = max(maxi, max(r - arr[pos2], arr[pos1] - l))
	print(maxi)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	(x, y, l, r) = map(int, input().split())
	(tx, ty) = (x, y)
	(a, b) = (0, 0)
	while tx < r:
		tx = tx * x
		a += 1
	while ty < r:
		ty = ty * y
		b += 1
	s = set()
	for i in range(a + 1):
		for j in range(b + 1):
			v = x ** i + y ** j
			if v >= l and v <= r:
				s.add(v)
	res = sorted(list(s))
	if len(res) == 0:
		print(r - l + 1)
		return
	ans = max(res[0] - l, r - res[-1])
	for i in range(1, len(res)):
		ans = max(ans, res[i] - res[i - 1] - 1)
	print(ans)
main()


(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())
u = set([l - 1, r + 1])
for a in range(70):
	for b in range(70):
		c = x ** a + y ** b
		if c >= l and c <= r:
			u.add(c)
u = sorted(u)
m = 0
for i in range(len(u) - 1):
	m = max(m, u[i + 1] - u[i] - 1)
print(m)


def main():
	(x, y, l, r) = map(int, input().split())
	unlucky = set()
	unlucky.add(l - 1)
	unlucky.add(r + 1)
	for i in range(70):
		X = x ** i
		if X > r:
			break
		for j in range(70):
			Y = y ** j
			u = X + Y
			if l <= u <= r:
				unlucky.add(u)
	lu = sorted(list(unlucky))
	ans = 0
	for i in range(len(lu) - 1):
		ans = max(ans, lu[i + 1] - lu[i] - 1)
	return ans
print(main())


def f(n, m):
	res = []
	for i in range(60):
		for h in range(60):
			res.append(x ** i + y ** h)
	res.append(n - 1)
	res.append(m + 1)
	return list(set(res))
(x, y, l, r) = map(int, input().split(' '))
unh = f(l, r)
unh.sort()
res = 0
for i in range(len(unh) - 1):
	if l - 1 <= unh[i] <= r + 1 and l - 1 <= unh[i + 1] <= r + 1:
		res = max(res, unh[i + 1] - unh[i])
print(res - 1)


(x, y, l, r) = map(int, input().split())
a = [0]
p = 1
for i in range(70):
	q = 1
	for j in range(70):
		a.append(p + q)
		q *= y
	p *= x
a.sort()
ans = 0
for i in range(len(a) - 1):
	ans = max(ans, min(r, a[i + 1] - 1) - max(l, a[i] + 1) + 1)
print(ans)


(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) = 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)


from sys import stdin, stdout
from random import randrange
(x, y, l, r) = map(int, stdin.readline().split())
sze = 60
challengers = []
for i in range(sze):
	for j in range(sze):
		if l <= x ** i + y ** j <= r:
			challengers.append(x ** i + y ** j)
		elif x ** i + y ** j > r:
			break
challengers = sorted(list(set(challengers)))
ans = 0
if challengers:
	if l not in challengers:
		ans = max(ans, challengers[0] - l)
	if r not in challengers:
		ans = max(ans, r - challengers[-1])
	n = len(challengers)
	for i in range(1, n):
		ans = max(ans, challengers[i] - challengers[i - 1] - 1)
else:
	ans = r - l + 1
stdout.write(str(ans))


(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) = map(int, input().split())
s = list()
a = 0
while pow(x, a) <= 1e+18:
	b = 0
	while pow(y, b) <= 1e+18:
		num = pow(x, a) + pow(y, b)
		if num >= l and num <= r:
			s.append(num)
		if num > r:
			break
		b += 1
	a += 1
s.sort()
res = 0
prev = l
for num in s:
	res = max(res, num - prev)
	prev = num + 1
res = max(res, r + 1 - prev)
print(res)


(x, y, l, r) = map(int, input().split())
vals = []
p = 1
while p < r:
	z = 1
	t = p
	while t + z <= r:
		if t + z >= l:
			vals.append(t + z)
		z = z * y
	p = p * x
if len(vals) == 0:
	print(r - l + 1)
	exit(0)
vals.sort()
ans = max(vals[0] - l, r - vals[-1])
ans = max(ans, 0)
for i in range(1, len(vals)):
	ans = max(ans, vals[i] - vals[i - 1] - 1)
print(ans)


(x, y, l, r) = 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))


(x, y, lo, h) = list(map(int, input().strip().split(' ')))
p = 1
q = 1
l = []
l.append(lo - 1)
l.append(h + 1)
while p < h:
	q = 1
	while q < h:
		if lo <= p + q and p + q <= h:
			l.append(p + q)
		q *= y
	p *= x
max = 0
l.sort()
for i in range(1, len(l)):
	if max <= l[i] - l[i - 1] - 1:
		max = l[i] - l[i - 1] - 1
print(max)


(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)


inp = input().split()
x = int(inp[0])
y = int(inp[1])
l = int(inp[2])
r = int(inp[3])
numbers = []
for a in range(0, 65):
	for b in range(0, 65):
		c = x ** a + y ** b
		if l <= c <= r:
			numbers.append(c)
numbers.append(l - 1)
numbers.append(r + 1)
numbers.sort()
l = len(numbers)
maxi = 0
for i in range(1, l):
	maxi = max(maxi, numbers[i] - numbers[i - 1] - 1)
print(maxi)


(x, y, l, r) = 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)


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left, bisect
import math
from operator import itemgetter
from heapq import heapify, heappop, heappush
(x, y, l, r) = map(int, input().split())
p = [i for i in range(61)]
q = [i for i in range(61)]
t = set()
c = 0
if r == 1:
	print(1)
else:
	a = math.ceil(math.log(r - 1, x))
	b = math.ceil(math.log(r - 1, y))
	for i in range(a + 1):
		for j in range(b + 1):
			s = x ** p[i] + y ** q[j]
			if s >= l and s <= r:
				t.add(s)
	t = list(sorted(t))
	c = len(t)
	if c > 0:
		m = t[0] - l
		for i in range(c - 1):
			m = max(m, t[i + 1] - t[i] - 1)
		m = max(m, r - t[-1])
	else:
		m = r - l + 1
	print(m)


(x, y, l, r) = map(int, input().split())
i = 0
a = list()
a.append(l - 1)
a.append(r + 1)
for i in range(100):
	for j in range(100):
		a.append(x ** i + y ** j)
a.sort()
m = 0
for i in range(len(a) - 1):
	if a[i] <= r:
		m = max(m, min(r, a[i + 1] - 1) - max(a[i] + 1, l) + 1)
print(m)


def sol(x, y, a):
	se = set()
	for i in range(0, 100):
		if x ** i > a:
			return se
		tmp = x ** i
		for j in range(0, 100):
			if y ** j + tmp > a:
				break
			else:
				se.add(y ** j + tmp)
(x, y, l, r) = map(int, input().split())
se = sol(x, y, r)
e = f = -1
ans = 0
se = sorted(se, reverse=True)
while se:
	e = se.pop()
	if e >= l:
		if f > -1:
			ans = max(ans, e - f - 1)
		else:
			ans = max(ans, e - l)
		f = e
if f > 0:
	ans = max(ans, r - e)
else:
	ans = r - l + 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)


inp = input()
x = int(inp.split()[0])
y = int(inp.split()[1])
l = int(inp.split()[2])
r = int(inp.split()[3])
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) = input().split()
x = int(x)
y = int(y)
l = int(l)
r = int(r)
xp = 0
while True:
	if not r // x ** xp:
		break
	else:
		xp += 1
xp -= 1
temp = r
yp = 0
while True:
	if not temp // y ** yp:
		break
	else:
		yp += 1
yp -= 1
s = []
for i in range(xp + 1):
	for j in range(yp + 1):
		if x ** i + y ** j >= l and x ** i + y ** j <= r:
			s.append(x ** i + y ** j)
s.sort()
m = 0
for i in range(len(s) - 1):
	if s[i + 1] - s[i] > m:
		m = s[i + 1] - s[i]
m -= 1
try:
	print(max(m, s[0] - l, r - s[len(s) - 1]))
except:
	print(r - l + 1)


[x, y, l, r] = 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)


s = [int(x) for x in input().split()]
x = s[0]
y = s[1]
l = s[2]
r = s[3]
v = [l - 1, r + 1]
xx = 1
yy = 1
for i in range(64):
	yy = 1
	for j in range(64):
		if xx + yy <= r and xx + yy >= l:
			v.append(xx + yy)
		yy = y * yy
	xx = xx * x
v = sorted(v)
ans = 0
for i in range(1, len(v)):
	ans = max(ans, v[i] - v[i - 1] - 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)


(x, y, l, r) = map(int, input().split())
X = []
Y = []
X.append(1)
Y.append(1)
while X[-1] <= 1000000000000000000:
	X.append(X[-1] * x)
while Y[-1] <= 1000000000000000000:
	Y.append(Y[-1] * y)
Z = []
for i in range(len(X)):
	for j in range(len(Y)):
		if X[i] + Y[j] <= r and X[i] + Y[j] >= l:
			Z.append(X[i] + Y[j])
Z.sort()
c = 0
maxi = 0
if len(Z) == 0:
	print(r - l + 1)
	exit()
for i in range(1, len(Z)):
	maxi = max(maxi, Z[i] - Z[i - 1] - 1)
if l not in Z and len(Z) > 0:
	maxi = max(maxi, Z[0] - l)
if r not in Z and len(Z) > 0:
	maxi = max(maxi, r - Z[-1])
print(maxi)


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
(x, y, l, r) = MAP()
a = []
ll = 0
rr = 0
for i in range(62):
	for j in range(62):
		n = x ** i + y ** j
		if n >= l and n <= r:
			a.append(n)
			if n == l:
				ll = 1
			if n == r:
				rr = 1
if ll == 0:
	a.append(l - 1)
if rr == 0:
	a.append(r + 1)
a.sort()
mx = 0
for i in range(len(a) - 1):
	div = a[i + 1] - a[i] - 1
	mx = max(mx, div)
print(mx)


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


(x, y, l, r) = map(int, input().split())
rec = []
for i in range(61):
	for j in range(61):
		if l <= x ** i + y ** j <= r:
			rec.append(x ** i + y ** j)
rec.append(r + 1)
rec = sorted(set(rec))
pre = l - 1
num = 0
for i in range(len(rec)):
	num = max(num, rec[i] - pre - 1)
	pre = rec[i]
print(num)


(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)


import math
(x, y, l, r) = map(int, input().split())
yrs = []
lim = math.ceil(math.log(r, min(x, y)))
for i in range(int(lim + 1)):
	for j in range(int(lim + 1)):
		num = x ** i + y ** j
		if l <= num and num <= r:
			yrs.append(num)
yrs.append(l - 1)
yrs.append(r + 1)
yrs.sort()
ans = 0
for i in range(1, len(yrs)):
	ans = max(ans, yrs[i] - yrs[i - 1] - 1)
print(ans)


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

def pows(x, r):
	ps = [1]
	while ps[-1] * x < r:
		ps.append(ps[-1] * x)
	return ps
px = pows(x, r)
py = pows(y, r)
nothappy = [l - 1, r + 1]
for ppx in px:
	for ppy in py:
		if l <= ppx + ppy <= r:
			nothappy.append(ppx + ppy)
nothappy.sort()
best = max((nh1 - nh0 for (nh0, nh1) in zip(nothappy, nothappy[1:]))) - 1
print(best)


import math
(x, y, l, r) = (int(i) for i in input().split())
vvrs = []
for i in range(60):
	for j in range(60):
		cur = x ** i + y ** j
		if cur > r:
			break
		if cur >= l:
			vvrs.append(cur)
vvrs.sort()
maxcnt = 0
for i in range(len(vvrs) - 1):
	rzn = vvrs[i + 1] - vvrs[i]
	if rzn > maxcnt:
		maxcnt = rzn
if len(vvrs) == 0:
	print(r - l + 1)
else:
	print(max(maxcnt - 1, vvrs[0] - l, r - vvrs[-1]))


(x, y, l, r) = list(map(int, input().split()))
xx = list()
yy = list()
a = 1
b = 1
while a <= 1e+18:
	xx.append(a)
	a = a * x
while b <= 1e+18:
	yy.append(b)
	b = b * y
st = set()
st.add(l - 1)
st.add(r + 1)
for i in xx:
	for j in yy:
		if l <= i + j <= r:
			st.add(j + i)
last = l
mx = 0
st = sorted(st)
for i in st:
	mx = max(mx, i - last - 1)
	last = i
print(mx)


from math import log, ceil
(x, y, l, r) = [int(i) for i in input().split()]
ans = 0
nm = ceil(log(r, x)) + 2
mm = ceil(log(r, y)) + 2
v = [l - 1, r + 1]
for n in range(nm):
	for m in range(mm):
		cur = x ** n + y ** m
		if cur < l or cur > r:
			continue
		v.append(cur)
v.sort()
for i in range(1, len(v)):
	ans = max(ans, v[i] - v[i - 1] - 1)
print(ans)


[x, y, l, r] = [int(i) for i in input().split()]
unlucky = []
x_pow = 0
while x ** x_pow < r and x > 1:
	x_pow += 1
y_pow = 0
while y ** y_pow < r and y > 1:
	y_pow += 1
for i in range(0, x_pow + 1):
	for j in range(0, y_pow + 1):
		unlucky.append(x ** i + y ** j)
unlucky.sort()
max_golden = 0
i = 0
while i < len(unlucky) and unlucky[i] < l:
	i += 1
if i < len(unlucky) and unlucky[i] <= r:
	max_golden = max(max_golden, unlucky[i] - l)
	while i + 1 < len(unlucky) and unlucky[i + 1] <= r:
		max_golden = max(max_golden, unlucky[i + 1] - unlucky[i] - 1)
		i += 1
	if i < len(unlucky):
		max_golden = max(max_golden, r - max(l, unlucky[i]))
else:
	max_golden = r - l + 1
print(max_golden)


(x, y, l, r) = map(int, input().split())
xl = [1]
while xl[-1] * x <= r:
	xl.append(xl[-1] * x)
yl = [1]
while yl[-1] * y <= r:
	yl.append(yl[-1] * y)
xyl = {a + b for a in xl for b in yl if l <= a + b <= r}
if xyl:
	xyl = sorted(list(xyl))
	ans = max(xyl[0] - l, r - xyl[-1])
	for i in range(len(xyl) - 1):
		ans = max(ans, xyl[i + 1] - xyl[i] - 1)
	print(ans)
else:
	print(r - l + 1)


(x, y, l, r) = input().split()
x = int(x)
y = int(y)
l = int(l)
r = int(r)
a = [1]
b = [1]
INF = 10 ** 18
while a[-1] < INF:
	a.append(a[-1] * x)
while b[-1] < INF:
	b.append(b[-1] * y)
c = []
for i in a:
	for j in b:
		c.append(i + j)
c = list(set(c))
c.sort()
MAX = 0
last = l
for i in c:
	if i < l:
		continue
	if i > r:
		MAX = max(MAX, r - last + 1)
		break
	MAX = max(MAX, i - last)
	last = i + 1
print(MAX)


(x, y, l, r) = list(map(int, input().strip().split()))
(xC, yC) = ([1], [1])
while xC[-1] < r:
	a = xC[-1] * x
	xC.append(a)
while yC[-1] < r:
	a = yC[-1] * y
	yC.append(a)
summing = []
for i in xC:
	for j in yC:
		if l <= i + j <= r:
			summing.append(i + j)
summing = list(set(summing))
summing.sort(reverse=True)
summing.append(l - 1)
summing = summing[::-1]
summing.append(r + 1)
m = 0
for i in range(len(summing) - 1):
	m = max(m, summing[i + 1] - summing[i] - 1)
print(m)


(x, y, l, r) = map(int, input().split())
a = []
b = []
cur = 1
while cur <= 1e+18:
	a.append(cur)
	cur *= x
cur = 1
while cur <= 1e+18:
	for i in a:
		if l <= i + cur <= r:
			b.append(i + cur)
	cur *= y
b.sort()
ans = 0
for i in range(1, len(b)):
	ans = max(ans, b[i] - b[i - 1] - 1)
if len(b) > 0:
	ans = max(ans, b[0] - l)
	ans = max(ans, r - b[-1])
else:
	ans = max(ans, r - l + 1)
print(ans)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
(x, y, l, r) = inpl()
se = set()
for a in range(100):
	if x ** a > r:
		break
	for b in range(100):
		now = x ** a + y ** b
		if now > r:
			break
		if now >= l:
			se.add(now)
res = 0
se = list(se)
se.sort()
ln = len(se)
for i in range(ln - 1):
	res = max(res, se[i + 1] - se[i] - 1)
if ln == 0:
	print(r - l + 1)
else:
	res = max(res, se[0] - l)
	res = max(res, r - se[-1])
	print(res)


(x, y, l, r) = list(map(int, input().split()))
xs = []
ys = []
ans = []
for i in range(61):
	xs.append(x ** i)
for i in range(61):
	ys.append(y ** i)
for i in range(61):
	for k in range(61):
		if l <= xs[i] + ys[k] <= r:
			ans.append(xs[i] + ys[k])
pref = l
ans.append(l - 1)
ans.append(r + 1)
ans.sort()
a = 0
for i in range(len(ans)):
	a = max(a, ans[i] - pref - 1)
	pref = ans[i]
print(a)


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
(x, y, l, r) = rints()
li = [l - 1, r + 1]
for i in range(65):
	for j in range(65):
		s = x ** i + y ** j
		if l <= s <= r:
			li.append(s)
li = sorted(li)
mx = 0
for i in range(len(li) - 1):
	mx = max(mx, li[i + 1] - li[i] - 1)
print(mx)


(x, y, l, r) = list(map(int, input().strip().split(' ')))
cx = 1
cy = 1
v = []
while True:
	if cx > r:
		break
	cy = 1
	while True:
		if cx + cy > r:
			break
		if cx + cy >= l:
			v.append(cx + cy)
		cy = cy * y
	cx = cx * x
v.append(l - 1)
v.append(r + 1)
v.sort()
vd = []
res = 0
for i in range(len(v) - 1):
	if v[i] == v[i + 1]:
		continue
	res = max(res, v[i + 1] - v[i] - 1)
print(res)


from math import *
(x, y, l, r) = input().split()
x = int(x)
y = int(y)
l = int(l)
r = int(r)
n = log2(r)
n = ceil(n)
A = [l - 1]
for i in range(n):
	xx = x ** i
	s = 0
	j = 0
	while s <= r:
		if s >= l:
			A.append(s)
		yy = y ** j
		s = xx + yy
		j += 1
A.sort()
A.append(r + 1)
B = [1]
for i in range(1, len(A)):
	if A[i] - A[i - 1] > 1:
		B.append(A[i] - A[i - 1])
print(max(B) - 1)


(sx, sy, sl, sr) = input().split()
x = int(sx)
y = int(sy)
l = int(sl)
r = int(sr)
qx = []
now = 1
while now <= r:
	qx.append(now)
	now *= x
qy = []
now = 1
while now <= r:
	qy.append(now)
	now *= y
q = []
for i in qx:
	for j in qy:
		if l <= i + j and i + j <= r:
			q.append(i + j)
q.sort()
c = []
lenq = len(q)
ans = 0
if lenq > 0:
	ans = r - q[lenq - 1]
else:
	ans = r - l + 1
for i in range(0, lenq):
	if i == 0:
		ans = max(ans, q[i] - l)
	else:
		ans = max(ans, q[i] - q[i - 1] - 1)
print(ans)


inp = input().split()
x = int(inp[0])
y = int(inp[1])
l = int(inp[2])
r = int(inp[3])
list = []
for i in range(0, 64):
	for j in range(0, 64):
		t = x ** i + y ** j
		if t >= l and t <= r:
			list.append(t)
list.sort()
res = 0
if len(list) == 0:
	print(r - l + 1)
	quit()
for i in range(1, len(list)):
	res = max(res, list[i] - list[i - 1] - 1)
res = max(list[0] - l, res)
res = max(res, r - list[len(list) - 1])
print(res)


(x, y, l, r) = map(int, input().split())
one = []
two = []
X = 1
while X <= r:
	one += [X]
	X *= x
Y = 1
while Y <= r:
	two += [Y]
	Y *= y
ans = [l - 1, r + 1]
for x in one:
	for y in two:
		if x + y >= l and x + y <= r:
			ans += [x + y]
ans = sorted(ans)
res = 0
for i in range(1, len(ans)):
	res = max(res, ans[i] - ans[i - 1])
print(res - 1)


R = range(65)
(x, y, l, r) = map(int, input().split())
g = [l - 1, r + 1]
for a in [x ** i for i in R if x ** i <= r]:
	for b in [y ** i for i in R if y ** i <= r]:
		t = a + b
		if l <= t and t <= r:
			g += [t]
g = list(sorted(g))
a = 0
for i in range(1, len(g)):
	a = max(a, g[i] - g[i - 1] - 1)
print(a)


(x, y, l, r) = map(int, input().split())
v = []
for a in range(64):
	for b in range(64):
		v.append(x ** a + y ** b)
v = sorted(list(filter(lambda n: l <= n <= r, v)))
v = [l - 1] + v + [r + 1]
best = max((v - u - 1 for (u, v) in zip(v, v[1:])))
print(best)


p = range(61)
(x, y, l, r) = map(int, input().split())
a = [l - 1, r + 1]
for i in [x ** i for i in p if x ** i <= r]:
	for j in [y ** i for i in p if y ** i <= r - i]:
		if i + j >= l and i + j <= r:
			a.append(i + j)
a.sort()
ans = 0
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()))
s = set()
s.add(l - 1)
s.add(r + 1)
i = 0
j = 0
while True:
	j = 0
	a = x ** i
	if a > r:
		break
	while True:
		b = a + y ** j
		j += 1
		if b > r:
			break
		if b >= l:
			s.add(b)
	i += 1
ans = 0
s = sorted(list(s))
for i in range(1, len(s)):
	ans = max(ans, s[i] - s[i - 1] - 1)
print(ans)


(x, y, l, r) = map(int, input().split())
t = 1
ans = 0
a = []
while t <= r:
	t2 = 1
	while t2 <= r:
		a.append(t + t2)
		t2 *= y
	t = t * x
a.append(l - 1)
a.append(r + 1)
a.sort()
for l2 in range(len(a) - 1):
	if a[l2] >= l - 1 and a[l2 + 1] <= r + 1 and (a[l2 + 1] - a[l2] > ans):
		ans = a[l2 + 1] - a[l2] - 1
print(ans)


(x, y, l, r) = map(int, input().split())
s1 = 1
s2 = 1
ans = 0
ll = []
while s1 <= r:
	while s2 <= r:
		if s1 + s2 <= r and s1 + s2 >= l:
			ll.append(s1 + s2)
			ans += 1
		s2 *= y
	s1 *= x
	s2 = 1
ll.sort()
maxi = 0
for i in range(len(ll) - 1):
	if maxi < ll[i + 1] - ll[i] - 1:
		maxi = ll[i + 1] - ll[i] - 1
if len(ll) == 0:
	print(r - l + 1)
else:
	if maxi < r - ll[len(ll) - 1]:
		maxi = r - ll[len(ll) - 1]
	if maxi < ll[0] - l:
		maxi = ll[0] - l
	print(maxi)


(x, y, l, r) = [int(x) for x in input().split()]
ant = 0
v = []
xp = 1
for i in range(0, 64):
	yp = 1
	for j in range(0, 64):
		if l <= xp + yp <= r:
			v.append(xp + yp)
		yp *= y
	xp *= x
v.append(r + 1)
v.sort()
l -= 1
for i in v:
	if i - l - 1 > ant:
		ant = i - l - 1
	l = i
print(ant)


(x, y, l, r) = map(int, input().split())
A = []
B = []
a = 1
while a < r:
	A.append(a)
	a *= x
b = 1
while b < r:
	B.append(b)
	b *= y
X = [l - 1, r + 1]
for a in A:
	for b in B:
		s = a + b
		if l <= s and s <= r:
			X.append(s)
X = sorted(X)
ans = 0
for i in range(len(X) - 1):
	ans = max(ans, X[i + 1] - X[i] - 1)
print(ans)


(x, y, l, r) = map(int, input().split())
li = [l - 1]
for i in range(64):
	a = x ** i
	if a > r:
		break
	for j in range(64):
		b = y ** j
		if a + b > r:
			break
		if a + b >= l:
			li.append(a + b)
li.append(r + 1)
li = sorted(li)
ans = 0
for i in range(len(li) - 1):
	ans = max(ans, li[i + 1] - li[i] - 1)
print(ans)


n = [int(i) for i in input().split()]
l = []
for i in range(100):
	if n[0] ** i > n[3]:
		break
	for j in range(100):
		if n[0] ** i + n[1] ** j > n[3]:
			break
		else:
			if 732028847861235713 == n[0] ** i + n[1] ** j:
				print('sjj')
			l.append(n[0] ** i + n[1] ** j)
l.sort()
count = 0
for i in l:
	if i >= n[2]:
		count = min(i, n[3] + 1) - n[2]
		break
for i in l[::-1]:
	if i <= n[3]:
		count = max(count, n[3] - max(n[2] - 1, i))
		break
for i in range(len(l) - 1):
	if l[i] >= n[2] and l[i + 1] <= n[3]:
		count = max(count, l[i + 1] - l[i] - 1)
assert count >= 0
if n[2] == 1 and n[3] == 1:
	print(1)
else:
	print(count)


(x, y, l, r) = map(int, input().split())
a = []
b = []
c = []
d = 0
k = 0
u = x ** k
while u <= r:
	a.append(u)
	k += 1
	u = x ** k
ter1 = k - 1
k = 0
v = y ** k
while v <= r:
	b.append(v)
	k += 1
	v = y ** k
ter2 = k - 1
for i in range(0, ter1 + 1):
	for j in range(0, ter2 + 1):
		if a[i] + b[j] >= l and a[i] + b[j] <= r:
			c.append(a[i] + b[j])
			d += 1
ans = 0
c.append(l - 1)
c.append(r + 1)
c = sorted(c)
d += 2
for i in range(1, d):
	ans = max(ans, c[i] - c[i - 1] - 1)
print(ans)


(x, y, l, r) = list(map(int, input().split()))
pointsX = set()
d = 1
while d <= r:
	pointsX.add(d)
	d *= x
pointsY = set()
d = 1
while d <= r:
	pointsY.add(d)
	d *= y
points = set()
for a in pointsX:
	for b in pointsY:
		if a + b >= l and a + b <= r:
			points.add(a + b)
points.add(l - 1)
points.add(r + 1)
points = sorted(points)
intv = ()
v = 0
for i in range(len(points) - 1):
	t = points[i + 1] - points[i] - 1
	if t > v:
		v = t
		intv = (points[i] + 1, points[i + 1] - 1)
print(v)


(Xs, Ys) = ([1], [1])
(x, y, l, r) = list(map(int, input().split()))
while Xs[-1] / x <= r:
	Xs.append(x * Xs[-1])
while Ys[-1] / y <= r:
	Ys.append(y * Ys[-1])
s = set()
for X in Xs:
	for Y in Ys:
		s.add(X + Y)
s.add(0)
ls = list(s)
ls.sort()
val = 0
for i in range(len(ls)):
	val = max(min(r + 1, ls[i]) - max(l - 1, ls[i - 1]) - 1, val)
print(val)


(x, y, l, r) = map(int, input().split())
a = 1
b = 1
arr = []
ans = 0
while a <= r * x:
	b = 1
	while b <= r * y:
		arr.append(a + b)
		b = b * y
	a = a * x
arr.append(l - 1)
arr.append(r + 1)
arr.sort()
for z in range(1, len(arr)):
	if arr[z] <= r + 1:
		if arr[z - 1] >= l - 1:
			if arr[z] - arr[z - 1] - 1 > ans:
				ans = arr[z] - arr[z - 1] - 1
print(ans)


(x, y, l, r) = [int(masuk) for masuk in input().split()]
ans = 0
buruk = []
for a in range(70):
	for b in range(70):
		kiri = pow(x, a)
		kanan = pow(y, b)
		wew = kiri + kanan
		if wew > r:
			break
		if wew < l:
			continue
		if not wew in buruk:
			buruk.append(wew)
if not l - 1 in buruk:
	buruk.append(l - 1)
if not r + 1 in buruk:
	buruk.append(r + 1)
buruk.sort()
for i in range(len(buruk) - 1):
	ans = max(ans, buruk[i + 1] - buruk[i] - 1)
print(ans)


(x, y, l, r) = map(int, input().split())
a = set()
i = 0
j = 0
while x ** i <= r:
	j = 0
	while x ** i + y ** j <= r:
		if x ** i + y ** j >= l and x ** i + y ** j <= r:
			a.add(x ** i + y ** j)
		j += 1
	i += 1
a = list(a)
a.sort()
ma = 0
length = len(a)
for i in range(1, length):
	if a[i] - a[i - 1] - 1 > ma:
		ma = a[i] - a[i - 1] - 1
if len(a) > 0:
	if a[0] - l > ma:
		ma = a[0] - l
	if r - a[length - 1] > ma:
		ma = r - a[length - 1]
	print(ma)
else:
	print(r - l + 1)


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


from sys import stdin, stderr
from bisect import bisect

def readInts():
	return map(int, stdin.readline().strip().split())

def print_err(*args, **kwargs):
	print(*args, file=stderr, **kwargs)

def solve(x, y, l, r):

	def inr(v):
		if v < l - 1:
			return False
		if v > r + 1:
			return False
		return True
	unluckies = all_unlucky(x, y)
	unluckies.add(l - 1)
	unluckies.add(r + 1)
	unluckies = filter(inr, sorted(unluckies))
	deltas = []
	first = True
	prev = None
	for u2 in unluckies:
		if not first:
			deltas.append(u2 - prev - 1)
		prev = u2
		first = False
	return max(deltas)

def all_unlucky(x, y):
	out = set()
	for a in range(100):
		if x ** a > 10 ** 18:
			break
		for b in range(100):
			if x ** a + y ** b > 10 ** 18:
				break
			out.add(x ** a + y ** b)
	return out

def run():
	(x, y, l, r) = readInts()
	print(solve(x, y, l, r))
run()


(x, y, l, r) = map(int, input().split())
vec = []
i = j = -1
while i <= 65:
	i += 1
	j = -1
	while j <= 65:
		j += 1
		if l <= x ** i + y ** j <= r:
			vec.append(x ** i + y ** j)
vec.append(l - 1)
vec.append(r + 1)
vec.sort()
maxi = 0
for i in range(1, len(vec)):
	maxi = max(maxi, vec[i] - vec[i - 1] - 1)
print(maxi)


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


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) = map(int, input().split())
vx = 1
vy = 1
xs = []
ys = []
arr = []
while vx < r:
	xs.append(vx)
	vx *= x
while vy < r:
	ys.append(vy)
	vy *= y
for i in xs:
	for j in ys:
		if i + j >= l and i + j <= r:
			arr.append(i + j)
arr = sorted(arr)
last = l
result = 0
i = 0
for e in arr:
	if i == 0:
		result = max(result, e - last)
	else:
		result = max(result, e - last - 1)
	i += 1
	last = e
if len(arr) > 0:
	result = max(result, r - arr[-1])
else:
	result = r - l + 1
print(str(result))


inp = input().split()
x = int(inp[0])
y = int(inp[1])
l = int(inp[2])
r = int(inp[3])
xx = 1
yy = 1
tst = []
while xx <= int(r):
	yy = 1
	while yy <= r:
		if xx + yy >= l and xx + yy <= r and (xx + yy not in tst):
			tst.append(xx + yy)
		yy *= y
	xx *= x
tst.sort()
ans = 0
for i in range(1, len(tst)):
	ans = max(ans, tst[i] - tst[i - 1] - 1)
if len(tst) > 0:
	ans = max(ans, tst[0] - l)
if len(tst) > 0:
	ans = max(ans, r - tst[len(tst) - 1])
if len(tst) == 0:
	ans = max(ans, r - l + 1)
print(ans)


(x, y, l, r) = map(int, input().split())
all_reprs = set()
a = 0
while x ** a <= r:
	b = 0
	while x ** a + y ** b <= r:
		all_reprs.add(x ** a + y ** b)
		b += 1
	a += 1
sl = sorted(list(all_reprs))
result = 0
a = l
i = 0
while i < len(sl) and sl[i] < l:
	i += 1
prev = l - 1
while i < len(sl) and sl[i] <= r:
	result = max([result, sl[i] - prev - 1])
	prev = sl[i]
	i += 1
result = max([result, r - prev])
print(result)


def solve():
	(x, y, l, r) = [int(s) for s in input().split(' ')]

	def pows(x):
		z = 1
		p = set()
		while z <= 10 ** 18:
			p.add(z)
			z *= x
		return p
	x_pows = pows(x)
	y_pows = pows(y)
	fails = [l - 1]
	for a in x_pows:
		for b in y_pows:
			if l <= a + b <= r:
				fails.append(a + b)
	fails.sort()
	fails.append(r + 1)
	m = 0
	for i in range(1, len(fails)):
		m = max(fails[i] - fails[i - 1] - 1, m)
	return m
print(solve())


(x, y, l, r) = map(int, input().split())
px = [1] * 65
py = [1] * 65
for i in range(1, 65):
	px[i] = px[i - 1] * x
	py[i] = py[i - 1] * y
s = set()
for i in range(65):
	for j in range(65):
		v = px[i] + py[j]
		if l <= v <= r:
			s.add(v)
if not s:
	print(r - l + 1)
	exit(0)
s = sorted(list(s))
result = max(s[0] - l, r - s[-1])
for i in range(1, len(s)):
	result = max(result, s[i] - s[i - 1] - 1)
print(result)


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


import sys
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

def main():
	(x, y, l, r) = get_ints()
	v = set()
	v.add(l - 1)
	v.add(r + 1)
	for i in range(61):
		curx = x ** i
		if curx > r:
			break
		for j in range(61):
			cury = y ** j
			if cury > r:
				break
			if curx + cury >= l and curx + cury <= r:
				v.add(curx + cury)
	v = sorted(list(v))
	ans = 0
	for i in range(1, len(v)):
		ans = max(ans, v[i] - v[i - 1] - 1)
	print(ans)
main()


import sys
from bisect import bisect_left as lb
from collections import deque
from queue import PriorityQueue as pq
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7
(x, y, l, r) = il()
a = [0]
for i in range(70):
	for j in range(70):
		a.append(x ** i + y ** j)
a.sort()
ans = 0
for i in range(len(a) - 1):
	ans = max(ans, min(r, a[i + 1] - 1) - max(l, a[i] + 1) + 1)
print(ans)


(x, y, l, r) = map(int, input().split())
fx = [1]
fy = [1]
while fx[-1] <= r:
	fx.append(fx[-1] * x)
while fy[-1] <= r:
	fy.append(fy[-1] * y)
f = [l - 1, r + 1]
for i in fx:
	for j in fy:
		f.append(i + j)
f.sort()
ans = 0
for i in range(len(f) - 1):
	if f[i + 1] - f[i] > 1 and f[i + 1] > l and (f[i] < r):
		ans = max(ans, min(r, f[i + 1] - 1) - max(l, f[i] + 1) + 1)
print(ans)


(x, y, l, r) = map(int, input().split())
z = []
lm = 0
rm = 0
for n in range(63):
	for k in range(63):
		v = x ** n + y ** k
		if v > r:
			break
		if v >= l and v <= r:
			z.append(v)
		if v == l:
			lm = 1
		if v == r:
			rm = 1
if lm == 0:
	z.append(l - 1)
if rm == 0:
	z.append(r + 1)
z.sort()
kl = 0
for n in range(len(z) - 1):
	kl = max(kl, z[n + 1] - z[n] - 1)
print(kl)


s = input()
aa = s.split()
x = int(aa[0])
y = int(aa[1])
l = int(aa[2])
r = int(aa[3])
vbad = []
for a in range(0, 1000):
	if x ** a > r:
		break
	for b in range(0, 1000):
		t = x ** a + y ** b
		if t < l:
			continue
		if t > r:
			break
		vbad.append(t)
vbad.append(l - 1)
vbad.append(r + 1)
vbad.sort()
md = 0
for i in range(0, len(vbad) - 1):
	if vbad[i + 1] - vbad[i] > md:
		md = vbad[i + 1] - vbad[i]
print(md - 1)


(x, y, l, r) = map(int, list(input().split(' ')))
ar = []
x1 = 1
for i in range(1, 70):
	y1 = 1
	for j in range(1, 70):
		ar.append(x1 + y1)
		y1 *= y
		if y1 > r:
			break
	x1 *= x
	if x1 > r:
		break
ar.append(l - 1)
ar.append(r + 1)
ar.sort()
i_l = -1
i_r = -1
for i in range(len(ar)):
	if ar[i] == l - 1:
		i_l = i
	if ar[i] == r + 1:
		i_r = i
ans = -1
for i in range(i_l, i_r):
	if ar[i + 1] - ar[i] - 1 > ans:
		ans = ar[i + 1] - ar[i] - 1
print(ans)


import sys
import math
from collections import defaultdict, deque
import heapq
(x, y, l, r) = map(int, sys.stdin.readline().split())
left1 = math.ceil(math.log(l, x))
left2 = math.ceil(math.log(l, y))
right1 = math.floor(math.log(r, x))
right2 = math.floor(math.log(r, y))
a = set()
for i in range(right1 + 1):
	cnt = 0
	while pow(x, i) + pow(y, cnt) <= r:
		z = pow(x, i) + pow(y, cnt)
		if z >= l:
			a.add(z)
		cnt += 1
for i in range(right2 + 1):
	cnt = 0
	while pow(y, i) + pow(x, cnt) <= r:
		z = pow(y, i) + pow(x, cnt)
		if z >= l:
			a.add(z)
		cnt += 1
a.add(l - 1)
a.add(r + 1)
a = list(a)
a.sort()
ans = 0
n = len(a)
for i in range(n - 1):
	dif = a[i + 1] - a[i] - 1
	ans = max(ans, dif)
print(ans)


(x, y, l, r) = map(int, input().split())
lst = []
pwdX = 1
pwdY = 1
for i in range(0, 65):
	pwdY = 1
	for j in range(0, 65):
		if pwdX + pwdY > r:
			lst.append(pwdX + pwdY)
			break
		toAdd = pwdX + pwdY
		lst.append(toAdd)
		pwdY *= y
	pwdX *= x
lst.append(r + 1)
lst.append(l - 1)
lst.sort()
maximum = 0
for i in range(0, len(lst) - 1):
	if lst[i] >= l - 1 and lst[i + 1] <= r + 1:
		maximum = max(maximum, lst[i + 1] - lst[i] - 1)
print(maximum)


(x, y, l, r) = list(map(int, input().split()))
a = []
i = 0
while True:
	if x ** i > 10 ** 19:
		break
	else:
		a.append(x ** i)
		i += 1
b = []
i = 0
while True:
	if y ** i > 10 ** 19:
		break
	else:
		b.append(y ** i)
		i += 1
c = [0]
for i in range(len(a)):
	for j in range(len(b)):
		c.append(a[i] + b[j])
c.append(10 ** 19)
(e, f) = (0, 0)
if l not in c:
	e = 1
if r not in c:
	f = 1
c.append(l)
c.append(r)
c.sort()
for i in range(len(c)):
	if c[i] > l:
		j = i - 1
		break
for i in range(len(c)):
	if c[i] > r:
		k = i - 1
		break
d = [0]
for i in range(j, k):
	d.append(c[i + 1] - c[i] - 1)
if len(d) == 1:
	if e == 1 or f == 1:
		print(1)
	else:
		print(0)
else:
	if e == 1:
		d[1] += 1
	if f == 1:
		d[-1] += 1
	print(max(d))


lx = [1]
(x, y, Left, r) = map(int, input().split())
X = x
while X <= r:
	lx.append(X)
	X *= x
ly = [1]
Y = y
while Y <= r:
	ly.append(Y)
	Y *= y
L = []
for i in lx:
	for j in ly:
		L.append(i + j)
L = set(L)
l = [Left - 1]
for i in L:
	if Left <= i <= r:
		l.append(i)
l.sort()
l.append(r + 1)
ans = 0
for i in range(1, len(l)):
	ans = max(ans, l[i] - l[i - 1] - 1)
print(ans)