#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    if (0) {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
template <typename T>
inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a) l = r;
  return a;
}
template <typename T>
inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a) l = r;
  return a;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v) is >> it;
  return is;
}
class range {
 private:
  struct I {
    int x;
    int operator*() { return x; }
    bool operator!=(I &lhs) { return x < lhs.x; }
    void operator++() { ++x; }
  };
  I i, n;

 public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  I &begin() { return i; }
  I &end() { return n; }
};
long long calc_rp(long long lp, long long L, long long k, long long l,
                  long long r, long long n) {
  return (k - (l + lp)) / L - (n + lp);
}
bool isless(long long lp, long long rp, long long L, long long n, long long l,
            long long r) {
  if (lp < 0) return true;
  if (rp > r) return true;
  return false;
}
bool isok(long long lp, long long rp, long long L, long long n, long long l,
          long long r, long long k) {
  return (0 <= lp && lp <= l) && (0 <= rp && rp <= r) &&
         ((n + lp + rp) * L + (l + lp) == k);
}
long long solve(long long L, long long k, long long l, long long r,
                long long n) {
  long long lp_min = (k - l) % L;
  if (lp_min < 0) lp_min += L;
  long long rp_max = calc_rp(lp_min, L, k, l, r, n);
  if (!isless(lp_min, rp_max, L, n, l, r)) {
    if (isok(lp_min, rp_max, L, n, l, r, k)) {
      return lp_min + rp_max;
    }
  } else {
    long long lb = 0;
    long long ub = l + 10;
    while (ub - lb > 1) {
      const int mid = (lb + ub) / 2;
      if (isless(lp_min + L * mid, rp_max - (L + 1) * mid, L, n, l, r)) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    {
      long long lp = lp_min + L * ub;
      long long rp = rp_max - (L + 1) * ub;
      if (isok(lp, rp, L, n, l, r, k)) {
        return lp + rp;
      }
    }
  }
  return -2;
}
int main() {
  long long n, l, r, k;
  cin >> n >> l >> r >> k;
  l--;
  r += n - l;
  r %= n;
  long long ret = -1;
  if (n <= 1123456) {
    for (int _s : range(n + 1)) {
      long long s = _s;
      {
        long long L = k / (n + s);
        long long a = k - L * (n + s) - r;
        long long b = s - a;
        if (isok(a, b, L, n, r, n - r, k)) {
          chmax(ret, s);
        }
      }
      if (r > 0) {
        long long L = k / (n + s);
        long long a = (k + 1) - L * (n + s) - 2 - (r - 1);
        long long b = (s - 1 - a);
        if (isok(a, b, L, n - 1, r - 1, n - r, k + 1 - 2 * (L + 1))) {
          chmax(ret, s);
        }
      }
    }
  } else {
    if (isok(k - r, n - r, 0, n, r, n - r, k)) {
      chmax(ret, (k - r) + (n - r));
    }
    if (isok(k - r, n - r, 0, n - 1, r - 1, n - r, k - 1)) {
      if (r > 0) chmax(ret, (k - r) + (n - r) + 1);
    }
    for (long long L = 1; L * n <= k; L++) {
      chmax(ret, solve(L, k, r, n - r, n));
      if (r > 0)
        chmax(ret, solve(L, k + 1 - 2 * (L + 1), r - 1, n - r, n - 1) + 1);
      if (L == 1 && r == 0) {
        chmax(ret, solve(L, k + 1 - 2, r, n - r - 1, n - 1) + 1);
      }
    }
  }
  cout << ret << endl;
  return 0;
}
