#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, l, r, ans = 0;
  cin >> x >> y >> l >> r;
  vector<long long> v;
  for (int i = 0; pow((long double)x, (long double)i) <= (long double)r; i++) {
    for (int j = 0; pow((long double)x, (long double)i) +
                        pow((long double)y, (long double)j) <=
                    (long double)r;
         j++) {
      if (pow((long double)x, (long double)i) +
              pow((long double)y, (long double)j) >=
          l)
        v.push_back((long long)(pow((long double)x, (long double)i) +
                                pow((long double)y, (long double)j)));
    }
  }
  sort(v.begin(), v.end());
  if (v.size()) ans = max(ans, v[0] - l);
  for (int i = 0; i < max(0, (int)v.size() - 1); i++) {
    ans = max(ans, v[i + 1] - v[i] - 1);
  }
  if (v.size()) ans = max(ans, r - v.back());
  if (!v.size()) ans = r - l + 1;
  cout << ans << endl;
  return 0;
}
