#include <bits/stdc++.h>
using lint = long long int;
using pii = std::pair<int, int>;
using std::vector;
vector<lint> ve[3];
int main(void) {
  lint x, y, l, r;
  scanf("%lld %lld %lld %lld", &x, &y, &l, &r);
  lint temp_val = 1;
  ve[0].push_back(1);
  while (temp_val <= r / x) {
    temp_val *= x;
    ve[0].push_back(temp_val);
  }
  temp_val = 1;
  ve[1].push_back(1);
  while (temp_val <= r / y) {
    temp_val *= y;
    ve[1].push_back(temp_val);
  }
  for (int i = 0; i < ve[0].size(); i++) {
    for (int j = 0; j < ve[1].size(); j++) {
      ve[2].push_back(ve[0][i] + ve[1][j]);
    }
  }
  sort(ve[2].begin(), ve[2].end());
  lint ans = 0, back = l - 1;
  for (int i = 0; i < ve[2].size() && ve[2][i] <= r; i++) {
    ans = std::max(ans, ve[2][i] - back - 1);
    if (ve[2][i] > back) {
      back = ve[2][i];
    }
  }
  ans = std::max(ans, r - back);
  printf("%lld", ans);
  return 0;
}
