#include <bits/stdc++.h>
using namespace std;
const long long N = 1e11 + 7;
const long long oo = 1e18 + 7, mod = 1e9 + 7;
long long n, l, r, k;
void process() {
  cin >> n >> l >> r >> k;
  long long ans = -1, len = 0;
  if (r >= l)
    len = (r - l + 1);
  else
    len = n - (l - r - 1);
  if (k < len) {
    cout << -1;
    return;
  }
  if (n >= 1000000) {
    if (len * 2 >= k) {
      ans = n - (len * 2 - k);
    }
    for (long long b = 1; b <= (k / n) + 5; b++) {
      long long remaining = k - len - b * n;
      if (remaining < 0) break;
      long long c, a;
      l = 0, r = min(remaining / b, n);
      while (l < r) {
        long long mid = (l + r + 1) >> 1;
        long long c = mid, a = remaining - b * c;
        if ((c - a) > (n - len))
          r = mid - 1;
        else
          l = mid;
      }
      c = l, a = remaining - b * c;
      if (a > min(c, len)) continue;
      if ((c - a) > (n - len)) continue;
      ans = max(ans, c);
    }
    k--;
    len--;
    if (len * 2 >= k) {
      ans = max(ans, n - (len * 2 - k));
    }
    for (long long b = 1; b <= (k / n) + 5; b++) {
      long long remaining = k - len - b * n;
      if (remaining < 0) break;
      long long c, a;
      l = 0, r = min(remaining / b, n);
      while (l < r) {
        long long mid = (l + r + 1) >> 1;
        long long c = mid, a = remaining - b * c;
        if ((c - a) > (n - len))
          r = mid - 1;
        else
          l = mid;
      }
      c = l, a = remaining - b * c;
      if (a >= min(c, len + 1)) continue;
      if ((c - a) > (n - len)) continue;
      ans = max(ans, c);
    }
    cout << ans;
  } else {
    for (long long c = 0; c <= n; c++) {
      long long remaining = k - len;
      long long a = remaining % (n + c);
      if (a > min(len, c)) continue;
      if ((n - len) < (c - a)) continue;
      ans = max(ans, c);
    }
    k--;
    len--;
    for (long long c = 0; c <= n; c++) {
      long long remaining = k - len;
      long long a = remaining % (n + c);
      if (a >= min(len + 1, c)) continue;
      if ((n - len) < (c - a)) continue;
      ans = max(ans, c);
    }
    cout << ans;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  process();
}
