#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long x, y, l, r, a = 1, b = 1;
  vector<long long> ss, v, w;
  cin >> x >> y >> l >> r;
  while (a <= r) {
    v.push_back(a);
    if (r / a + 4 < x) break;
    a *= x;
  }
  a = v.back();
  while (b <= r) {
    w.push_back(b);
    long long sw = 1;
    if (r / b + 4 < y) break;
    b *= y;
  }
  ss.push_back(l - 1);
  ss.push_back(r + 1);
  for (long long i = 0; i < v.size(); i++) {
    for (long long j = 0; j < w.size(); j++) {
      if (v[i] + w[j] >= l && v[i] + w[j] <= r) ss.push_back(v[i] + w[j]);
    }
  }
  sort(ss.begin(), ss.end());
  long long ans = 0;
  for (long long i = 1; i < ss.size(); i++) {
    ans = max(ans, ss[i] - ss[i - 1] - 1);
  }
  cout << ans << '\n';
}
