#include <bits/stdc++.h>
using namespace std;
vector<long long> pots(long long first) {
  double tmp = 1;
  long long xx = 1;
  vector<long long> sol;
  while (true) {
    if (tmp > 2e18) break;
    sol.push_back(xx);
    tmp *= (double)first;
    xx *= first;
  }
  return sol;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long first, second, l, r;
  cin >> first >> second >> l >> r;
  vector<long long> all = {l - 1, r + 1};
  vector<long long> pa = pots(first), push_back = pots(second);
  for (auto xx : pa)
    for (auto yy : push_back)
      if (l <= xx + yy && xx + yy <= r) all.push_back(xx + yy);
  sort(all.begin(), all.end());
  long long sol = 0;
  for (int i = (int)(0); i < (int)(all.size() - 1); ++i)
    sol = max(sol, all[i + 1] - all[i] - 1);
  cout << sol << endl;
  return 0;
}
