#include <bits/stdc++.h>
using namespace std;
long long a[111], b[111];
int main() {
  long long x, y, l, r;
  scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
  a[0] = b[0] = 1;
  int cnt1 = 1, cnt2 = 1;
  while (r / x >= a[cnt1 - 1]) {
    a[cnt1] = a[cnt1 - 1] * x;
    cnt1++;
  }
  while (r / y >= b[cnt2 - 1]) {
    b[cnt2] = b[cnt2 - 1] * y;
    cnt2++;
  }
  vector<long long> ulck;
  for (int i = 0; i < cnt1; i++) {
    for (int j = 0; j < cnt2; j++) {
      if (a[i] + b[j] >= l && a[i] + b[j] <= r) {
        ulck.push_back(a[i] + b[j]);
      }
    }
  }
  sort(ulck.begin(), ulck.end());
  long long ans = 0;
  if (ulck.size())
    ans = max(ulck[0] - l, r - ulck[ulck.size() - 1]);
  else
    ans = r - l + 1;
  for (int i = 0; i < ulck.size(); i++) {
    ans = max(ans, ulck[i] - 1 - l);
    l = ulck[i];
  }
  printf("%I64d\n", ans);
  return 0;
}
