#include <bits/stdc++.h>
using namespace std;
void si(int &n) { scanf("%d", &n); }
void sii(int &n, int &m) { scanf("%d%d", &n, &m); }
constexpr long long int TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
const int N = 251;
const int M = 251;
const long long int INF = 1e18 + 10;
int main() {
  ios::sync_with_stdio(false);
  long long int x, y, l, r;
  cin >> x >> y >> l >> r;
  map<long long int, bool> mp;
  long long int ans = 0, aa = 1, bb = 1, n;
  for (int a = 0; a < (int)(77); ++a) {
    if (aa > r) break;
    bb = 1;
    for (int b = 0; b < (int)(77); ++b) {
      n = aa + bb;
      if (bb > r || n > r) break;
      if (n >= l && n <= r) {
        mp[n] = true;
      }
      if (bb <= INF / y)
        bb *= y;
      else
        break;
    }
    if (aa <= INF / x)
      aa *= x;
    else
      break;
  }
  mp[l - 1] = true;
  mp[r + 1] = true;
  long long int prev;
  for (auto x : mp) {
    if (x.first == l - 1) {
      prev = x.first;
      continue;
    }
    ans = max(ans, x.first - prev - 1);
    prev = x.first;
  }
  cout << ans;
}
