#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
static void redirect(int argc, const char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  if (argc > 1) {
    static filebuf f;
    f.open(argv[1], ios::in);
    cin.rdbuf(&f);
    if (!cin) {
      cerr << "Failed to open '" << argv[1] << "'" << endl;
      exit(1);
    }
  }
  if (argc > 2) {
    static filebuf f;
    f.open(argv[2], ios::out | ios::trunc);
    cout.rdbuf(&f);
    if (!cout) {
      cerr << "Failed to open '" << argv[2] << "'" << endl;
    }
  }
}
int main(int argc, const char **argv) {
  redirect(argc, argv);
  long long n, l, r, k;
  cin >> n >> l >> r >> k;
  long long q = (r - l + n) % n + 1;
  k -= q;
  long long ans = -1;
  if (k >= 0 && k <= q) ans = min(k + 1, q) + (n - q);
  for (long long pass = 0; pass < 2; pass++) {
    for (long long i = 1; i * i <= k; i++) {
      long long s = (k - pass) / i - n;
      s = min(s, (k - i * n + n - q) / (i + 1));
      if (s >= 0 && s <= n) {
        long long t = k - i * (n + s);
        if (t >= pass && t <= q && t <= s && s - t <= n - q) ans = max(ans, s);
      }
      if (i >= n && i <= 2 * n) {
        s = i - n;
        long long lo = max(pass, s - (n - q));
        long long w = (k - lo) / (n + s);
        long long t = k - w * (n + s);
        if (t >= pass && t <= q && t <= s && s - t <= n - q) ans = max(ans, s);
      }
    }
    k++;
  }
  cout << ans << '\n';
  return 0;
}
