#include <bits/stdc++.h>
using namespace std;
long long solve1(long long n, long long d, long long k) {
  long long ans = -1;
  for (long long x = 0; x <= n; x++) {
    for (long long y = max(x + d - n, 0LL); y <= min(d, x); y++) {
      long long k2 = k - d - y;
      if (k2 >= 0 && k2 % (n + x) == 0) {
        ans = x;
      }
      if (y > 0 && k2 + 1 >= 0 && (k2 + 1) % (n + x) == 0) ans = x;
    }
  }
  return ans;
}
long long solve2(long long n, long long d, long long k) {
  long long ans = -1;
  for (long long r = 0; r <= k / n; r++) {
    long long k2 = k - d - n * r;
    if (r == 0) {
      if (k2 < 0)
        continue;
      else if (k2 <= d - 1)
        ans = max(ans, n - d + (k2 + 1));
      else if (k2 == d)
        ans = max(ans, n);
      continue;
    }
    if (k2 >= 0) {
      long long x1, x2, x;
      x1 = k2 / r;
      x2 = (k2 + n - d) / (r + 1);
      x = min(n, min(x1, x2));
      if (k2 - r * x <= d && k2 - r * x <= x) {
        ans = max(ans, x);
      }
    }
    k2++;
    if (k2 >= 0) {
      long long x1, x2, x;
      x1 = (k2 - 1) / r;
      x2 = (k2 + n - d) / (r + 1);
      x = min(n, min(x1, x2));
      if (k2 - r * x <= d && k2 - r * x <= x) {
        ans = max(ans, x);
      }
    }
  }
  return ans;
}
int main() {
  long long n, l, r, k;
  cin >> n >> l >> r >> k;
  l--;
  r--;
  long long d = (n + r - l) % n + 1;
  if (n <= 5000) {
    cout << solve1(n, d, k) << "\n";
  } else
    cout << solve2(n, d, k) << "\n";
}
