#include <bits/stdc++.h>
using namespace std;
const int tinf = (int)1e9 + 7;
const long long inf = (long long)1e18 + 7;
const int N = 1e5 + 5;
int main() {
  {
    ios ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  long long x, y, l, r;
  cin >> x >> y >> l >> r;
  map<long long, long long> g;
  vector<long long> d;
  long double a1 = 1, b1 = 1;
  while (a1 <= 1e18) {
    b1 = 1;
    while (b1 + a1 <= 1e18) {
      long long t = a1 + b1;
      if (!g[t]) d.push_back(t), g[t] = 1;
      b1 *= y;
    }
    a1 *= x;
  }
  if (!g[l]) g[l] = 2, d.push_back(l);
  if (!g[r]) g[r] = 2, d.push_back(r);
  if (l == r) {
    if (g[l] != 1)
      cout << 1;
    else
      cout << 0;
    return 0;
  }
  sort(d.begin(), d.end());
  long long ans = 0, e = 0, l1 = 0;
  for (int i = 0; i < d.size(); i++)
    if (d[i] == l) {
      e = 1;
      l1 = d[i];
    } else if (e) {
      if (d[i] == r) {
        e = 0;
        if (g[r] == 2) {
          if (g[l1] == 1)
            ans = max(d[i] - l1, ans);
          else
            ans = max(d[i] - l1 + 1, ans);
        }
      }
      if (g[d[i]] == 1) {
        if (g[l1] == 1)
          ans = max(d[i] - l1 - 1, ans);
        else
          ans = max(d[i] - l1, ans);
      }
      l1 = d[i];
    }
  cout << ans;
}
