#include <bits/stdc++.h>
using namespace std;
long long n, l, r, m, s, up, x, y;
void exgcd(long long a, long long b, long long& x, long long& y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= x * (a / b);
}
long long frac(long long x, long long y) {
  if (x >= 0) return x / y;
  return x % y == 0 ? x / y : x / y - 1;
}
long long l1, r1, l2, r2, ans = -1;
int main() {
  scanf("%lld%lld%lld%lld", &n, &l, &r, &m);
  s = (r - l + n) % n + 1;
  up = m - s;
  if (m < s) return puts("-1"), 0;
  if (m - s <= s) ans = max(ans, m - s + n - s);
  for (long long i = n; i <= min(2 * n, up); i = up / (up / i) + 1) {
    long long k = up / i, right = up - k * n;
    if (right == 0) {
      ans = max(ans, 0ll);
      continue;
    }
    x = 1;
    y = -1;
    x = x * right;
    y = y * right;
    l1 = (x - s) % k == 0 ? (x - s) / k : (x - s) / k + 1;
    r1 = x / k;
    l2 = y % (k + 1) == 0 ? -y / (k + 1) : -y / (k + 1) + 1;
    r2 = frac((n - s - y), (k + 1));
    if (l2 > r1 || l1 > r2 || l1 > r1 || l2 > r2) continue;
    ans = max(ans, x + y + min(r1, r2));
  }
  m++;
  up = m - s;
  if (m - s <= s) ans = max(ans, m - s + n - s);
  for (long long i = n; i <= min(2 * n, up); i = up / (up / i) + 1) {
    long long k = up / i, right = up - k * n;
    if (right == 0) continue;
    x = 1;
    y = -1;
    x = x * right;
    y = y * right;
    l1 = (x - s) % k == 0 ? (x - s) / k : (x - s) / k + 1;
    r1 = x % k == 0 ? x / k - 1 : x / k;
    l2 = y % (k + 1) == 0 ? -y / (k + 1) : -y / (k + 1) + 1;
    r2 = frac((n - s - y), (k + 1));
    if (l2 > r1 || l1 > r2 || l1 > r1 || l2 > r2) continue;
    ans = max(ans, x + y + min(r1, r2));
  }
  printf("%lld\n", ans);
  return 0;
}
