#include <bits/stdc++.h>
using namespace std;
long long n, k, l, r, m, x, y;
long long max(long long a, long long c) {
  if (a > c)
    return a;
  else
    return c;
}
int main() {
  cin >> n >> l >> r >> k;
  r = (r - l + 1 + n) % n;
  long long ans = -1;
  if (n < 1e7) {
    for (x = 0; x <= n; x++) {
      if ((k % (n + x)) - r <= min(r, x) &&
          (k % (n + x) - r >= max(0, x - 1 - (n - r))))
        ans = x;
    }
  } else {
    for (m = (k - r) / (2 * n); m * n <= k; m++) {
      long long a = 0, kmnr = k - m * n - r, b = n;
      while (a <= b) {
        x = (a + b) / 2;
        y = kmnr - m * x;
        if (y > min(r, x)) {
          a = x + 1;
        } else if (y < max(0, x - (n - r) - 1)) {
          b = x - 1;
        } else {
          ans = max(ans, x);
          a = x + 1;
        }
      }
    }
  }
  if (r == 0) r = n;
  if (n < 1e7) {
    for (x = 0; x <= n; x++) {
      if ((k % (n + x)) - r <= min(r, x) &&
          (k % (n + x) - r >= max(0, x - 1 - (n - r))))
        ans = max(x, ans);
    }
  } else {
    for (m = (k - r) / (2 * n); m * n <= k; m++) {
      long long a = 0, kmnr = k - m * n - r, b = n;
      while (a <= b) {
        x = (a + b) / 2;
        y = kmnr - m * x;
        if (y > min(r, x)) {
          a = x + 1;
        } else if (y < max(0, x - (n - r) - 1)) {
          b = x - 1;
        } else {
          ans = max(ans, x);
          a = x + 1;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
