#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
  return x * x;
}
template <typename T>
T abs(T x) {
  return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
bool chmin(T& x, const T& y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T>
bool chmax(T& x, const T& y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
int f(long long n, long long m, long long k, long long w, long long t) {
  if (t < 0 || t > n) {
    return -1;
  }
  long long d = k - w * (n + t);
  if (d < (m + max(0LL, t - (n - m) - 1))) {
    return -1;
  }
  if (d > (m + min(m, t))) {
    return 1;
  }
  return 0;
}
int main(int, char**) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, l, r, k;
  while (cin >> n >> l >> r >> k) {
    long long m = (r + n - l) % n + 1;
    long long ans = -1;
    for (long long w = 0; w <= 1e+5 + 42; ++w) {
      long long l = 0;
      long long r = n;
      while (l < r) {
        long long c = (l + r) / 2;
        int v = f(n, m, k, w, c);
        if (v == -1) {
          r = c - 1;
        } else if (v == 1) {
          l = c + 1;
        } else {
          chmax(ans, c);
          l = c + 1;
        }
      }
      if (f(n, m, k, w, l) == 0) {
        chmax(ans, l);
      }
      if (f(n, m, k, w, r) == 0) {
        chmax(ans, r);
      }
    }
    for (long long t = min(n, (long long)1e+6 + 42); t > ans; --t) {
      long long d = (k - 1) % (n + t) + 1;
      if ((m + max(0LL, t - (n - m) - 1)) <= d && d <= (m + min(m, t))) {
        ans = t;
        break;
      }
    }
    cout << ans << endl;
  }
  cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << endl;
  return 0;
}
