#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool smin(T &a, const T &b) {
  return a > b ? a = b : a;
}
template <typename T>
inline bool smax(T &a, const T &b) {
  return a < b ? a = b : a;
}
const long long N = (long long)0, mod = (long long)0, oo = (long long)1e18;
int32_t main() {
  long long first, second, l, r;
  cin >> first >> second >> l >> r;
  set<long long> st;
  for (long long p = 0; p <= 64; ++p)
    for (long long q = 0; q <= 64; ++q) {
      long long x0 = 1, y0 = 1;
      for (long long j = 0; j < p; ++j) {
        if ((long double)x0 * first > oo) {
          x0 = oo;
        } else {
          x0 = x0 * first;
        }
      }
      for (long long j = 0; j < q; ++j) {
        if ((long double)y0 * second > oo) {
          y0 = oo;
        } else {
          y0 = y0 * second;
        }
      }
      long long val = x0 + y0;
      if (l <= val && val <= r) {
        st.insert(val);
      }
    }
  vector<long long> v;
  v.push_back(l - 1);
  for (long long first : st) v.push_back(first);
  v.push_back(r + 1);
  long long res = 0;
  for (long long j = 0; j < (long long)v.size() - 1; ++j)
    res = max(res, v[j + 1] - v[j] - 1);
  cout << res << endl;
}
