#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long maxN = 300005;
const long long oo = 1e18 + 7;
long long n, a[maxN];
long long x, y, z, k;
const long long lim = 1e6;
long long ans = -1;
bool killed = 0;
bool chk_x(long long vx1, long long vx2) {
  bool rtv;
  vx2 = vx2 - n - vx1;
  if (vx2 > (n - x) || vx1 < 0)
    rtv = 0;
  else
    rtv = 1;
  if (vx1 < 0 || vx2 < 0 || vx1 > x || vx2 > (n - x)) return rtv;
  if ((k - vx1) % (n + vx1 + vx2) == 0) {
    if (killed && vx1 == 0) return 0;
    ans = max(ans, vx1 + vx2);
    return rtv;
  }
  return rtv;
}
void solve_x(long long val) {
  long long rem = k % val;
  chk_x(rem, val);
}
void solve_dumb(long long val) {
  long long l = n;
  long long r = n * 2;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (chk_x(k - val * mid, mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> x >> y >> k;
  if (y < x) y += n;
  x = y - x;
  ++x;
  k -= x;
  if (n <= lim) {
    for (long long i = n; i <= n * 2; ++i) solve_x(i);
  } else {
    for (long long i = 0; i <= lim; ++i) solve_dumb(i);
  }
  ++k;
  killed = 1;
  if (n <= lim) {
    for (long long i = n; i <= n * 2; ++i) solve_x(i);
  } else {
    for (long long i = 0; i <= lim; ++i) solve_dumb(i);
  }
  cout << ans << "\n";
}
