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