#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
long long x, y, l, r, a[10005], b[10005], ans;
int l1, r1, cnt;
set<long long> s;
void init() {
  for (long long i = 1; i <= r; i *= x) {
    for (long long j = 1; j + i <= r; j *= y) {
      s.insert(i + j);
      if ((double)i / y + j > (double)r / y) break;
    }
    if (i > (double)r / x) break;
  }
}
int main() {
  cin >> x >> y >> l >> r;
  init();
  for (auto it = s.lower_bound(l);
       it == s.end() ? (ans = max(ans, r - l + 1), 0) : 1; ++it) {
    if (it == s.lower_bound(l)) ans = max(ans, (*it) - l);
    auto it1 = ++it;
    --it;
    if (it1 == s.end()) {
      ans = max(ans, r - (*it));
      break;
    }
    long long tmp = (*it1) - (*it) - 1;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}
