#include <bits/stdc++.h>
using namespace std;
long long N, K;
long long L, R, ans = -1, ind;
long long Dist(long long x, long long y) {
  if (x <= y) return y - x + 1;
  return N - x + 1 + y;
}
void firstCase() {
  long long dist = Dist(L, R);
  if (K < dist) return;
  for (int p = 1;; p++) {
    if (p == 78) {
      int a;
      a = 0;
    }
    long long left = K - dist - N * p;
    if (left < 0) break;
    long long X = left / p, X2 = left % p;
    if (dist - X2 < 0 || X - X2 < 0) continue;
    long long l1 = X - N, r1 = min((dist - X2) / (p), X);
    long long l2 = (X + dist - N - X2) / (p + 1);
    if (((X + dist - N - X2) % (p + 1)) != 0) l2++;
    if ((X + dist - N - X2) < 0) l2 = 0;
    if (ind == 1 && X2 == 0) l1 = max(l1, 1LL), l2 = max(l2, 1LL);
    l1 = max(l1, 0LL);
    l2 = max(l2, 0LL);
    long long r2 = (X - X2) / (p + 1);
    if (l1 > r2 || l2 > r1 || l2 > r2 || l1 > r1) continue;
    if (l1 > l2) {
      swap(l1, l2);
      swap(r1, r2);
    }
    long long q = max(l1, l2);
    ans = max(ans, X - q);
  }
  long long X2 = K - dist;
  if (X2 <= dist) ans = max(ans, X2 + N - dist);
}
void secondCase() {
  long long dist = Dist(L, R);
  if (K < dist) return;
  for (long long x = N; x >= 0; x--) {
    long long p = (K - dist) / (x + N);
    long long x2 = (K - dist) % (x + N);
    if (x2 <= dist && x2 <= x && N - x >= dist - x2) {
      if (ind == 1 && x2 == 0) continue;
      ans = max(ans, x);
      break;
    }
  }
}
int main() {
  cin >> N >> L >> R >> K;
  ind = 0;
  if (N > K / N)
    firstCase();
  else
    secondCase();
  ++K;
  ind = 1;
  if (N > K / N)
    firstCase();
  else
    secondCase();
  cout << ans << '\n';
  return 0;
}
