#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using db = long double;
LL n, l, r, k, lrst, ans = -1E9, st;
int main() {
  scanf("%lld%lld%lld%lld", &n, &l, &r, &k);
  r = r - l + 1;
  if (r < 0) r += n;
  if (r == n) r = 0;
  if (n > 1E6) {
    if (k <= 2 * r && k >= r) ans = k - r + n - r;
    for (LL p = max(1LL, k / n / 2 - 1); p <= k / n; p++) {
      LL tmp = k - 2 * p * n + (p - 1) * r;
      if (tmp <= 0)
        lrst = 0;
      else
        lrst = max((tmp - 1) / (p + 1) + 1, 0LL);
      st = (k - p * n - r - lrst) / p;
      lrst = k - p * n - r - p * st;
      if (lrst <= r) ans = max(ans, st);
    }
    k++;
    if (k <= 2 * r && k >= r) ans = k - r + n - r;
    for (LL p = max(k / n / 2 - 1, 1LL); p <= k / n; p++) {
      LL tmp = k - 2 * p * n + (p - 1) * r;
      if (tmp <= 0)
        lrst = 0;
      else
        lrst = max((tmp - 1) / (p + 1) + 1, 0LL);
      st = (k - p * n - r - lrst) / p;
      lrst = k - p * n - r - p * st;
      if (lrst <= r && (lrst > 0 || r == 0)) ans = max(ans, st);
    }
  } else {
    if (k <= 2 * r && k >= r) ans = k - r + n - r;
    for (LL i = n; i <= n * 2; i++) {
      LL p = k / i;
      if (p == 0) break;
      LL tmp = k - 2 * p * n + (p - 1) * r;
      if (tmp <= 0)
        lrst = 0;
      else
        lrst = max((tmp - 1) / (p + 1) + 1, 0LL);
      st = (k - p * n - r - lrst) / p;
      lrst = k - p * n - r - p * st;
      if (lrst <= r) ans = max(ans, st);
    }
    k++;
    if (k <= 2 * r && k >= r) ans = k - r + n - r;
    for (LL i = n; i <= n * 2; i++) {
      LL p = k / i;
      if (p == 0) break;
      LL tmp = k - 2 * p * n + (p - 1) * r;
      if (tmp <= 0)
        lrst = 0;
      else
        lrst = max((tmp - 1) / (p + 1) + 1, 0LL);
      st = (k - p * n - r - lrst) / p;
      lrst = k - p * n - r - p * st;
      if (lrst <= r && (lrst > 0 || r == 0)) ans = max(ans, st);
    }
  }
  printf("%lld", ans < 0 ? -1 : ans);
  return 0;
}
