#include <bits/stdc++.h>
using namespace std;
void cal(long long x, long long y, long long r, long long l) {
  long long i;
  long long maxi = min(x, y);
  long long mini = max(x, y);
  long long xx = 1;
  map<long long, long long> m;
  while (xx <= r) {
    long long yy = 1;
    while (xx + yy <= r) {
      if (xx + yy >= l) {
        long long no = xx + yy;
        m[no] = 1;
      }
      if (r / yy < mini) {
        break;
      }
      yy *= mini;
    }
    if (r / xx < maxi) {
      break;
    }
    xx *= maxi;
  }
  if (m.size() == 0) {
    cout << r - l + 1 << "\n";
    return;
  }
  auto it = m.begin();
  long long le = it->first;
  long long res = le - l;
  for (auto it = m.begin(); it != m.end(); it++) {
    if (it == m.begin()) continue;
    long long tans = it->first - le - 1;
    res = max(tans, res);
    le = it->first;
  }
  long long tans = r - le;
  res = max(res, tans);
  cout << res << "\n";
}
void solve() {
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  cal(x, y, r, l);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
