#include <bits/stdc++.h>
using namespace std;
vector<long long> unl;
map<long long, bool> mapa;
int main() {
  long long x, y, x1, y1, l, r, px = 1, py = 1;
  scanf("%lld %lld", &x, &y);
  scanf("%lld %lld", &l, &r);
  for (int i = 0; i < 65; i++) {
    if (px + 1 <= r) {
      for (int j = 0; j < 65; j++) {
        if (py + px <= r) {
          if (py + px >= l) {
            if (mapa.find(py + px) == mapa.end()) {
              mapa[py + px] = true;
              unl.push_back(py + px);
            }
          }
        } else
          break;
        if (py <= (r - px) / y)
          py *= y;
        else
          break;
      }
      py = 1;
      if (px <= (r - 1) / x)
        px *= x;
      else
        break;
    } else
      break;
  }
  sort(unl.begin(), unl.end());
  if ((int)unl.size() > 0) {
    long long ans = unl[0] - l;
    for (int i = 1; i < (int)unl.size(); i++) {
      ans = max(ans, unl[i] - unl[i - 1] - 1);
    }
    ans = max(ans, r - unl[(int)unl.size() - 1]);
    printf("%lld\n", ans);
  } else
    printf("%lld\n", r - l + 1);
  return 0;
}
