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