#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  long long i = 1, j = 1, res_x = 1, res_y = 1, arr_x[60], arr_y[60], limit_x,
            limit_y;
  arr_x[0] = 1;
  arr_y[0] = 1;
  while (res_x < 1e18 / x) {
    res_x = res_x * x;
    arr_x[i++] = res_x;
  }
  limit_x = i - 1;
  while (res_y < 1e18 / y) {
    res_y = res_y * y;
    arr_y[j++] = res_y;
  }
  limit_y = j - 1;
  long long array1[3600], k = 0;
  for (i = 0; i <= limit_x; i++) {
    for (j = 0; j <= limit_y; j++) {
      array1[k++] = arr_x[i] + arr_y[j];
    }
  }
  sort(array1, array1 + k);
  array1[k] = 1e19;
  long long fin = 0, flagl = 0, flagr = 0;
  for (i = 0; i <= k; i++) {
    if (array1[i] >= l && array1[i] <= r) {
      if (flagl == 0) {
        fin = array1[i] - l;
        flagl = 1;
      } else {
        if (array1[i] - array1[i - 1] > fin) {
          fin = array1[i] - array1[i - 1] - 1;
        }
      }
    }
    if (array1[i] > r && flagr == 0) {
      if (flagl == 0)
        fin = (r - l) + 1;
      else {
        if (r - array1[i - 1] > fin) fin = r - array1[i - 1];
      }
      flagr = 1;
      break;
    }
  }
  cout << fin;
  return 0;
}
