#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long N = 1e18;
map<long long, long long> ma;
long long x[110], y[110];
int main() {
  memset(x, 0, sizeof(x));
  memset(y, 0, sizeof(y));
  long long a, b, l, r;
  scanf("%I64d%I64d%I64d%I64d", &a, &b, &l, &r);
  x[0] = 1;
  y[0] = 1;
  ma[l - 1] = 1;
  ma[r + 1] = 1;
  for (int i = 1; x[i - 1] <= N / a; i++) x[i] = x[i - 1] * a;
  for (int i = 1; y[i - 1] <= N / b; i++) y[i] = y[i - 1] * b;
  for (int i = 0; x[i] != 0; i++)
    for (int j = 0; y[j] != 0; j++)
      if (x[i] + y[j] <= r && x[i] + y[j] >= l) ma[x[i] + y[j]] = 1;
  map<long long, long long>::iterator fir, sec;
  sec = ma.begin();
  fir = ma.begin();
  sec++;
  long long ans = 0;
  for (; sec != ma.end(); fir++, sec++)
    ans = max(ans, sec->first - fir->first - 1);
  printf("%I64d", ans);
  return 0;
}
