#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T& a, const T& b) {
  return a < b ? (a = b, true) : false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}
long long X, Y, L, R;
const long long INF = 5e18;
int main() {
  scanf("%lld%lld%lld%lld", &X, &Y, &L, &R);
  vector<long long> A{L - 1, R + 1};
  {
    for (long long i = 1; i <= INF; i *= X) {
      for (long long j = 1; j <= INF; j *= Y) {
        A.push_back(i + j);
        if (j > INF / Y) break;
      }
      if (i > INF / X) break;
    }
  }
  sort(A.begin(), A.end());
  long long ans = 0;
  for (int i = (int)(1); i < (int)(A.size()); i++) {
    if (A[i - 1] >= L - 1 && A[i] <= R + 1) {
      chmax(ans, A[i] - A[i - 1] - 1);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
