#include <bits/stdc++.h>
using namespace std;
long long pow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
signed main() {
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  long long inf = 1e18;
  long long a = log(inf) / log(x);
  long long b = log(inf) / log(y);
  set<long long> s;
  for (long long i = 0; i <= a; i++) {
    long long p1 = pow(x, i);
    for (long long j = 0; j <= b; j++) {
      long long p2 = pow(y, j);
      long long val = p1 + p2;
      if (val <= r and val >= l) s.insert(val);
    }
  }
  s.insert(l - 1);
  s.insert(r + 1);
  long long maxlen = 0, last = *s.begin();
  auto it = s.begin();
  it++;
  for (; it != s.end(); it++) {
    maxlen = max(maxlen, *it - last - 1);
    last = *it;
  }
  cout << maxlen << "\n";
  return 0;
}
