#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
using db = long double;
using ii = pair<int, int>;
using ll = long long;
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
ll get(ll x, ll y) {
  if (x > 1000000000000000000ll / y) return 1000000000000000000ll + 5;
  return x * y;
}
ll x, y, l, r;
vector<ll> getShit(ll x) {
  vector<ll> shit;
  ll cur = 1;
  while (cur <= r) {
    shit.push_back(cur);
    cur = get(cur, x);
  }
  return shit;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> x >> y >> l >> r;
  vector<ll> xShit = getShit(x);
  vector<ll> yShit = getShit(y);
  vector<ll> totShit;
  totShit.push_back(l - 1);
  totShit.push_back(r + 1);
  for (auto x : xShit)
    for (auto y : yShit)
      if (x + y <= r && x + y >= l) totShit.push_back(x + y);
  sort(totShit.begin(), totShit.end());
  totShit.resize(unique(totShit.begin(), totShit.end()) - totShit.begin());
  ll ans = 0;
  for (int i = 1; i < totShit.size(); i++)
    ans = max(ans, totShit[i] - totShit[i - 1] - 1);
  cout << ans << '\n';
  return 0;
}
