#include <bits/stdc++.h>
using namespace std;
vector<long long> v1, v2;
set<long long> S;
long long x, y, l, r;
int main() {
  cin >> x >> y >> l >> r;
  v1.push_back(1);
  v2.push_back(1);
  long long t1 = 1, t2 = 1;
  long double x1 = 1.0, x2 = 1.0;
  for (int i = 1;; i++) {
    x1 *= x;
    if (x1 > r) break;
    t1 *= x;
    v1.push_back(t1);
  }
  for (int i = 1;; i++) {
    x2 *= y;
    if (x2 > r) break;
    t2 *= y;
    v2.push_back(t2);
  }
  for (int i = 0; i < (int)v1.size(); i++)
    for (int j = 0; j < (int)v2.size(); j++)
      if (v1[i] + v2[j] <= r) S.insert(v1[i] + v2[j]);
  long long ans = max(0ll, min(*S.begin() - 1, r) - l + 1);
  for (set<long long>::iterator it = S.begin(); it != S.end(); ++it) {
    set<long long>::iterator ite = it;
    if (++ite == S.end()) break;
    ite = it;
    ans = max(ans, min(*(++ite) - 1, r) - max(l, *it + 1) + 1);
  }
  long long tmp = S.empty() ? 0 : *(--S.end()) + 1;
  ans = max(ans, r - max(tmp, l) + 1);
  printf("%I64d\n", ans);
  return 0;
}
