#include <bits/stdc++.h>
using namespace std;
long long int log_floor(long long int a, long long int b) {
  long long int i = 0, p = 1, k = a / b;
  while (p <= a) {
    if (p > k) break;
    p *= b;
    if (p <= a) i++;
  }
  return i;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long int x, y, l, r, nx, ny;
  cin >> x >> y >> l >> r;
  nx = log_floor(r, x);
  ny = log_floor(1e18, y);
  long long int i, j, t, xp[nx + 1], yp[ny + 1];
  xp[0] = yp[0] = 1;
  for (i = 1; i <= nx; i++) xp[i] = xp[i - 1] * x;
  for (i = 1; i <= ny; i++) yp[i] = yp[i - 1] * y;
  set<long long int> s;
  for (i = 0; i <= nx; i++) {
    for (j = 0; j <= ny; j++) {
      t = xp[i] + yp[j];
      if (t >= l && t <= r) s.insert(t);
    }
  }
  long long int ans, k = s.size();
  if (k == 0)
    ans = r - l + 1;
  else {
    set<long long int>::iterator it, it1;
    it = it1 = s.begin();
    ans = (*it) - l;
    it++;
    for (; it != s.end(); it++, it1++) ans = max(ans, (*it) - (*it1) - 1);
    ans = max(ans, r - (*it1));
  }
  cout << ans << "\n";
  return 0;
}
