#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  long long vx = 1, vy = 1;
  vector<long long> a;
  long long maxx = r / x, val, maxg = 0, maxy;
  while (vx <= r) {
    vy = 1;
    maxy = (r - vx) / y;
    while (vy <= r - vx) {
      val = vx + vy;
      if (val >= l && val <= r) {
        a.push_back(val);
      }
      if (vy > maxy) break;
      vy *= y;
    }
    if (vx > maxx) break;
    vx *= x;
  }
  long long pre = l;
  int si = a.size();
  if (!si) {
    cout << (r - l + 1);
    return 0;
  }
  sort((a).begin(), (a).end());
  for (int(i) = 0; (i) < (si); (i)++) {
    maxg = max(maxg, a[i] - pre - 1);
    pre = a[i];
  }
  maxg = max(maxg, r - a[si - 1]);
  maxg = max(maxg, a[0] - l);
  cout << maxg;
  return 0;
}
