#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, l, r, i = 1, j, mx = -1;
  cin >> x >> y >> l >> r;
  vector<long long> v;
  v.push_back(l - 1);
  v.push_back(r + 1);
  while (1) {
    j = 1;
    while (1) {
      if (i + j >= l && i + j <= r) v.push_back(i + j);
      if (r / j < y)
        break;
      else
        j *= y;
    }
    if (r / i < x)
      break;
    else
      i *= x;
  }
  sort(v.begin(), v.end());
  for (i = 0; i < v.size() - 1; i++) mx = max(mx, v[i + 1] - v[i] - 1);
  cout << mx;
}
