#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int x, y, l, r;
  cin >> x >> y >> l >> r;
  vector<long long int> v1, v2;
  long long int i = 1;
  v1.push_back(1);
  while ((1e18 / i) >= x) {
    i *= x;
    v1.push_back(i);
  }
  i = 1;
  v2.push_back(1);
  while ((1e18 / i) >= y) {
    i *= y;
    v2.push_back(i);
  }
  map<long long int, long long int> m;
  for (i = 0; i < v1.size(); i++) {
    for (long long int j = 0; j < v2.size(); j++) {
      m[v1[i] + v2[j]]++;
    }
  }
  m[l - 1]++;
  m[r + 1]++;
  auto it = m.find(l - 1);
  long long int out = 0;
  for (; it != m.end(); it++) {
    if ((it->first) == r + 1) {
      break;
    }
    auto itt = it;
    itt++;
    out = max(out, ((itt->first) - (it->first)) - 1);
  }
  cout << out;
  return 0;
}
