#include <bits/stdc++.h>
using namespace std;
long long int x, y, l, r;
vector<long long int> A, B, v;
int main() {
  cin >> x >> y >> l >> r;
  long long int temp = 1;
  for (int i = 0;; i++) {
    if (temp > r) break;
    A.push_back(temp);
    if (temp > 1000000000000000008 / x) break;
    temp *= x;
  }
  temp = 1;
  for (int i = 0;; i++) {
    if (temp > r) break;
    B.push_back(temp);
    if (temp > 1000000000000000008 / y) break;
    temp *= y;
  }
  map<long long int, long long int> mp;
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < B.size(); j++) {
      temp = A[i] + B[j];
      mp[temp]++;
      if (mp[temp] == 1) v.push_back(temp);
    }
  }
  v.push_back(1000000000000000018);
  v.push_back(0LL);
  sort(v.begin(), v.end());
  long long int ans = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    long long int a = v[i], b = v[i + 1];
    temp = 0;
    if (l > a) temp++;
    if (r < b) temp++;
    ans = max(ans, max(0LL, min(b, r) - max(a, l) - 1 + temp));
  }
  cout << ans << endl;
  return 0;
}
