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