#include <bits/stdc++.h>
using namespace std;
long long n, pos, tot;
long long getr(long long l, long long r) {
  long long slo = tot / l, res, mid;
  if (l > r) return 0;
  if (tot - slo * l < pos + max(0ll, l + pos - 2ll * n - 1)) return 0;
  res = l;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (tot - slo * mid >= pos + max(0ll, mid + pos - 2ll * n - 1)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return res;
}
int main() {
  long long i, j, l, r, ans, res, last;
  scanf("%lld%lld%lld%lld", &n, &l, &r, &tot);
  pos = r - l + 1;
  if (pos <= 0) pos += n;
  if (pos == n) {
    ans = -1;
    if (tot % n == 0) ans = 0;
    for (i = n; i <= tot; i = last + 1) {
      last = tot / (tot / i);
      if ((tot % last == 0) && (last - n <= n)) ans = last - n;
    }
    tot++;
    for (i = n; i <= tot; i = last + 1) {
      last = tot / (tot / i);
      if ((tot % last == 0) && (last - n <= n)) ans = max(ans, last - n);
    }
    if (tot - n <= n) ans = max(ans, tot - n);
    printf("%lld\n", ans);
    return 0;
  }
  ans = -1;
  for (i = n; i <= tot; i = last + 1) {
    last = tot / (tot / i);
    res = getr(i, last);
    if (res) {
      if (tot % res <= min(2ll * pos, pos + res - n)) ans = res - n;
    }
  }
  res = getr(tot + 1, 2ll * n);
  if (res) {
    if (min(2ll * pos, pos + (res - n)) >= tot) ans = res - n;
  }
  printf("%lld\n", ans);
  return 0;
}
