#include <bits/stdc++.h>
using namespace std;
long long c[100000];
long long pow(long long a, int p) {
  if (p == 0) return 1;
  return p % 2 == 1 ? (a * pow(a, p - 1)) : (pow(a * a, p / 2));
}
int main() {
  long long x, y, l, r;
  while (scanf("%lld%lld%lld%lld", &x, &y, &l, &r) != EOF) {
    for (int i = 0; i < 100000; i++) c[i] = 0;
    double a = log(r) / log(x);
    double b = log(r) / log(y);
    int m = (int)a;
    int n = (int)b;
    int p = 0;
    for (int i = 0; i <= m; i++) {
      for (int j = 0; j <= n; j++) {
        long long k = (pow(x, i) + pow(y, j));
        if (k >= l && k <= r) c[p++] = k;
      }
    }
    if (p == 0)
      printf("%lld\n", r - l + 1);
    else {
      sort(c, c + p);
      int q = 0, v = 0;
      for (q = p - 1; c[q] > r; q--)
        ;
      for (v = 0; c[v] < l; v++)
        ;
      long long ans = 0;
      ans = max(c[v] - l, r - c[q]);
      for (int i = 0; i < q; i++) ans = max(ans, c[i + 1] - c[i] - 1);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
