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