#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int nax = 1000000 + 10;
int main() {
  ios::sync_with_stdio(0);
  vector<long long int> v1, v2, v;
  long long int x, y, l, r;
  cin >> x >> y >> l >> r;
  long long int k = 1;
  v1.push_back(k);
  while (((long double)(2e18) / k >= x)) {
    k *= x;
    v1.push_back(k);
  }
  k = 1;
  v2.push_back(k);
  while (((long double)(2e18) / k >= y)) {
    k *= y;
    v2.push_back(k);
  }
  for (auto x : v1) {
    for (auto y : v2) {
      long long int num = x + y;
      if (num >= l && num <= r) v.push_back(num);
    }
  }
  v.push_back(l - 1);
  v.push_back(r + 1);
  sort(v.begin(), v.end());
  long long int maxi = 0;
  for (int i = 1; i < v.size(); i++) maxi = max(maxi, v[i] - v[i - 1] - 1);
  cout << maxi << endl;
  return 0;
}
