#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline ll ceil(ll a, ll b) { return a % b == 0 ? a / b : a / b + 1; }
ll solve1(ll n, ll r, ll k) {
  if (n <= 1e6) {
    for (ll s = n; s >= 0; --s) {
      ll p = (k - r) % (n + s);
      if (p >= 0 && p >= s - n + r && p <= r && p <= s) return s;
    }
    return -1;
  }
  ll ans = -1;
  if (k <= r * 2) {
    ans = k - r + n - r;
  }
  for (ll t = 1; t * n <= k; ++t) {
    if (t * 2 * n + 2 * n < k) continue;
    ll lo, hi;
    lo = max(ceil(k - n * t - 2 * r, t), ceil(k - n * t - r, t + 1));
    hi = min((k - n * t - r) / t, (k - (t - 1) * n - 2 * r) / (t + 1));
    if (lo <= hi) {
      ans = max(ans, hi);
    }
  }
  return ans;
}
ll solve2(ll n, ll r, ll k) {
  if (n <= 1e6) {
    for (ll s = n; s >= 1; --s) {
      ll p = (k + 1 - r) % (n + s);
      if (p >= 1 && p >= s - n + r && p <= r && p <= s) return s;
    }
    return -1;
  }
  ll ans = -1;
  if (k <= r * 2) {
    ans = k - r + n - r + 1;
  }
  for (ll t = 1; t * n <= k; ++t) {
    if (t * 2 * n + 2 * n < k) continue;
    ll lo, hi;
    lo = max(ceil(k + 1 - n * t - 2 * r, t), ceil(k + 1 - n * t - r, t + 1));
    hi = min((k + 1 - n * t - r - 1) / t,
             (k + 1 - (t - 1) * n - 2 * r) / (t + 1));
    if (lo <= hi) {
      ans = max(ans, hi);
    }
  }
  return ans;
}
int main() {
  ll n, l, r, k;
  cin >> n >> l >> r >> k;
  r -= l - 1;
  if (r <= 0) r += n;
  l = 1;
  if (k < r) {
    printf("-1\n");
    return 0;
  }
  printf("%lld\n", max(solve1(n, r, k), solve2(n, r, k)));
}
