#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const double eps = 1e-10;
const double pi = acos(-1.0);
inline long long read() {
  long long x = 0, f = 1;
  char ch;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch & 15);
    ch = getchar();
  }
  if (f)
    return x;
  else
    return -x;
}
long long n, K, l, r, len, ans = -1;
int main() {
  n = read();
  l = read();
  r = read();
  K = read();
  len = (r - l + n) % n + 1;
  if (n < 2000000)
    for (long long i = n; i >= 0; i--) {
      long long y = (K - 1) % (n + i) + 1 - len;
      if (y < 0 || y > i || y > len || len - (y + 1) > n - i) continue;
      if ((y + 1 <= i && y + 1 <= len) || len - y <= n - i) {
        ans = i;
        break;
      }
    }
  else {
    for (long long j = 0; j <= K / n; j++) {
      long long i = (K - 2 * len - (j - 1) * n + 1) / (j + 1);
      long long y = K - j * (n + i) - len;
      if (y < 0 && j) {
        long long dis = (-y - 1) / j + 1;
        y += j * dis;
        i -= dis;
      }
      if (i > n) {
        y += (i - n) * j;
        i = n;
      }
      if (y < 0 || y > i || y > len || len - (y + 1) > n - i) continue;
      if ((y + 1 <= i && y + 1 <= len) || len - y <= n - i) ans = max(ans, i);
    }
  }
  cout << ans << endl;
  return 0;
}
