#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, l, r, k;
  cin >> n >> l >> r >> k;
  long long lim = sqrt(k) + 1;
  if (n <= lim) {
    long long ans = -1;
    long long len;
    if (l <= r)
      len = r - l + 1;
    else
      len = r + n - l + 1;
    for (long long s = 0; s <= n; s++) {
      long long lef = max(0ll, s - (n - len)), rig = min(s, len);
      lef = lef + len, rig = rig + len;
      long long L = (k - rig) / (n + s), R = (k - lef) / (n + s);
      bool bz = 0;
      for (long long all = L; all <= R; all++)
        if ((n + s) * all + lef <= k && (n + s) * all + rig >= k) bz = 1;
      if (bz) ans = s;
    }
    for (long long s = 1; s <= n; s++) {
      long long lef = max(1ll, s - (n - len)) + len, rig = min(s, len) + len;
      long long L = (k + 1 - rig) / (n + s), R = (k + 1 - lef) / (n + s);
      bool bz = 0;
      for (long long all = L; all <= R; all++)
        if ((n + s) * all + lef <= k + 1 && (n + s) * all + rig >= k + 1)
          bz = 1;
      if (bz) ans = max(ans, s);
    }
    cout << ans << endl;
  } else {
    long long ans = -1;
    long long len;
    if (l <= r)
      len = r - l + 1;
    else
      len = r + n - l + 1;
    if (len <= k && len * 2 >= k) ans = max(ans, k - len) + n - len;
    if (len <= k && len * 2 >= k + 1) ans = max(ans, k - len + 1 + n - len);
    for (long long all = 1; all <= lim; all++) {
      long long L = (k - n - len) / all - n, R = (k - len) / all - n;
      if (L < 0) L = 0;
      if (R > n) R = n;
      if (L <= R) {
        if (L < min(n - len, len)) {
          long long l1 = (k - len - n * all) / (all + 1), r1 = R;
          while ((n + l1) * all + len + l1 < k) l1++;
          if (l1 < L) l1 = L;
          if (r1 > min(n - len, len) - 1) r1 = min(n - len, len) - 1;
          if (l1 <= r1) ans = max(ans, r1);
        }
        if (max(len, L) <= min(R, n - len - 1)) {
          long long l1 = (k - len - len) / all - n, r1 = R;
          while ((n + l1) * all + len + len < k) l1++;
          if (l1 < max(len, L)) l1 = max(len, L);
          if (r1 > min(R, n - len - 1)) r1 = min(R, n - len - 1);
          if (l1 <= r1) ans = max(ans, r1);
        }
        if (max(n - len, L) <= min(len - 1, R)) {
          long long l1 = (k - len - n * all) / (all + 1),
                    r1 = (k - len + (n - len) - n * all) / (all + 1);
          while ((n + l1) * all + len + l1 < k) l1++;
          if (l1 < max(n - len, L)) l1 = max(n - len, L);
          if (r1 > min(len - 1, R)) r1 = min(len - 1, R);
          if (l1 <= r1) ans = max(ans, r1);
        }
        if (max(max(n - len, len), L) <= R) {
          long long l1 = (k - len - len) / all - n,
                    r1 = (k - len + (n - len) - n * all) / (all + 1);
          while ((n + l1) * all + len + len < k) l1++;
          if (l1 < max(max(n - len, len), L)) l1 = max(max(n - len, len), L);
          if (r1 > R) r1 = R;
          if (l1 <= r1) ans = max(ans, r1);
        }
      }
    }
    k++;
    for (long long all = 1; all <= lim; all++) {
      long long L = (k - n - len) / all - n, R = (k - len - 1) / all - n;
      if (L < 1) L = 1;
      if (R > n) R = n;
      if (L <= R) {
        if (L < min(n - len, len - 1)) {
          long long l1 = (k - len - n * all) / (all + 1), r1 = R;
          while ((n + l1) * all + len + l1 < k) l1++;
          if (l1 < L) l1 = L;
          if (r1 > min(n - len, len - 1)) r1 = min(n - len, len - 1);
          if (l1 <= r1) ans = max(ans, r1);
        }
        if (max(len, L) <= min(R, n - len)) {
          long long l1 = (k - len - len) / all - n, r1 = R;
          while ((n + l1) * all + len + len < k) l1++;
          if (l1 < max(len, L)) l1 = max(len, L);
          if (r1 > min(R, n - len)) r1 = min(R, n - len);
          if (l1 <= r1) ans = max(ans, r1);
        }
        if (max(n - len + 1, L) <= min(len - 1, R)) {
          long long l1 = (k - len - n * all) / (all + 1),
                    r1 = (k - len + (n - len) - n * all) / (all + 1);
          while ((n + l1) * all + len + l1 < k) l1++;
          if (l1 < max(n - len + 1, L)) l1 = max(n - len + 1, L);
          if (r1 > min(len - 1, R)) r1 = min(len - 1, R);
          if (l1 <= r1) ans = max(ans, r1);
        }
        if (max(max(n - len + 1, len), L) <= R) {
          long long l1 = (k - len - len) / all - n,
                    r1 = (k - len + (n - len) - n * all) / (all + 1);
          while ((n + l1) * all + len + len < k) l1++;
          if (l1 < max(max(n - len + 1, len), L))
            l1 = max(max(n - len + 1, len), L);
          if (r1 > R) r1 = R;
          if (l1 <= r1) ans = max(ans, r1);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
