#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
  o << "sz = " << vc.size() << endl << "[";
  for (const T& v : vc) o << v << ",";
  o << "]";
  return o;
}
long long x, y, L, R;
int main() {
  cin >> x >> y >> L >> R;
  R++;
  vector<long long> xs, ys;
  {
    long long a = 1;
    xs.push_back(a);
    while (true) {
      if (a >= 2e18 / x) break;
      a *= x;
      xs.push_back(a);
    }
  }
  {
    long long a = 1;
    ys.push_back(a);
    while (true) {
      if (a >= 2e18 / y) break;
      a *= y;
      ys.push_back(a);
    }
  }
  vector<long long> as;
  for (long long x : xs)
    for (long long y : ys) {
      long long a = x + y;
      if (L <= a && a < R) as.push_back(a);
    }
  as.push_back(L - 1);
  as.push_back(R);
  sort(as.begin(), as.end());
  as.erase(unique(as.begin(), as.end()), as.end());
  int K = as.size();
  long long ans = 0;
  for (int i = 0; i < (int)(K - 1); i++) ans = max(ans, as[i + 1] - as[i] - 1);
  cout << ans << endl;
}
