#include <bits/stdc++.h>
long long arr[100005], count, maxx;
int main() {
  long long a, b, i, i2, now, now2, thres = 1e18, l, r;
  scanf("%lld %lld %lld %lld", &a, &b, &l, &r);
  now = 1;
  while (1) {
    now2 = 1;
    while (1) {
      arr[++count] = now + now2;
      if (thres / now2 < b) break;
      now2 *= b;
    }
    if (thres / now < a) break;
    now *= a;
  }
  arr[++count] = l - 1;
  arr[++count] = r + 1;
  std::sort(arr + 1, arr + 1 + count);
  for (i = 1; i < count; i++) {
    if (arr[i] >= l - 1 && arr[i] <= r + 1 && arr[i + 1] >= l - 1 &&
        arr[i + 1] <= r + 1)
      maxx = std::max(maxx, arr[i + 1] - arr[i] - 1);
  }
  printf("%lld", maxx);
  return 0;
}
