#include <bits/stdc++.h>
using namespace std;
unsigned long long x, y, l, r, mult, f;
vector<unsigned long long> v, w, u;
map<unsigned long long, bool> cek;
map<unsigned long long, bool>::iterator it;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> x >> y >> l >> r;
  mult = x;
  x = 1;
  while (x <= (unsigned long long)1e18) {
    if (x <= (unsigned long long)1e18) v.push_back(x);
    if ((unsigned long long)1e18 / mult < x) break;
    x *= mult;
  }
  mult = y;
  y = 1;
  while (y <= (unsigned long long)1e18) {
    if (y <= (unsigned long long)1e18) w.push_back(y);
    if ((unsigned long long)1e18 / mult < y) break;
    y *= mult;
  }
  for (long long i = 0; i <= v.size() - 1; i++) {
    for (long long j = 0; j <= w.size() - 1; j++) {
      if (v[i] + w[j] <= (unsigned long long)1e18) {
        cek[v[i] + w[j]] = true;
      } else
        break;
    }
  }
  for (it = cek.begin(); it != cek.end(); it++) {
    if (it->first >= l && it->first <= r) {
      u.push_back(it->first);
    }
  }
  if (u.empty()) {
    cout << r - l + 1;
    return 0;
  }
  f = u[0] - l;
  f = max(f, r - u[u.size() - 1]);
  if (u.size() >= 2) {
    for (long long i = 0; i <= u.size() - 2; i++) {
      f = max(f, u[i + 1] - u[i] - 1);
    }
  }
  cout << f;
}
