#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  long long res = power(a, b / 2);
  long long res2 = (res * res);
  return (res2 * (b % 2 == 0 ? 1 : a));
}
int main() {
  long long first, second, l, r;
  cin >> first >> second >> l >> r;
  set<long long> ul;
  for (int i = 0; i <= log(2e18) / log(first); i++) {
    for (int j = 0; j <= log(2e18) / log(second); ++j) {
      long long res = power(first, i) + power(second, j);
      assert(res <= 4e18);
      if (res >= l and res <= r) {
        ul.insert(res);
      }
    }
  }
  ul.insert(l - 1);
  ul.insert(r + 1);
  auto it = ul.begin();
  long long res = 0;
  long long prev = *it;
  it++;
  for (; it != ul.end(); ++it) {
    res = max(res, (*it) - prev - 1);
    prev = *it;
  }
  cout << res;
}
