#include <bits/stdc++.h>
using namespace std;
long long n, l, r, K, p;
bool check(long long x) {
  long long t = K % (n + x);
  if (t == 0) t = n + x;
  if (t > min(p * 2, p + x)) return 0;
  if (t < p + max(0LL, x - n + p - 1)) return 0;
  return 1;
}
signed main() {
  scanf("%lld%lld%lld%lld", &n, &l, &r, &K);
  p = (r - l + 1 + n) % n;
  if (p > K) {
    return puts("-1"), 0;
  }
  if (p == 0) p += n;
  if (n <= 100000) {
    for (long long i = n; i >= 0; i--)
      if (check(i)) {
        printf("%lld\n", i);
        return 0;
      }
    puts("-1");
    return 0;
  }
  long long ans = 0;
  for (long long t = ceil((double)(K - 2 * p) / (double)(2 * n));
       t * n + p <= K; t++) {
    long long y = K - n * t - p;
    long long x;
    if (t != 0)
      x = min((long long)floor((double)(n - p + y) / (1 + t)),
              (long long)y / t);
    else {
      x = (K - p + 1) + (n - p);
    }
    y = K - n * t - p - t * x;
    if (y < 0 || y > p || y > x || x > n) continue;
    ans = max(ans, x);
    y = K - n * t - p + 1;
    if (t != 0)
      x = min((long long)floor((double)(n - p + y) / (1 + t)),
              (long long)y / t);
    else {
      x = (K - p + 1) + (n - p);
    }
    y = K - n * t - p - t * x;
    if (y < 0 || y > p || y > x || x > n) continue;
    ans = max(ans, x);
  }
  printf("%lld\n", ans);
  return 0;
}
