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