#include <bits/stdc++.h>
using namespace std;
long long a[70];
long long b[70];
long long ans[4909];
int main() {
  long long x, y, l, r, u = 1, v = 1, w = 0;
  cin >> x >> y >> l >> r;
  a[0] = 1;
  b[0] = 1;
  for (int i = 1; i <= 65; i++) {
    if (a[u - 1] <= 1000000000000000010 / x) {
      a[u] = a[u - 1] * x;
      u++;
    }
  }
  for (int i = 1; i <= 65; i++) {
    if (b[v - 1] <= 1000000000000000010 / y) {
      b[v] = b[v - 1] * y;
      v++;
    }
  }
  for (int i = 0; i < u; i++) {
    for (int j = 0; j < v; j++) {
      if (1ll * a[i] + 1ll * b[j] >= l && 1ll * a[i] + 1ll * b[j] <= r) {
        ans[w++] = 1ll * a[i] + 1ll * b[j];
      }
    }
  }
  ans[w++] = l - 1;
  ans[w++] = r + 1;
  sort(ans, ans + w);
  long long fans = 0;
  for (int i = 1; i < w; i++) {
    if (ans[i] - ans[i - 1] - 1 > fans) {
      fans = ans[i] - ans[i - 1] - 1;
    }
  }
  cout << fans;
}
