#include <bits/stdc++.h>
using namespace std;
long long N, K, L, R;
long long G, A;
long long ans;
void run(int use, long long ntot) {
  if (N <= 500000) {
    for (int nplus = 0; nplus <= N - 1; nplus++) {
      long long rr = (N + nplus + use);
      long long rv = G + (1 + use);
      long long nbefore = ((ntot - rv) % rr + rr) % rr;
      long long nafter = nplus - nbefore;
      if (nbefore >= 0 && nbefore <= G && nafter >= 0 && nafter <= A)
        ans = max(ans, (long long)nplus + use);
    }
  } else {
    if (G + 1 + use <= ntot) {
      long long rv = ntot - G - 1 - use;
      if (rv <= G) ans = max(ans, rv + use + A);
    }
    for (int nrun = 1; nrun * N <= ntot; nrun++) {
      long long nstart = (nrun * (N + use)) + (G + 1 + use);
      long long nleft = ntot - nstart;
      if (nleft >= 0) {
        long long nbefore = nleft % nrun;
        long long nafter = (nleft - ((nrun + 1) * (nbefore))) / nrun;
        if (nafter > A) {
          long long ndrop = (nafter - A + nrun) / (nrun + 1);
          nafter -= ndrop * (nrun + 1);
          nbefore += ndrop * nrun;
        }
        if (nafter >= 0 && nafter <= A && nbefore >= 0 && nbefore <= G)
          ans = max(ans, nbefore + nafter + use);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> N >> L >> R >> K;
  G = (R - L + N) % N;
  A = (N - 1 - G);
  ans = -1;
  for (int i = 0; i <= 1; i++) {
    for (int j = 0; j <= i; j++) {
      run(i, K + j);
    }
  }
  cout << ans << "\n";
}
