#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, l, r;
  scanf("%lld %lld %lld %lld", &x, &y, &l, &r);
  vector<long long> v;
  long long mini = min(x, y);
  y = max(x, y), x = mini;
  long long p1 = 1, p2 = 1, flagl = 0, flagr = 0;
  while (1) {
    p2 = 1;
    while (1) {
      long long val = p1 + p2;
      if (l <= val && val <= r) {
        if (l == val) flagl = 1;
        if (r == val) flagr = 1;
        v.push_back(val);
      }
      if (val > r) break;
      if (p2 <= r / y)
        p2 = p2 * y;
      else
        break;
    }
    if (p1 <= r / x)
      p1 = p1 * x;
    else
      break;
  }
  if (!flagl) v.push_back(l - 1);
  if (!flagr) v.push_back(r + 1);
  sort(v.begin(), v.end());
  long long diff = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    diff = max(diff, v[i + 1] - v[i]);
  }
  printf("%lld\n", max(diff - 1, 0LL));
  return 0;
}
