#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 3e5 + 5;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
vector<long long> a, b;
set<long long> s;
int main() {
  long long x, y, l, r;
  scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
  long long k = 1;
  for (int i = 1; i <= 64; i++) {
    a.push_back(k);
    if (r / k < x) break;
    k *= x;
  }
  k = 1;
  for (int i = 1; i <= 64; i++) {
    b.push_back(k);
    if (r / k < y) break;
    k *= y;
  }
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] + b[j] >= l && a[i] + b[j] <= r) {
        s.insert(a[i] + b[j]);
      }
    }
  }
  long long res = 0;
  k = l - 1;
  for (set<long long>::iterator ite = s.begin(); ite != s.end(); ite++) {
    res = max(res, *ite - k - 1);
    k = *ite;
  }
  res = max(res, r - k);
  cout << res << endl;
  return 0;
}
