#include <bits/stdc++.h>
using namespace std;
int fromFirstLength;
vector<long long> fromFirst;
int allYearsLength;
vector<long long> allYears;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long x, y, l, r;
  long long number, limit;
  long long answer = 0;
  cin >> x >> y >> l >> r;
  number = 1;
  limit = r / x;
  while (number <= r) {
    fromFirst.push_back(number);
    fromFirstLength++;
    if (number > limit) break;
    number *= x;
  }
  number = 1;
  limit = r / y;
  while (number <= r) {
    for (int i = 0; i < fromFirstLength; i++) {
      if (fromFirst[i] + number >= l && fromFirst[i] + number <= r) {
        allYears.push_back(fromFirst[i] + number);
        allYearsLength++;
      }
    }
    if (number > limit) break;
    number *= y;
  }
  sort(allYears.begin(), allYears.end());
  for (int i = 1; i < allYearsLength; i++) {
    if (allYears[i] - allYears[i - 1] - 1 > answer)
      answer = allYears[i] - allYears[i - 1] - 1;
  }
  if (allYearsLength > 0) {
    if (allYears[0] - l > answer) answer = allYears[0] - l;
    if (r - allYears[allYearsLength - 1] > answer)
      answer = r - allYears[allYearsLength - 1];
  } else
    answer = (r - l) + 1;
  cout << answer << '\n';
}
