#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int maxn = 200;
long long x, y, l, r;
void solve() {
  set<long long> a, b, S;
  a.insert(1);
  b.insert(1);
  long long base = x;
  while (base <= INF) {
    a.insert(base);
    if (INF / base < x || x == 1) break;
    base *= x;
  }
  base = y;
  while (base <= INF) {
    b.insert(base);
    if (INF / base < y || y == 1) break;
    base *= y;
  }
  for (auto i : a)
    for (auto j : b)
      if (i + j <= r && i + j >= l) S.insert(i + j);
  S.insert(r + 1);
  long long pre = l - 1;
  long long ans = 0;
  for (auto z : S) {
    ans = max(ans, z - pre - 1);
    pre = z;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  while (cin >> x >> y >> l >> r) solve();
  return 0;
}
