#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, l, r, temp, result = 0, last, temp1, temp2, ans;
  set<long long> first, second, sum;
  cin >> a >> b >> l >> r;
  last = l;
  ans = 1;
  first.insert(ans);
  while (r / ans >= a) {
    ans *= a;
    first.insert(ans);
  }
  ans = 1;
  second.insert(ans);
  while (r / ans >= b) {
    ans *= b;
    second.insert(ans);
  }
  for (set<long long>::iterator first_it = first.begin();
       first_it != first.end(); first_it++) {
    for (set<long long>::iterator second_it = second.begin();
         second_it != second.end(); second_it++) {
      if (r - *first_it < *second_it) break;
      sum.insert(*first_it + *second_it);
    }
  }
  for (set<long long>::iterator sum_it = sum.begin(); sum_it != sum.end();
       sum_it++) {
    if (*sum_it < l) continue;
    temp = (*sum_it - last);
    last = *sum_it + 1;
    if (temp > result) result = temp;
  }
  if (!sum.empty()) {
    set<long long>::iterator sum_it = sum.end();
    sum_it--;
    if (*sum_it >= l)
      temp = r - *sum_it;
    else
      temp = r - l + 1;
    if (temp > result) result = temp;
  } else
    result = r - l + 1;
  cout << result << endl;
  return 0;
}
