#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void ckmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
inline void ckmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
long long solve0(int n, int c, int d) {
  int ret = -1;
  int a[32];
  for (int set = 0; set < (1 << n); ++set) {
    for (int i = 0; i < (n); ++i) a[i] = 1 + (int)((((set) & (1 << (i))) != 0));
    int p = 0;
    for (int w = c; w > a[p]; p = (p + 1) % n) w -= a[p];
    if (p == d) {
      int c = 0;
      for (int i = 0; i < (n); ++i)
        if ((((set) & (1 << (i))) != 0)) ++c;
      ckmax(ret, c);
    }
  }
  return ret;
}
long long solve1(long long n, long long c, long long d) {
  long long ret = -1;
  for (int bit = 1; bit <= 2; bit++)
    for (int actual = 1; actual <= bit; actual++)
      if (actual <= c)
        for (long long m = n; m <= n + n; m++) {
          long long z = (c - actual) % m;
          if (z < d || z > d + d) continue;
          if (z + bit + (n - d - 1) > m || z + bit + (n - d - 1) * 2 < m)
            continue;
          ckmax(ret, m - n);
        }
  return ret;
}
long long solve2(long long n, long long c, long long d) {
  long long ret = -1;
  for (int bit = 1; bit <= 2; bit++)
    for (int actual = 1; actual <= bit; actual++)
      if (actual <= c) {
        if (c - actual >= d && c - actual <= d + d)
          ckmax(ret, c - actual + bit + (n - d - 1) * 2 - n);
        for (long long cnt = 1; cnt <= (c - actual) / n; cnt++) {
          long long t = (c - actual) / cnt;
          long long s = (c - actual) / (cnt + 1) + 1;
          ckmax(s, n);
          ckmin(t, n + n);
          if (s > t) continue;
          ckmin(t, (c - actual - d) / cnt);
          ckmax(s, (c - actual - d - d + cnt - 1) / cnt);
          if (s > t) continue;
          ckmin(t, (c - actual + bit + 2 * (n - d - 1)) / (cnt + 1));
          ckmax(s, (c - actual + bit + (n - d - 1) + cnt) / (cnt + 1));
          if (s > t) continue;
          ckmax(ret, t - n);
        }
      }
  return ret;
}
int main() {
  std::ios::sync_with_stdio(false);
  long long n, k, l, r;
  while (cin >> n >> l >> r >> k) {
    long long d = (r - l + n) % n;
    long long ret;
    if (n <= 1000000)
      ret = solve1(n, k, d);
    else
      ret = solve2(n, k, d);
    cout << ret << endl;
    break;
  }
  return 0;
}
