#include <bits/stdc++.h>
using namespace std;
namespace io {
template <typename T>
void println(const T &t) {
  cout << t << '\n';
}
template <typename T, typename... Args>
void println(const T &t, const Args &...rest) {
  cout << t << ' ';
  println(rest...);
}
template <typename T>
void print(const T &t) {
  cout << t << ' ';
}
template <typename T, typename... Args>
void print(const T &t, const Args &...rest) {
  cout << t;
  print(rest...);
}
template <class T>
void scan(T &t) {
  cin >> t;
}
template <class T, class... Args>
void scan(T &a, Args &...rest) {
  cin >> a;
  scan(rest...);
}
}  // namespace io
using namespace io;
using ll = long long;
ll ple(ll fz, ll fm) { return fz / fm; }
ll pge(ll fz, ll fm) { return (fz + fm - 1) / fm; }
ll le(ll fz, ll fm) { return fz >= 0 ? ple(fz, fm) : -pge(-fz, fm); }
ll ge(ll fz, ll fm) { return fz >= 0 ? pge(fz, fm) : -ple(-fz, fm); }
int main() {
  ios::sync_with_stdio(false);
  ll n, l, r, k;
  cin >> n >> l >> r >> k;
  ll x = r - l + 1;
  if (x <= 0) x += n;
  ll y = n - x;
  ll ans = -1;
  if (n <= (ll)cbrt(k)) {
    for (ll a = 0; a <= x; a++) {
      for (ll b = 0; b <= y; b++) {
        ll s = a + x + b + y;
        ll bb = k - a - x;
        if (bb >= 0 && bb % s == 0) {
          ans = max(ans, a + b);
        }
        if (a > 0 && (bb + 1) >= 0 && (bb + 1) % s == 0) {
          ans = max(ans, a + b);
        }
      }
    }
  } else {
    for (ll t = 0; t <= k / n; t++) {
      ll bb = k - n * t - x;
      if (bb >= 0) {
        if (t == 0) {
          if (bb <= x) ans = max(ans, bb + y);
          if (bb + 1 <= x) ans = max(ans, bb + 1 + y);
        } else {
          ll minz = max(ge(bb - x, t), ge(bb, t + 1));
          ll maxz = min(le(bb, t), le(bb + y, t + 1));
          if (maxz >= minz) {
            ans = max(ans, maxz);
          }
          bb++;
          minz = max(ge(bb - x, t), ge(bb, t + 1));
          maxz = min(le(bb - 1, t), le(bb + y, t + 1));
          if (maxz >= minz) {
            ans = max(ans, maxz);
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
