#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long x, y, l, r, k1 = 1, k2 = 1;
  cin >> x >> y >> l >> r;
  vector<long long> yr1, yr2, yr;
  double bound = log(LLONG_MAX) / log(x);
  for (int i = 0; i <= floor(bound); i++) {
    yr1.push_back(k1);
    k1 *= x;
  }
  bound = log(LLONG_MAX) / log(y);
  for (int i = 0; i <= floor(bound); i++) {
    yr2.push_back(k2);
    k2 *= y;
  }
  for (auto u : yr1) {
    for (auto v : yr2) {
      if (u + v >= l && u + v <= r) {
        yr.push_back(u + v);
      }
    }
  }
  yr.push_back(r + 1);
  yr.push_back(l - 1);
  sort(yr.begin(), yr.end());
  long long mx = 0;
  for (long long i = 1; i < yr.size(); i++) {
    mx = max(mx, yr[i] - yr[i - 1] - 1);
  }
  mx = max(yr[0] - l, mx);
  mx = max(r - yr.back(), mx);
  cout << mx << "\n";
  return 0;
}
