#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, l, r;
  while (cin >> x >> y >> l >> r) {
    set<long long> st;
    st.insert(l - 1);
    st.insert(r + 1);
    for (long long i = 1;; i *= x) {
      for (long long j = 1;; j *= y) {
        long long s = i + j;
        if (s >= l && s <= r) st.insert(s);
        if (j > r / y) break;
      }
      if (i > r / x) break;
    }
    long long maxll = 0;
    for (set<long long>::iterator it = st.begin(); it != st.end(); ++it) {
      set<long long>::iterator itplus = it;
      itplus++;
      if (itplus == st.end()) break;
      maxll = max(maxll, *itplus - *it - 1);
    }
    cout << maxll << endl;
  }
  return 0;
}
