#include <bits/stdc++.h>
using namespace std;
long long x, y, l, r;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> x >> y >> l >> r;
  long long X = 1;
  set<long long> unlucky;
  while (X < r) {
    for (long long Y = 1; X + Y <= r; Y *= y) {
      if (X + Y >= l) unlucky.insert(X + Y);
      if (LLONG_MAX / y < Y) break;
    }
    if (LLONG_MAX / x < X) break;
    X *= x;
  }
  if (unlucky.size() == r - l + 1) {
    cout << "0\n";
    return 0;
  }
  if (unlucky.empty()) {
    cout << r - l + 1 << "\n";
    return 0;
  }
  long long prev = l - 1, ans = 0;
  for (long long next : unlucky) {
    ans = max(ans, next - prev - 1);
    prev = next;
  }
  ans = max(ans, r - *(--end(unlucky)));
  cout << ans << "\n";
  return 0;
}
