#include <bits/stdc++.h>
using namespace std;
int main() {
  long long l, r;
  long long x, y;
  long long a[100005];
  while (cin >> x >> y >> l >> r) {
    int k = 0;
    for (long long i = 1;; i *= x) {
      for (long long j = 1;; j *= y) {
        if (i + j >= l && i + j <= r) a[k++] = i + j;
        if (j > r / y) break;
      }
      if (i > r / x) break;
    }
    sort(a, a + k);
    if (k == 0) {
      cout << r - l + 1 << endl;
      continue;
    }
    long long ans = a[0] - l;
    a[k] = r + 1;
    for (int i = 0; i < k; i++) {
      ans = max(ans, a[i + 1] - a[i] - 1);
    }
    cout << ans << endl;
  }
}
