#include <bits/stdc++.h>
using namespace std;
bool f(long long x, long long i) {
  int cnt = 0;
  long long ii = i;
  while (i > 0) {
    i /= x;
    cnt++;
  }
  if (pow(x, cnt - 1) == ii) return true;
  return false;
}
int main() {
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  long long cnt = 0;
  long long t1 = r / x;
  long long t2 = r / y;
  vector<long long> v;
  v.push_back(l - 1ll);
  v.push_back(r + 1ll);
  for (long long i = 1;; i *= x) {
    for (long long j = 1;; j *= y) {
      if (i + j >= l && i + j <= r) v.push_back(i + j);
      if (j > r / y) break;
    }
    if (i > r / x) break;
  }
  sort(v.begin(), v.end());
  long long mx = 0ll;
  int sz = v.size() - 1;
  for (int i = 0; i < sz; i++) mx = max(mx, v[i + 1] - v[i] - 1ll);
  cout << mx << endl;
  return 0;
}
