#include <bits/stdc++.h>
using namespace std;
const long long Lim = 1000000;
long long n, l, r, k;
long long check(long long k) {
  long long ans = -1;
  for (long long i = 0; i <= Lim && i <= n; i++)
    if (k % (n + i) == 0) ans = max(ans, i);
  for (long long i = 1; i <= Lim; i++)
    if (k % i == 0 && k / i <= n + n) ans = max(ans, k / i - n);
  return ans;
}
long long bigeq(long long x, long long y) {
  long long tmp = x / y;
  while (tmp * y < x) tmp++;
  while ((tmp - 1) * y >= x) tmp--;
  return tmp;
}
long long smalleq(long long x, long long y) {
  long long tmp = x / y;
  while (tmp * y > x) tmp--;
  while ((tmp + 1) * y <= x) tmp++;
  return tmp;
}
signed main() {
  scanf("%lld%lld%lld%lld", &n, &l, &r, &k);
  r = (r + n - l) % n + 1;
  l = 1;
  long long ans = -1;
  if (r == n) {
    printf("%lld\n", max(check(k), check(k + 1)));
    return 0;
  }
  for (long long i = 0; i <= Lim && i <= n; i++) {
    long long s = k % (n + i), j = s - r;
    if (0 <= j && j <= i && j <= r && i - j <= n - r) ans = max(ans, i);
    j = s - r;
    if (0 <= j && j < i && j < r && i - j - 1 <= n - r) ans = max(ans, i);
  }
  for (long long t = 0; t <= Lim; t++) {
    long long k = ::k - t * n;
    long long r1 = !t ? (k - r >= 0 ? n : -1) : smalleq(k - r, t);
    long long r2 = smalleq(k + n + 1 - r - r, t + 1);
    long long l1 = !t ? (k - r - r <= 0 ? 0 : n + 1) : bigeq(k - r - r, t);
    long long l2 = bigeq(k - r, t + 1);
    long long ll = max(l1, l2);
    long long rr = min(r1, r2);
    if (ll <= rr) ans = max(ans, rr);
  }
  printf("%lld\n", ans);
  return 0;
}
