#include <bits/stdc++.h>
using namespace std;
vector<long long> xx, yy, s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  long long cnt = 0, t = 1;
  long long temp = r;
  while (temp) {
    temp /= x;
    cnt++;
  }
  for (int i = 0; i < cnt; i++) {
    xx.push_back(t);
    t *= x;
  }
  cnt = 0, t = 1, temp = r;
  while (temp) {
    temp /= y;
    cnt++;
  }
  for (int i = 0; i < cnt; i++) {
    yy.push_back(t);
    t *= y;
  }
  for (int i = 0; i < xx.size(); i++) {
    for (int j = 0; j < yy.size(); j++) {
      if (xx[i] + yy[j] >= l && xx[i] + yy[j] <= r) s.push_back(xx[i] + yy[j]);
    }
  }
  sort(s.begin(), s.end());
  if (!s.size()) {
    cout << r - l + 1 << endl;
    return 0;
  }
  long long ans = max(s[0] - l, r - s[s.size() - 1]);
  for (int i = 0; i < s.size(); i++) ans = max(ans, s[i] - s[i - 1] - 1);
  cout << ans << endl;
  return 0;
}
