#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;
  long long int num = 1;
  vector<long long int> p1, p2, sum;
  p1.push_back(num);
  while (num <= 1e18 / x) {
    num = num * x;
    p1.push_back(num);
  }
  num = 1;
  p2.push_back(num);
  while (num <= 1e18 / y) {
    num = num * y;
    p2.push_back(num);
  }
  for (long long int i = 0; i < p1.size(); i++) {
    for (long long int j = 0; j < p2.size(); j++) sum.push_back(p1[i] + p2[j]);
  }
  sum.push_back(l - 1);
  sum.push_back(r + 1);
  sort(sum.begin(), sum.end());
  unique(sum.begin(), sum.end());
  long long int ans = 0, flag1 = 0, flag2 = 0;
  for (long long int i = 0; i < sum.size() - 1; i++) {
    if (sum[i] < l - 1 || sum[i] > r + 1) continue;
    if (sum[i + 1] < l - 1 || sum[i + 1] > r + 1) continue;
    ans = max(ans, sum[i + 1] - sum[i] - 1);
  }
  cout << ans << endl;
  return 0;
}
