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