#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000000 + 7;
int64_t x, y, l, r, pp;
set<int64_t> s;
int64_t xx, yy, aa = 0;
int main() {
  cin >> x >> y >> l >> r;
  vector<int64_t> a, b;
  int64_t i = 0, j = 0, num = 1, k;
  for (num = 1; num; num *= num > r / x ? 0 : x) a.push_back(num);
  for (num = 1; num; num *= num > r / y ? 0 : y) b.push_back(num);
  for (k = 0; k < a.size(); k++) {
    for (pp = 0; pp < b.size(); pp++) {
      s.insert(a[k] + b[pp]);
    }
  }
  yy = l - 1;
  for (auto e : s) {
    if (l <= e && r >= e) aa = max(aa, e - yy - 1), yy = e;
  }
  cout << max(aa, r - yy);
  return 0;
}
