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