#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long x, y, l, r, ans = 0;
  cin >> x >> y >> l >> r;
  vector<long long> v1, v2;
  v1.push_back(1);
  long long res = x;
  v1.push_back(x);
  while (res <= 1e18 / x) {
    res *= x;
    v1.push_back(res);
  }
  v2.push_back(1);
  res = y;
  v2.push_back(res);
  while (res <= 1e18 / y) {
    res *= y;
    v2.push_back(res);
  }
  vector<long long> v3;
  for (int i = 0; i < v1.size(); ++i)
    for (int j = 0; j < v2.size(); ++j) v3.push_back(v1[i] + v2[j]);
  sort(v3.begin(), v3.end());
  std::vector<long long> v;
  for (int i = 0; i < v3.size(); ++i) {
    if (v3[i] >= l && v3[i] <= r) v.push_back(v3[i]);
  }
  sort(v.begin(), v.end());
  for (int i = 1; i < v.size(); ++i) ans = max(ans, v[i] - v[i - 1] - 1);
  if (v.size() == 0)
    ans = r - l + 1;
  else {
    ans = max(ans, v[0] - l);
    ans = max(ans, r - v[v.size() - 1]);
  }
  cout << ans << "\n";
}
