#include <bits/stdc++.h>
using namespace std;
long long solve(long long n, long long r, long long k, long long mina) {
  if (k < r) return -1;
  long long ans = -1;
  if (n > k / n / n) {
    if (k <= 2 * r) ans = max(ans, k - r + n - r);
    for (long long x = max((k - r * 2 - 1) / (2 * n) + 1, 1LL);
         x <= (k - r) / n; x++) {
      long long a = (k - r) % x;
      if (a < mina) a += x;
      long long b = (k - r - a) / x - n - a;
      if (b < 0) continue;
      if (b > n - r) {
        long long t = (b - n + r - 1) / (x + 1) + 1;
        a += t * x;
        b -= t * (x + 1);
      }
      if (a <= r && b >= 0) ans = max(ans, a + b);
    }
  } else {
    for (long long a = mina; a <= r; a++)
      for (long long b = 0; b <= n - r; b++)
        if ((k - r - a) % (n + a + b) == 0) ans = max(ans, a + b);
  }
  return ans;
}
int main() {
  long long n, l, r, k;
  scanf("%I64d%I64d%I64d%I64d", &n, &l, &r, &k);
  r = (r - l + n) % n + 1;
  printf("%I64d\n", max(solve(n, r, k + 1, 1), solve(n, r, k, 0)));
  return 0;
}
