#include <bits/stdc++.h>
using namespace std;
long long n, l, r, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> l >> r >> k;
  r = (r - l + n) % n + 1;
  long long ans = -1;
  long long o = n - r;
  if (n <= 5000) {
    for (long long i = (0); i <= (signed)(r); i++) {
      for (long long j = (0); j <= (signed)(o); j++) {
        long long per = r + i + j + o;
        long long res = (k - 1) % per + 1;
        if (res == r + i) {
          ans = max(ans, i + j);
        }
        if (res + 1 == r + i && i != 0) {
          ans = max(ans, i + j);
        }
      }
    }
    cout << ans << "\n";
  } else {
    for (long long i = (0); i <= (signed)(k / n); i++) {
      if (i == 0) {
        if (r * 2 < k || r > k) continue;
        ans = max(ans, n - r * 2 + k);
        if (r * 2 != k) ++ans;
        continue;
      }
      long long ot = r * i + o * i + r;
      if (ot > k) continue;
      long long ls = k - ot;
      long long sma = ls % i;
      long long lgb = (ls - sma) / i - sma;
      if (lgb > o) {
        long long dif = lgb - o;
        long long ch = (dif + i) / (i + 1);
        lgb -= ch * (i + 1);
        sma += ch * i;
      }
      if (lgb < 0 || sma > r) continue;
      ans = max(ans, lgb + sma);
    }
    for (long long i = (1); i <= (signed)(k / n); i++) {
      long long ot = r * i + o * i + r;
      if (ot > k + 1) continue;
      long long ls = k + 1 - ot;
      long long sma = ls % i;
      long long lgb = (ls - sma) / i - sma;
      if (lgb > o) {
        long long dif = lgb - o;
        long long ch = (dif + i) / (i + 1);
        lgb -= ch * (i + 1);
        sma += ch * i;
      }
      if (lgb < 0 || sma > r || sma == 0) continue;
      ans = max(ans, lgb + sma);
    }
    cout << ans << "\n";
  }
  return 0;
}
