#include <bits/stdc++.h>
using namespace std;
const long long X = 2e6 + 5;
int main() {
  long long n, l, r, k;
  cin >> n >> l >> r >> k;
  long long d = (r - l + n) % n + 1;
  long long ans = -1;
  if (n < X) {
    for (long long p = n; p >= 0; p--) {
      long long y = (k - 1) % (n + p) + 1 - d;
      if (y < 0 || y > p || y > d || d - (y + 1) > n - p) continue;
      long long y2 = y + 1;
      if (y2 <= p && y2 <= d) {
        ans = max(ans, p);
      }
      if (d - y <= n - p) {
        ans = max(ans, p);
      }
    }
  } else {
    for (long long i = 0; i <= k / n; i++) {
      long long p = (k - 2 * d - (i - 1) * n + 1) / (i + 1);
      long long y = k - i * (n + p) - d;
      if (y < 0) {
        if (i == 0) continue;
        long long dis = (-y - 1) / i + 1;
        y += i * dis;
        p -= dis;
      }
      if (p > n) {
        y += (p - n) * i;
        p == n;
      }
      if (y < 0 || y > p || y > d || d - (y + 1) > n - p) continue;
      long long y2 = y + 1;
      if (y2 <= p && y2 <= d) {
        ans = max(ans, p);
      }
      if (d - y <= n - p) {
        ans = max(ans, p);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
