#include <bits/stdc++.h>
using namespace std;
long long n, k, l, r, t;
long long extgcd(long long a, long long b, long long c, long long &x,
                 long long &y) {
  if (b == 0) {
    x = c / a, y = 0;
    return a;
  }
  long long g = extgcd(b, a % b, c, y, x);
  y -= x * (a / b);
  return g;
}
bool getans(long long a, long long b, long long c, long long &x, long long &y,
            long long n) {
  if (a == 0) {
    y = c / b;
    x = n;
    return y >= 0 && y <= t;
  }
  extgcd(a, b, c, x, y);
  y = (y % a + a) % a;
  if (y > t) return false;
  x = (c - y * b) / a;
  if (x > n) y += (x - n) * a, x = n;
  if (x < 0 || x > n || y < 0 || y > t) return false;
  return true;
}
int main() {
  scanf("%lld%lld%lld%lld", &n, &l, &r, &k), --l, --r;
  t = (r + n - l) % n + 1;
  if (k / n > n) {
    for (long long x = n; x >= 0; x--) {
      long long a, b;
      if (getans(x + n, 1, k - t, a, b, k / n) && x - b <= n - t)
        return printf("%lld\n", x) * 0;
      if (getans(x + n, 1, k + 1 - t, a, b, k / n) && x - b <= n - t && b > 0)
        return printf("%lld\n", x) * 0;
    }
    puts("-1");
  } else {
    long long res = -1;
    for (long long w = 0; w * n + t <= k; w++) {
      long long a, b;
      if (getans(w, 1, k - n * w - t, a, b, n)) {
        long long kk = a - b <= n - t ? 0 : (n - t - a + b - w) / (w + 1);
        a += kk, b -= kk * w;
        if (a >= 0 && a <= n && b >= 0 && b <= t) res = max(res, a);
      }
      if (getans(w, 1, k + 1 - n * w - t, a, b, n)) {
        long long kk = a - b <= n - t ? 0 : (n - t - a + b - w) / (w + 1);
        a += kk, b -= kk * w;
        if (b <= 0) b += w, --a;
        if (a >= 0 && a <= n && b >= 0 && b <= t) res = max(res, a);
      }
    }
    printf("%lld\n", res);
  }
  return 0;
}
