#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x, y, l, r;
  cin >> x >> y >> l >> r;
  vector<long long int> vec(0);
  long long int xa = 1;
  long long int cap = 0;
  long long int r1 = r;
  while (r1 > 0) {
    r1 /= x;
    ++cap;
  }
  for (long long int i = 0; i < cap; ++i) {
    long long int ya = 1;
    long long int diff = r - xa;
    long long int cap2 = 0;
    while (diff > 0) {
      diff /= y;
      ++cap2;
    }
    for (long long int j = 0; j < cap2; ++j) {
      if (xa + ya >= l && xa + ya <= r) {
        vec.push_back(xa + ya);
      }
      ya *= y;
    }
    xa *= x;
  }
  if (vec.size() == 0) {
    cout << r - l + 1;
    return 0;
  }
  sort(vec.begin(), vec.end());
  long long int answer = vec[0] - l;
  for (int i = 1; i < vec.size(); ++i) {
    if (vec[i] - vec[i - 1] - 1 > answer) answer = vec[i] - vec[i - 1] - 1;
  }
  if (r - vec[vec.size() - 1] > answer) answer = r - vec[vec.size() - 1];
  cout << answer;
}
