#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e6 + 10;
const int Mod = 1e9 + 7;
const int N = 2;
struct Matrix {
  Matrix() { memset(m, 0, sizeof m); }
  int m[N][N];
};
Matrix Mul(Matrix a, Matrix b) {
  Matrix c;
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++) {
      if (a.m[i][k] == 0) continue;
      for (int j = 0; j < N; j++)
        c.m[i][j] += a.m[i][k] * b.m[k][j] % Mod, c.m[i][j] %= Mod;
    }
  }
  return c;
}
long long Mul(long long a, long long b) {
  a %= Mod;
  b %= Mod;
  long long ret = 0;
  while (b) {
    if (b & 1) {
      ret += a;
      if (ret > Mod) ret -= Mod;
    }
    b >>= 1;
    a = (a << 1) % Mod;
  }
  return ret;
}
Matrix pows(Matrix x, long long n) {
  Matrix ret;
  for (int i = 0; i < 2; i++) ret.m[i][i] = 1;
  while (n) {
    if (n & 1) ret = Mul(ret, x);
    x = Mul(x, x);
    n >>= 1;
  }
  return ret;
}
long long pows(long long x, long long n) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = ret * x % Mod;
    x = x * x % Mod;
    n >>= 1;
  }
  return ret;
}
long long powss(long long x, long long n) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = Mul(ret, x);
    x = Mul(x, x);
    n >>= 1;
  }
  return ret;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long euler(int n) {
  int ret = n;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) {
      ret = ret / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  if (n > 1) ret = ret / n * (n - 1);
  return ret;
}
void ex_gcd(long long a, long long b, long long &d, long long &x,
            long long &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
    return;
  }
  ex_gcd(b, a % b, d, y, x);
  y -= a / b * x;
}
inline int lowbit(int x) { return x & (-x); }
long long x, y, n, m;
long long a[maxn], b[maxn];
int len1, len2;
long long c[maxn];
long long solve() {
  len1 = 0;
  len2 = 0;
  long long temp = 1;
  while (temp <= m) {
    a[len1++] = temp;
    if (temp > m / x) break;
    temp *= x;
  }
  temp = 1;
  while (temp <= m) {
    b[len2++] = temp;
    if (temp > m / y) break;
    temp *= y;
  }
  int len = 0;
  for (int i = 0; i < len1; i++) {
    for (int j = 0; j < len2; j++) {
      if (a[i] + b[j] >= n && a[i] + b[j] <= m) c[len++] = a[i] + b[j];
    }
  }
  sort(c, c + len);
  len = unique(c, c + len) - c;
  long long ans = 0;
  for (int i = 1; i < len; i++) {
    if (c[i] - c[i - 1] - 1 > ans) ans = c[i] - c[i - 1] - 1;
  }
  ans = max(ans, c[0] - n);
  ans = max(ans, m - c[len - 1]);
  if (len == 0) ans = m - n + 1;
  return ans;
}
int main() {
  while (~scanf("%I64d%I64d%I64d%I64d", &x, &y, &n, &m)) {
    cout << solve() << endl;
  }
  return 0;
}
