#include <bits/stdc++.h>
using namespace std;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const long long LINF = 1000000000000000000;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline void add(T& a, T b) {
  a = ((a + b) % MOD + MOD) % MOD;
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  vector<long long> xx, yy, cand;
  long long tmp = 1;
  xx.push_back(1), yy.push_back(1);
  while (tmp <= LINF / x) {
    tmp *= x;
    xx.push_back(tmp);
  }
  tmp = 1;
  while (tmp <= LINF / y) {
    tmp *= y;
    yy.push_back(tmp);
  }
  for (int i = 0; i < xx.size(); i++)
    for (int j = 0; j < yy.size(); j++) {
      if (xx[i] + yy[j] < l || r < xx[i] + yy[j]) continue;
      cand.push_back(xx[i] + yy[j]);
    }
  sort(cand.begin(), cand.end());
  cand.erase(unique(cand.begin(), cand.end()), cand.end());
  long long ma = 0;
  long long m = cand.size();
  if (m == 0)
    cout << r - l + 1 << endl;
  else {
    chmax(ma, cand[0] - l);
    chmax(ma, r - cand[m - 1]);
    for (int i = 0; i < m - 1; i++) chmax(ma, cand[i + 1] - cand[i] - 1);
    cout << ma << endl;
  }
}
