#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const int mod = (int)1e9 + 7;
const double PI = acos(-1.0);
long long x, y, l, r;
vector<long long> a, b, c;
int main() {
  cin >> x >> y >> l >> r;
  long long v;
  v = 1;
  while (true) {
    a.push_back(v);
    if (log(v) + log(x) <= log(r))
      v *= x;
    else
      break;
  }
  v = 1;
  while (true) {
    b.push_back(v);
    if (log(v) + log(y) <= log(r))
      v *= y;
    else
      break;
  }
  for (int i = 0; i < (int)a.size(); i++)
    for (int j = 0; j < (int)b.size(); j++) {
      if (a[i] + b[j] <= r && a[i] + b[j] >= l) c.push_back(a[i] + b[j]);
    }
  c.push_back(l - 1);
  c.push_back(r + 1);
  sort(c.begin(), c.end());
  long long ans = 0;
  for (int i = 0; i < (int)c.size() - 1; i++) {
    ans = max(ans, c[i + 1] - c[i] - 1);
  }
  cout << ans << endl;
  return 0;
}
