#include <bits/stdc++.h>
using namespace std;
long long n, l, r, k;
bool checks(long long s) {
  long long x = (k - r + l - 1) / (n + s);
  long long sp = k - r + l - 1 - x * (n + s);
  if (sp < r - l + 1) sp++;
  if (sp > r - l + 1) {
    return false;
  }
  if (s - sp > n - r + l - 1) {
    return false;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> l >> r >> k;
  if (l > r) r += n;
  if (k < r - l + 1) {
    cout << "-1";
    return 0;
  }
  if (n < 1e6) {
    long long res = -1;
    long long s = 0, sp = 0;
    for (s = 0; s <= n; s++) {
      if (checks(s)) res = s;
    }
    cout << res << "\n";
  } else {
    long long res = -1;
    long long m = k - r + l - 1;
    for (long long x = 1; x < 1e6; x++) {
      long long a, b;
      a = (m - x * n - r + l - 1) / x;
      b = (m - x * n + n - r + l) / (x + 1);
      b = min(b, n);
      if (a <= b && checks(b))
        res = max(res, b);
      else if (a + 1 <= b && checks(b - 1))
        res = max(res, b - 1);
    }
    long long sp = k - r + l - 1;
    if (sp < r - l + 1) sp++;
    if (sp >= 0 && sp <= r - l + 1) {
      res = max(res, n - r + l - 1 + sp);
    }
    cout << res << "\n";
  }
  return 0;
}
