#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  long long int x, y, l, r, c = 1, d = 1, z = 0;
  vector<long long int> a = {1}, b = {1}, w;
  cin >> x >> y >> l >> r;
  while (1) {
    c *= x;
    a.push_back(c);
    if (log10(c) + log10(x) > 18.5) break;
  }
  while (1) {
    d *= y;
    b.push_back(d);
    if (log10(d) + log10(y) > 18.5) break;
  }
  for (long long int i : a)
    for (long long int j : b) w.push_back(i + j);
  w.push_back(0);
  sort(w.begin(), w.end());
  w.push_back((long long int)1000000007 * 1000000007);
  for (int i = 1; i < w.size(); i++) {
    z = max(z, min(w[i] - 1, r) - max(w[i - 1] + 1, l) + 1);
  }
  cout << z;
}
