#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void in(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) f |= (c == '-'), c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar();
  f ? x = -x : 0;
}
template <class T>
inline void out(T x, const char c = '\n') {
  static short st[30];
  short m = 0;
  if (x < 0) putchar('-'), x = -x;
  do st[++m] = x % 10, x /= 10;
  while (x);
  while (m) putchar(st[m--] | '0');
  putchar(c);
}
template <class T>
inline void err(const T &x, const char c = '\n') {
  cerr << x << c;
}
template <class T, class... Args>
inline void in(T &x, Args &...args) {
  in(x);
  in(args...);
}
template <class T, class... Args>
inline void out(const T &x, const Args &...args) {
  out(x, ' ');
  out(args...);
}
template <class T, class... Args>
inline void err(const T &x, const Args &...args) {
  err(x, ' ');
  err(args...);
}
template <class T>
inline void prt(T a[], int n) {
  for (register int i = 0; i < n; ++i) out(a[i], i == n - 1 ? '\n' : ' ');
}
template <class T>
inline void clr(T a[], int n) {
  memset(a, 0, sizeof(T) * n);
}
template <class T>
inline void clr(T *a, T *b) {
  memset(a, 0, sizeof(T) * (b - a));
}
template <class T>
inline bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
inline bool ckmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
namespace MOD_CALC {
const int md = 66662333, inv2 = (md + 1) / 2;
inline int add(const int a, const int b) {
  return a + b >= md ? a + b - md : a + b;
}
inline int sub(const int a, const int b) {
  return a - b < 0 ? a - b + md : a - b;
}
inline int mul(const int a, const int b) { return (long long)a * b % md; }
inline void inc(int &a, const int b) { (a += b) >= md ? a -= md : 0; }
inline void dec(int &a, const int b) { (a -= b) < 0 ? a += md : 0; }
inline int qpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1) r = mul(r, a);
  return r;
}
inline int qpow(int a, int b, const int p) {
  int r = 1;
  for (; b; b >>= 1, a = (long long)a * a % p)
    if (b & 1) r = (long long)r * a % p;
  return r;
}
inline int mdinv(const int a) { return qpow(a, md - 2); }
template <class... Args>
inline int add(const int a, const int b, const Args &...args) {
  return add(add(a, b), args...);
}
template <class... Args>
inline int mul(const int a, const int b, const Args &...args) {
  return mul(mul(a, b), args...);
}
}  // namespace MOD_CALC
using namespace MOD_CALC;
namespace i207M {
long long n, l, r, len, k;
inline void gun(long long ans) {
  out(ans);
  exit(0);
}
signed main() {
  in(n, l, r, k);
  len = (l <= r ? r - l + 1 : n - (l - r) + 1);
  if (k < len) gun(-1);
  if (n <= 1000000) {
    for (long long a = n; a >= 1; --a) {
      long long r = k % (n + a);
      if (!r) {
        if (len == n) gun(a);
      } else {
        long long b = r - len;
        if (b >= 0 && b <= len && b >= a - (n - len) && b <= a) gun(a);
      }
      r = (k + 1) % (n + a);
      if (!r) {
        if (len == n) gun(a);
      } else {
        long long b = r - len;
        if (b > 0 && b <= len && b >= a - (n - len) && b <= a) gun(a);
      }
    }
    if (k % n == len) gun(0);
    gun(-1);
  } else {
    long long ans = -1;
    if (k + 1 <= 2 * len)
      ckmax(ans, k + 1 - len + n - len);
    else if (k <= 2 * len)
      ckmax(ans, k - len + n - len);
    for (long long c = 1; len + c * n <= k; ++c) {
      long long t = k - len - c * n, a = t / c, b = t - a * c,
                d = max(a - n, 0ll);
      a -= d, b += d * c;
      if (a > b + n - len) {
        long long w = (a - b - n + len + c) / (c + 1);
        a -= w, b += w * c;
      }
      if (b <= a && b <= len && a >= 0) ckmax(ans, a);
    }
    for (long long c = 1; len + c * n <= k + 1; ++c) {
      long long t = k + 1 - len - c * n, a = t / c, b = t - a * c,
                d = max(a - n, 0ll);
      if (b == 0) ckmax(d, 1ll);
      a -= d, b += d * c;
      if (a > b + n - len) {
        long long w = (a - b - n + len + c) / (c + 1);
        a -= w, b += w * c;
      }
      if (b > 0 && b <= a && b <= len && a >= 0) ckmax(ans, a);
    }
    gun(ans);
  }
  return 0;
}
}  // namespace i207M
signed main() {
  i207M::main();
  return 0;
}
