#include <bits/stdc++.h>
using namespace std;
template <class T>
T gi() {
  T x = 0;
  bool f = 0;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') f = 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f ? -x : x;
}
long long n, l, r, k, len;
namespace cpp1 {
bool check(long long x) {
  long long tmp = k % (x + n), x1 = tmp - len;
  if (x1 < 0) return 0;
  if (x1 < min(x, len)) x1++;
  if (x1 <= min(x, len) && x - x1 <= n - len) return 1;
  return 0;
}
void main() {
  for (int i = n; i >= 0; i--)
    if (check(i)) {
      printf("%d\n", i);
      return;
    }
  puts("-1");
  return;
}
}  // namespace cpp1
namespace cpp2 {
void main() {
  len = (r - l + n) % n + 1;
  long long ans = -1;
  for (long long i = 0; i <= k / n; i++) {
    long long p = (k - 2 * len - (i - 1) * n + 1) / (i + 1);
    long long y = k - i * (n + p) - len;
    if (y < 0) {
      if (i == 0) continue;
      long long dis = (-y - 1) / i + 1;
      y += i * dis;
      p -= dis;
    }
    if (p > n) {
      y += (p - n) * i;
    }
    if (y < 0 || y > p || y > len || len - (y + 1) > n - p) continue;
    long long y2 = y + 1;
    if (y2 <= p && y2 <= len) {
      ans = max(ans, p);
    }
    if (len - y <= n - p) {
      ans = max(ans, p);
    }
  }
  printf("%lld\n", ans);
}
}  // namespace cpp2
int main() {
  n = gi<long long>(), l = gi<long long>(), r = gi<long long>(),
  k = gi<long long>();
  len = (r - l + 1) % n;
  if (len < 0) len += n;
  if (n <= 2e6)
    cpp1 ::main();
  else
    cpp2 ::main();
  return 0;
}
