#include <bits/stdc++.h>
using namespace std;
long long X, Y, L, R;
int main() {
  scanf("%lld %lld %lld %lld", &X, &Y, &L, &R);
  if (X == 1 && Y == 1) {
    printf("%lld", max({0ll, 2 - L, R - max(3ll, L) + 1}));
    return 0;
  }
  if (X > Y) {
    swap(X, Y);
  }
  vector<long long> nums;
  long long x = 1;
  while (x < R) {
    long long y = 1;
    if (x + 1 > R) break;
    if (x + y >= L && x + y <= R) nums.push_back(x + y);
    while (y <= (R - x) / Y) {
      y *= Y;
      if (x + y >= L) {
        assert(x + y <= R);
        nums.push_back(x + y);
      }
    }
    if (X == 1) break;
    if (x > R / X) break;
    x *= X;
  }
  sort(nums.begin(), nums.end());
  long long ret = 0;
  if (nums.empty()) {
    ret = R - L + 1;
  } else {
    if (nums[0] > L) {
      ret = nums[0] - L;
    }
    for (int i = 1; i < int(nums.size()); i++) {
      ret = max(ret, nums[i] - nums[i - 1] - 1);
    }
    if (nums[nums.size() - 1] < R) {
      ret = max(ret, R - nums[nums.size() - 1]);
    }
  }
  printf("%lld", ret);
}
