#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5;
long long int a1[5000], a2[5000];
int idx1, idx2;
void pre(long long int x, long long int y) {
  a1[0] = 1;
  a2[0] = 1;
  for (long long int i = 1; i <= 60; i++) {
    int tmp = 1;
    if (a1[i - 1] <= 1e18 / x) a1[i] = a1[i - 1] * x, idx1 = i, tmp = 0;
    if (a2[i - 1] <= 1e18 / y) a2[i] = a2[i - 1] * y, idx2 = i, tmp = 0;
    if (tmp) break;
  }
  return;
}
long long int ans[N];
int main() {
  ios_base::sync_with_stdio(false);
  long long int x, y, l, r;
  cin >> x >> y >> l >> r;
  pre(x, y);
  vector<long long int> v;
  map<long long int, long long int> mp;
  for (int i = 0; i <= idx1; i++) {
    if (a1[i] == 0) continue;
    for (int j = 0; j <= idx2; j++) {
      if (a2[j] == 0) continue;
      if (mp[a1[i] + a2[j]] == 0 &&
          (a1[i] + a2[j] >= l && a1[i] + a2[j] <= r)) {
        v.push_back(a1[i] + a2[j]);
        mp[a1[i] + a2[j]] = 1;
      }
    }
  }
  sort(v.begin(), v.end());
  if (v.size() == 0) {
    cout << r - l + 1 << endl;
    return 0;
  }
  long long int mx = 0;
  int len = v.size();
  for (int i = 1; i < len; i++) {
    mx = max(v[i] - v[i - 1] - 1, mx);
  }
  if (v.size() > 0) {
    mx = max(abs(v[0] - l), mx);
    mx = max(abs(r - v[len - 1]), mx);
  }
  cout << mx << endl;
  return 0;
}
