#include <bits/stdc++.h>
using namespace std;
long long n, l, r, s, n1, n2;
bool check1(long long t, long long s, long long flag) {
  long long temp = s / (t + n);
  long long temp2 = s % (t + n);
  if (temp2 >= n1 + flag && temp2 <= n1 + min(t, n1) && t - (temp2 - n1) >= 0 &&
      t - (temp2 - n1) <= n2)
    return 1;
  if (temp) {
    temp2 += t + n;
    if (temp2 >= n1 + flag && temp2 <= n1 + min(t, n1) &&
        t - (temp2 - n1) >= 0 && t - (temp2 - n1) <= n2)
      return 1;
  }
  return 0;
}
bool check1(long long t) { return check1(t, s, 0) || check1(t, s + 1, 1); }
long long solve1() {
  for (long long x = n; x >= 0; x--)
    if (check1(x)) return x;
  return -1;
}
long long div1(long long x, long long y) {
  if (!x) return 0;
  if (x > 0)
    return x / y;
  else {
    x = -x;
    return -((x - 1) / y + 1);
  }
}
long long div2(long long x, long long y) {
  if (!x) return 0;
  if (x > 0)
    return (x - 1) / y + 1;
  else {
    x = -x;
    return -(x / y);
  }
}
long long calc2(long long k, long long s, long long flag) {
  s -= k * n + n1;
  long long l1 = div2(s, k + 1);
  long long l2 = div2(s - n1, k);
  long long l3 = div1(s - flag, k);
  long long l4 = div1(s + n2, k + 1);
  l1 = max(l1, l2);
  l3 = min(l3, l4);
  if (l1 <= l3) return l3;
  return -1;
}
long long solve2() {
  long long ans = -1;
  if (s >= n1 && s <= n1 * 2) {
    ans = max(ans, s - n1 + n2);
  }
  if (s + 1 > n1 && s + 1 <= n1 * 2) {
    ans = max(ans, s + 1 - n1 + n2);
  }
  for (long long k = 1; k * n <= s + 1; k++) {
    ans = max(ans, calc2(k, s, 0));
    ans = max(ans, calc2(k, s + 1, 1));
  }
  return ans;
}
int main() {
  cin >> n >> l >> r >> s;
  n1 = r - l + 1;
  if (n1 <= 0) n1 += n;
  n2 = n - n1;
  if (n < s / n)
    cout << solve1() << endl;
  else
    cout << solve2() << endl;
  return 0;
}
