#include <bits/stdc++.h>
using namespace std;
long long n, m, L, R, sum, ans, a[1005], b[1005], c[1005 * 10];
int ap, bp, pin;
inline long long read() {
  long long n = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    n = n * 10 + c - '0';
    c = getchar();
  }
  return n * f;
}
int main() {
  register int i, j;
  n = read();
  m = read();
  L = read();
  R = read();
  for (a[ap = 0] = sum = 1; sum <= R / n; a[++ap] = sum = sum * n)
    ;
  for (b[bp = 0] = sum = 1; sum <= R / m; b[++bp] = sum = sum * m)
    ;
  for (i = 0; i <= ap; ++i)
    for (j = 0; j <= bp; ++j) c[++pin] = a[i] + b[j];
  c[++pin] = L - 1;
  c[++pin] = R + 1;
  sort(c + 1, c + pin + 1);
  for (i = 2; i <= pin; ++i)
    if (c[i] >= L && c[i] <= R + 1) ans = max(ans, c[i] - c[i - 1] - 1);
  printf("%I64d", ans);
}
