#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, l, r;
  while (cin >> x >> y >> l >> r) {
    vector<long long> v1;
    vector<long long> v2;
    set<long long> st;
    v1.push_back(1);
    v2.push_back(1);
    long long tem = 1;
    while (1) {
      if (r / x < tem) break;
      tem *= x;
      v1.push_back(tem);
    }
    tem = 1;
    while (1) {
      if (r / y < tem) break;
      tem *= y;
      v2.push_back(tem);
    }
    for (int i = 0; i < v1.size(); i++)
      for (int j = 0; j < v2.size(); j++)
        if (v1[i] + v2[j] <= r && v1[i] + v2[j] >= l) st.insert(v1[i] + v2[j]);
    long long ans = 0;
    st.insert(l - 1);
    st.insert(r + 1);
    for (set<long long>::iterator it2 = st.begin(); it2 != st.end(); it2++) {
      set<long long>::iterator tem = ++it2;
      it2--;
      if (tem != st.end()) ans = max(ans, *tem - *it2 - 1);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
