#include <bits/stdc++.h>
using namespace std;
long long n, l, r, k, ans;
long long f1(long long x, long long y) {
  return x >= 0 ? x / y : -(-x + y - 1) / y;
}
long long f2(long long x, long long y) {
  return x >= 0 ? (x + y - 1) / y : -(-x / y);
}
int main() {
  scanf("%lld%lld%lld%lld", &n, &l, &r, &k);
  long long x = r - l + 1;
  if (x <= 0) x += n;
  long long y = n - x;
  ans = -1;
  if (n <= (long long)cbrt(k)) {
    for (long long a = 0; a <= x; a++)
      for (long long b = 0; b <= y; b++) {
        long long sum = a + b + x + y, b_ = k - a - x;
        if (b_ >= 0 && b_ % sum == 0) ans = max(ans, a + b);
        if (a > 0 && b_ + 1 >= 0 && (b_ + 1) % sum == 0) ans = max(ans, a + b);
      }
  } else {
    for (long long t = 0; t <= k / n; t++) {
      long long b_ = k - n * t - x;
      if (b_ < 0) continue;
      if (t == 0) {
        if (b_ <= x) ans = max(ans, b_ + y);
        if (b_ + 1 <= x) ans = max(ans, b_ + 1 + y);
      } else {
        long long Min = max(f2(b_ - x, t), f2(b_, t + 1));
        long long Max = min(f1(b_, t), f1(b_ + y, t + 1));
        if (Max >= Min) ans = max(ans, Max);
        b_++;
        Min = max(f2(b_ - x, t), f2(b_, t + 1));
        Max = min(f1(b_ - 1, t), f1(b_ + y, t + 1));
        if (Max >= Min) ans = max(ans, Max);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
