#include <bits/stdc++.h>
using namespace std;
long long x, y, l, r;
double inf = 1e18;
vector<long long> a, b;
vector<long long> V;
void del(vector<long long> &now, long long x) {
  long long tmp = 1;
  while (1) {
    now.push_back(tmp);
    double nxt = tmp;
    nxt *= x;
    if (nxt > inf) break;
    tmp = tmp * x;
  }
}
int main() {
  scanf("%lld%lld%lld%lld", &x, &y, &l, &r);
  del(a, x), del(b, y), V.push_back(l - 1), V.push_back(r + 1);
  for (auto &it : a)
    for (auto &it2 : b) {
      long long tmp = it + it2;
      if (tmp >= l && tmp <= r) {
        V.push_back(tmp);
      }
    }
  sort(V.begin(), V.end());
  long long ans = 0;
  for (int i = (1); i <= ((V.size() - 1)); ++i)
    ans = max(ans, V[i] - V[i - 1] - 1);
  printf("%lld\n", ans);
  return 0;
}
