#include <bits/stdc++.h>
using namespace std;
const int SZ = 1e2 + 5;
long long x, y, l, r, N = 1000000000000000000;
long long p1[SZ], p2[SZ];
vector<long long> res;
int main() {
  cin >> x >> y >> l >> r;
  int N1 = log(N) / log(x);
  int N2 = log(N) / log(y);
  p1[0] = 1LL, p2[0] = 1LL;
  for (int i = 1; i <= N1; i++) {
    p1[i] = p1[i - 1] * x;
  }
  for (int i = 1; i <= N2; i++) {
    p2[i] = p2[i - 1] * y;
  }
  for (int i = 0; i <= N1; i++) {
    for (int j = 0; j <= N2; j++) {
      if (p1[i] + p2[j] < l) continue;
      if (p1[i] + p2[j] > r) break;
      res.push_back(p1[i] + p2[j]);
    }
  }
  res.push_back(l - 1);
  res.push_back(r + 1);
  sort(res.begin(), res.end());
  long long len = 0;
  for (int i = 0; i < res.size() - 1; i++) {
    if (len < res[i + 1] - res[i]) len = res[i + 1] - res[i];
  }
  cout << max(0LL, len - 1);
}
