#include <bits/stdc++.h>
using namespace std;
template <typename T>
void inline chkMax(T &x, T y) {
  if (y > x) x = y;
}
template <typename T>
void inline chkMin(T &x, T y) {
  if (y < x) x = y;
}
template <typename T>
void inline read(T &x) {
  int f = 1;
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
  x *= f;
}
template <typename T>
void print(T x) {
  if (x < 0) {
    putchar('-');
    print(-x);
    return;
  }
  if (x >= 10) print(x / 10);
  putchar((x % 10) + '0');
}
long long inline calc(long long k, long long n, long long l, long long o) {
  k -= l;
  if (k < 0) return -1;
  if (n <= 10000000) {
    for (long long q = n; ~q; q--) {
      long long u = k % (n + q);
      if (max(o, q + l - n) <= u && u <= min(l, q)) {
        return q;
      }
    }
    return -1;
  } else {
    long long R = n;
    for (long long t = 0; t * n <= k; t++) {
      if (R == -1) break;
      long long L = max(0ll, (k - (t + 1) * n + 1 + t) / (t + 1));
      if (L > R) continue;
      long long q = min((k - l + n - t * n) / (t + 1), R);
      if (R >= q && q >= L) {
        long long u = k % (n + q);
        if (max(o, q + l - n) <= u && u <= min(l, q)) {
          return q;
        }
      }
      R = L - 1;
    }
    return -1;
  }
}
int main() {
  long long n, l, r, k;
  read(n), read(l), read(r), read(k);
  if (r < l) r += n;
  long long len = r - l + 1;
  long long ans = -1;
  chkMax(ans, calc(k, n, len, 0));
  chkMax(ans, calc(k + 1, n, len, 1));
  print(ans);
}
