#include <bits/stdc++.h>
using namespace std;
inline void read(long long &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(long long x) {
  static const long long maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  long long len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}
const long long MAXN = 3510;
long long n, m, k, l, r, len;
int main() {
  read(n);
  read(l);
  read(r);
  read(k);
  len = (r - l + n) % n + 1;
  if (n <= sqrt(3e7)) {
    for (long long sum = n; sum >= 0; sum--)
      for (long long x = 0; x <= sum; x++) {
        long long y = sum - x;
        if ((x < 0) || (x > (n - len)) || (y < 0) || (y > len)) continue;
        if ((k - len - y) % (x + y + n) == 0) {
          long long t = (k - len - y) / (x + y + n);
          if (t >= 0) {
            printf("%I64d\n", sum);
            return 0;
          }
        }
        if (y != 0)
          if ((k - len - y + 1) % (x + y + n) == 0) {
            long long t = (k - len - y) / (x + y + n);
            if (t >= 0) {
              printf("%I64d\n", sum);
              return 0;
            }
          }
      }
    puts("-1");
    return 0;
  } else {
    long long MAX = -1;
    if ((k <= 2 * len) && (k >= len))
      MAX = n - len + min(k - len + (k >= len), len);
    for (long long t = 1; t <= k / n; t++) {
      long long tmp = k - t * n - len;
      if (tmp < 0) continue;
      long long x0 = -tmp, y0 = tmp;
      long long TK = min(tmp / t, (n - len + tmp) / (t + 1));
      x0 = x0 + TK * (t + 1);
      y0 = y0 - TK * t;
      if ((x0 < 0) || (x0 > (n - len)) || (y0 < 0) || (y0 > len)) continue;
      MAX = max(MAX, x0 + y0);
    }
    for (long long t = 1; t <= k / n; t++) {
      long long tmp = k - t * n - len + 1;
      if (tmp < 0) continue;
      long long x0 = -tmp, y0 = tmp;
      long long TK = min(tmp / t, (n - len + tmp) / (t + 1));
      x0 = x0 + TK * (t + 1);
      y0 = y0 - TK * t;
      if (y0 == 0) {
        y0 += t;
        x0 -= t + 1;
      }
      if ((x0 < 0) || (x0 > (n - len)) || (y0 < 0) || (y0 > len)) continue;
      MAX = max(MAX, x0 + y0);
    }
    printf("%I64d\n", MAX);
  }
  return 0;
}
