#include <bits/stdc++.h>
using namespace std;
vector<long long int> v1, v2, vv;
unordered_map<long long int, bool> mp;
int main() {
  long long int x, y, l, r, i, j, n;
  cin >> x >> y >> l >> r;
  n = 1;
  v1.push_back(1);
  while (n <= (1e18 / x)) {
    n = n * x;
    v1.push_back(n);
  }
  n = 1;
  v2.push_back(1);
  while (n <= (1e18 / y)) {
    n *= y;
    v2.push_back(n);
  }
  for (i = 0; i < v1.size(); i++) {
    for (j = 0; j < v2.size(); j++) {
      if (!mp[v1[i] + v2[j]] && (v1[i] + v2[j]) >= l && (v1[i] + v2[j]) <= r) {
        vv.push_back(v1[i] + v2[j]);
        mp[v1[i] + v2[j]] = true;
      }
    }
  }
  if (vv.size()) sort(vv.begin(), vv.end());
  if (!vv.size()) {
    cout << r - l + 1 << endl;
    return 0;
  }
  long long int ans = 0;
  for (i = 1; i < vv.size(); i++) {
    ans = max(ans, vv[i] - vv[i - 1] - 1);
  }
  ans = max(ans, r - vv[vv.size() - 1]);
  ans = max(ans, vv[0] - l);
  cout << ans << endl;
  return 0;
}
