#include <bits/stdc++.h>
using namespace std;
int long long x, y, l, r, k, i, j, ans;
vector<int long long> mex, mey, my;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> x >> y >> l >> r;
  for (k = 1; k <= 1e18 / x; k *= x) mex.push_back(k);
  if (k <= 1e18) mex.push_back(k);
  for (k = 1; k <= 1e18 / y; k *= y) mey.push_back(k);
  if (k <= 1e18) mey.push_back(k);
  for (i = 0; i < mex.size(); i++) {
    for (j = 0; j < mey.size(); j++) {
      if (mex[i] + mey[j] > r) break;
      if (mex[i] + mey[j] >= l) {
        my.push_back(mex[i] + mey[j]);
      }
    }
  }
  my.push_back(l - 1);
  x = l;
  sort(my.begin(), my.end());
  my.push_back(r + 1);
  for (i = 1; i < my.size(); i++) {
    ans = max(ans, my[i] - my[i - 1] - 1);
  }
  cout << ans << endl;
}
