#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, k, l, r, p;
ll answ;
void Solve(ll& a, ll& b, ll top, ll bot) {
  if (bot == 0) {
    a = 1;
    b = 0;
    return;
  }
  ll ta, tb;
  Solve(ta, tb, bot, top % bot);
  a = tb;
  b = ta - (top / bot) * tb;
}
void Check(ll& answ, ll s1, ll s2, ll adc, ll c) {
  if (s1 < adc || s2 < 0 || s1 > p || s2 > (n - p) || c < 0) return;
  if ((n + s1 + s2) * c + s1 + p == k + adc) answ = max(answ, (s1 + s2));
}
ll divUp(ll top, ll bot) {
  if (top < 0) {
    ll posbot = (-top + bot - 1) / bot;
    assert((top + posbot * bot) >= 0);
    return ((top + posbot * bot) / bot) - posbot;
  }
  return top / bot;
}
int main() {
  cin >> n >> l >> r >> k;
  answ = -1;
  p = (r - (l - 1));
  if (p <= 0) p += n;
  if (p <= k && 2 * p >= k) {
    ll spec = 1 + min((k - p), p - 1);
    answ = max(answ, (n - p) + spec);
  }
  if (n <= 1000000) {
    for (ll s1s2 = 0; s1s2 <= n; s1s2++) {
      for (ll adc = 0; adc < 2; adc++) {
        ll z = (n + s1s2);
        ll c = (k + adc - p) / z;
        ll s1 = (k + adc - p) - c * z;
        ll s2 = s1s2 - s1;
        Check(answ, s1, s2, adc, c);
      }
    }
  } else {
    for (ll c = 0; c <= 100000 && c <= k; c++) {
      for (ll adc = 0; adc < 2; adc++) {
        ll z = k + adc - p - n * c;
        if (c == 0) {
          ll s1, s2;
          s2 = 0;
          s1 = z;
          Check(answ, s1, s2, adc, c);
        } else {
          ll sp1, sp2;
          Solve(sp1, sp2, c + 1, c);
          assert(sp1 * (c + 1) + sp2 * c == 1);
          sp1 = sp1 * z;
          sp2 = sp2 * z;
          ll t1 = divUp((adc - sp1 + (c - 1)), c);
          sp1 = sp1 + t1 * c;
          sp2 = sp2 - t1 * (c + 1);
          assert(sp1 * (c + 1) + sp2 * c == z);
          Check(answ, sp1, sp2, adc, c);
          t1 = divUp((p - sp1), c);
          sp1 = sp1 + t1 * c;
          sp2 = sp2 - t1 * (c + 1);
          assert(sp1 * (c + 1) + sp2 * c == z);
          Check(answ, sp1, sp2, adc, c);
          ll t2 = divUp(sp2, (c + 1));
          sp1 = sp1 + t2 * c;
          sp2 = sp2 - t2 * (c + 1);
          assert(sp1 * (c + 1) + sp2 * c == z);
          Check(answ, sp1, sp2, adc, c);
          t2 = divUp((sp2 - (n - p) + c), (c + 1));
          sp1 = sp1 + t2 * c;
          sp2 = sp2 - t2 * (c + 1);
          assert(sp1 * (c + 1) + sp2 * c == z);
          Check(answ, sp1, sp2, adc, c);
        }
      }
    }
  }
  cout << answ << endl;
}
