#include <bits/stdc++.h>
using namespace std;
long long l, r, x, y, LNG = 1000000000000000000;
vector<long long> v;
long long p(int a, int b) {
  long long ret = 0, s1 = 1ll, s2 = 1ll;
  int i;
  for (i = 1; i <= a && (s1 <= LNG / x); ++i) {
    s1 *= x;
  }
  if (i != a + 1) return -1;
  for (i = 1; i <= b && (s2 <= LNG / y); ++i) {
    s2 *= y;
  }
  if (i != b + 1) return -1;
  ret = s1 + s2;
  return ret;
}
int main() {
  cin >> x >> y >> l >> r;
  for (int i = 0; i <= 70; ++i)
    for (int j = 0; j <= 70; ++j) {
      long long ret = p(i, j);
      if (ret >= l && ret <= r) v.push_back(ret);
    }
  sort(v.begin(), v.end());
  long long mx = 0;
  if (v.size() == 0) {
    cout << r - l + 1 << "\n\n";
    return 0;
  }
  if (v.size() && v[0] != l) {
    mx = 1ll;
    mx = max(mx, v[0] - l);
    v.push_back(l);
  }
  sort(v.begin(), v.end());
  if (v.size() && v[v.size() - 1] != r) {
    mx = max(mx, 1ll);
    mx = max(mx, r - v[v.size() - 1]);
    v.push_back(r);
  }
  sort(v.begin(), v.end());
  for (int i = 1; i < v.size(); ++i) {
    mx = max(mx, v[i] - v[i - 1] - 1);
  }
  cout << mx << "\n";
}
