#include <bits/stdc++.h>
using namespace std;
long long n, l, r, k, x, y;
long long cal1(long long a, long long b) {
  long long val = x + a;
  if (k == val || (a != 0 && k == val - 1)) return a + b;
  long long temp = k - val;
  val = x + a + y + b;
  if (temp % val == 0 || (a != 0 && temp % val == val - 1)) return a + b;
  return -1;
}
long long solve1() {
  long long ans = -1;
  for (int i = 0; i <= x; i++)
    for (int j = 0; j <= y; j++) ans = max(ans, cal1(i, j));
  return ans;
}
long long cal2(long long t) {
  if (t == 0) {
    if (k - x < 0) return -1;
    if (x - (k - x) < 0) return -1;
    long long ans = y + min((k - x) + 1, x);
    return ans;
  }
  long long val = k - (n * t + x);
  if (val < 0) return -1;
  long long hold = min(val / t, y);
  long long mul = val - hold * t;
  if (mul > (t + 1) * x) return -1;
  long long rem = mul % (t + 1), keep = mul / (t + 1);
  if (t == 1) {
    if (rem != 0) {
      if (keep + rem + 1 <= x && rem <= hold) return hold + keep + 1;
      if (keep > 0 && keep + rem <= x && rem - 1 <= y) return hold + keep + 1;
    }
    if (keep + rem <= x && rem <= y) return hold + keep;
  } else if (keep + rem <= x && rem <= hold)
    return hold + keep;
  return -1;
}
long long solve2() {
  long long t = k / n;
  long long ans = -1;
  for (int i = 0; i <= t; i++) ans = max(ans, cal2(i));
  return ans;
}
int main(int argc, char const *argv[]) {
  cin >> n >> l >> r >> k;
  if (l <= r)
    x = r - l + 1, y = n - x;
  else {
    y = l - r - 1;
    x = n - y;
  }
  n <= 7000 ? cout << solve1() << "\n" : cout << solve2() << "\n";
  return 0;
}
