#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  long long x, y;
  long long l, r;
  cin >> x >> y >> l >> r;
  vector<long long> a, b, c;
  long long z = 1ll;
  while (r / z >= x) {
    a.push_back(z);
    z *= x;
  }
  a.push_back(z);
  z = 1ll;
  while (r / z >= y) {
    b.push_back(z);
    z *= y;
  }
  b.push_back(z);
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] + b[j] < l || a[i] + b[j] > r) continue;
      c.push_back(a[i] + b[j]);
    }
  }
  sort(c.begin(), c.end());
  if (!c.size())
    cout << r - l + 1;
  else {
    long long sol = c[0] - l;
    for (int i = 1; i < c.size(); i++) sol = max(sol, c[i] - c[i - 1] - 1);
    sol = max(sol, r - c[c.size() - 1]);
    cout << max(0ll, sol);
  }
  return 0;
}
