#include <bits/stdc++.h>
using namespace std;
const long long _n = 1e5 + 11;
const long long inf = 1e18 + 11;
const long long mod = 1e9 + 7;
const double eps = 1e-7;
long long x, y, l, r;
vector<long long> a, b, c;
set<long long> s;
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cout << fixed;
  cout << setprecision(15);
  srand(time(NULL));
  cin >> x >> y >> l >> r;
  a.push_back(1);
  b.push_back(1);
  for (long long i = 1; i < 61; i++) {
    if (inf / a.back() < x) continue;
    a.push_back(a.back() * x);
  }
  for (long long i = 1; i < 61; i++) {
    if (inf / b.back() < y) continue;
    b.push_back(b.back() * y);
  }
  for (auto v1 : a) {
    for (auto v2 : b) {
      if (inf - v1 < v2) continue;
      s.insert(v1 + v2);
    }
  }
  long long res = 0;
  for (auto v : s)
    if (l <= v && v <= r) c.push_back(v);
  c.push_back(l - 1);
  c.push_back(r + 1);
  sort(c.begin(), c.end());
  for (long long i = 1; i < (long long)c.size(); i++) {
    res = max(res, c[i] - c[i - 1] - 1);
  }
  cout << res << '\n';
  return 0;
}
