#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using pill = pair<int, ll>;
using vvi = vector<vi>;
ll gcd(ll a, ll b) {
  ll t;
  while ((t = a % b) != 0) {
    a = b;
    b = t;
  }
  return b;
}
ll fast_exp(ll base, ll n, ll m) {
  if (n == 0) return 1;
  ll t = fast_exp(base, n / 2, m);
  if (n % 2 == 0)
    return (t * t) % m;
  else
    return (((t * t) % m) * base) % m;
}
bool is_set(int i, ll mask) { return (mask >> i) & 1; }
int count_bits(ll mask) {
  int ans = 0;
  for (int i = 0; i < 64; ++i)
    if (is_set(i, mask)) ++ans;
  return ans;
}
int first_bit(ll mask) {
  int i = 0;
  while (i < 64)
    if (is_set(i++, mask)) return i - 1;
  return -1;
}
int main() {
  ios::sync_with_stdio(false);
  ll x, y, l, r;
  cin >> x >> y >> l >> r;
  int e1 = log(r) / log(x), e2 = log(r) / log(y);
  vll vx, vy;
  ll val = 1, i = 0;
  while (i <= e1) vx.push_back(val), val *= x, i++;
  val = 1, i = 0;
  while (i <= e2) vy.push_back(val), val *= y, i++;
  vll v;
  v.push_back(l - 1);
  for (auto i : vx)
    for (auto j : vy)
      if (i + j >= l and i + j <= r) v.push_back(i + j);
  v.push_back(r + 1);
  sort(v.begin(), v.end());
  ll ans = 0;
  for (int i = 1; i < v.size(); ++i) ans = max(ans, v[i] - v[i - 1] - 1);
  cout << ans << "\n";
  return 0;
}
