#include <bits/stdc++.h>
using namespace std;
long long x, y, l, r;
long long xx[105], yy[105];
vector<long long> ans;
void readcase() {
  cin >> x >> y >> l >> r;
  xx[0] = 1;
  yy[0] = 1;
  int lena = 1;
  int lenb = 1;
  while (r / x >= xx[lena - 1]) {
    xx[lena] = xx[lena - 1] * x;
    lena++;
  }
  while (r / y >= yy[lenb - 1]) {
    yy[lenb] = yy[lenb - 1] * y;
    lenb++;
  }
  for (int i = 0; i < lena; i++) {
    for (int j = 0; j < lenb; j++) {
      long long temp = xx[i] + yy[j];
      if (temp <= r && temp >= l) ans.push_back(temp);
    }
  }
  sort(ans.begin(), ans.end());
}
int main() {
  readcase();
  long long maxlen = 0;
  if (ans.size() == 0)
    maxlen = r - l + 1;
  else
    maxlen = max(ans[0] - l, r - ans[ans.size() - 1]);
  for (int i = 0; i < ans.size(); i++) {
    maxlen = max(maxlen, ans[i] - l - 1);
    l = ans[i];
  }
  cout << maxlen << endl;
  return 0;
}
