#include <bits/stdc++.h>
using namespace std;
long long n, k, p;
int main() {
  long long l, r, i, j, b, ans;
  cin >> n >> l >> r >> k;
  p = (r - l + n) % n + 1;
  if (k - p < 0) {
    cout << -1 << endl;
    return 0;
  }
  if (k - p == 0) {
    cout << n - p + 1 << endl;
    return 0;
  }
  if (k - p <= p) {
    cout << min(n, n + k - 2ll * p + 1) << endl;
    return 0;
  }
  if (n <= 4e7) {
    ans = -1;
    for (i = n; i >= 0; i--) {
      long long a = (k - p) % (n + i);
      if (a >= max(0ll, i - n + p - 1) && a <= min(i, p)) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
    return 0;
  }
  ans = -1;
  for (b = max(1ll, (k - p) / (2ll * n)); b <= (k - p) / n; b++) {
    long long x = k - p - n * b;
    if (p <= n - p + 1) {
      long long aa;
      aa = min(p, x / b);
      if (aa >= x / (b + 1)) ans = max(ans, aa);
      aa = min(n - p + 1, x / b);
      if (aa >= p && aa >= (x - p) / b) ans = max(ans, aa);
      aa = min(n, (x + n - p + 1) / (b + 1));
      if (aa >= n - p + 1 && aa >= (x - p) / b) ans = max(ans, aa);
    } else {
      long long aa;
      aa = min(n - p + 1, x / b);
      if (aa >= x / (b + 1)) ans = max(ans, aa);
      aa = min(p, (x + n - p + 1) / (b + 1));
      if (aa >= n - p + 1 && aa >= x / (b + 1)) ans = max(ans, aa);
      aa = min(n, (x + n - p + 1) / (b + 1));
      if (aa >= p && aa >= (x - p) / b) ans = max(ans, aa);
    }
  }
  cout << ans << endl;
  return 0;
}
