#include <bits/stdc++.h>
using namespace std;
const int Maxk = 1005;
string mul(string a, string b) {
  int res[Maxk];
  memset(res, 0, sizeof(res));
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  for (int i = 0; i < a.length(); i++) {
    for (int j = 0; j < b.length(); j++) {
      res[i + j] += (a[i] - '0') * (b[j] - '0');
    }
  }
  for (int i = 0; i < Maxk; i++) {
    if (res[i] >= 10) {
      res[i + 1] += (res[i] / 10);
      res[i] %= 10;
    }
  }
  string ret;
  bool flag = false;
  for (int i = Maxk - 1; i >= 0; i--) {
    if (flag || res[i]) {
      flag = true;
      ret = ret + (char)(res[i] + '0');
    }
  }
  if (ret == "") ret = "0";
  return ret;
}
string add(string a, string b) {
  if (a.length() < b.length()) swap(a, b);
  while (a.length() != b.length()) {
    b = '0' + b;
  }
  for (int i = a.length() - 1; i >= 0; i--) {
    a[i] += (b[i] - '0');
    if (a[i] > '9' && i) {
      a[i] -= 10;
      a[i - 1]++;
    }
  }
  if (a[0] > '9') {
    a[0] -= 10;
    a = '1' + a;
  }
  return a;
}
long long M = 3e18;
vector<long long> v1, v2, v3;
int main() {
  long long x, y, l, r;
  scanf("%I64d %I64d %I64d %I64d", &x, &y, &l, &r);
  long long now = 1;
  for (;;) {
    v1.push_back(now);
    if (M / x > now) {
      now *= x;
    } else
      break;
  }
  now = 1;
  for (;;) {
    v2.push_back(now);
    if (M / y > now) {
      now *= y;
    } else
      break;
  }
  for (int i = 0; i < v1.size(); i++) {
    for (int j = 0; j < v2.size(); j++) {
      v3.push_back(v1[i] + v2[j]);
    }
  }
  v3.push_back(l - 1);
  v3.push_back(r + 1);
  sort(v3.begin(), v3.end());
  long long anss = 0LL;
  for (int i = 0; i < v3.size() - 1; i++) {
    if (v3[i] >= l - 1 && v3[i + 1] <= r + 1)
      anss = max(anss, v3[i + 1] - v3[i] - 1);
  }
  printf("%I64d\n", anss);
  return 0;
}
