#include <bits/stdc++.h>
using namespace std;
long long n, l, r, k, x, res = -1;
long long ceil(long long a, long long b) {
  if (a < 0 != b < 0) return a / b;
  return (a + b - 1) / b;
}
void gcd(long long a, long long b, long long &x, long long &y, long long &d) {
  if (b == 0) {
    x = 1, y = 0, d = a;
    return;
  }
  gcd(b, a % b, x, y, d);
  long long tx = y;
  y = x - (a / b) * y;
  x = tx;
}
void solve1() {
  if (k - x >= 0 && k - x <= x) res = max(res, k + n - 2 * x + int(k - x < x));
  for (long long q = 1; k - x - q * n >= 0; q++) {
    long long y = k - x - q * n;
    long long a0, b0, d;
    gcd(q + 1, q, a0, b0, d);
    long long t = ceil(x + b0 * y - n, q + 1);
    long long a = y * a0 + q * t;
    long long b = y * b0 - (q + 1) * t;
    if (0 <= a && a <= x && 0 <= b && b <= n - x) res = max(res, a + b);
    y++;
    t = ceil(x + b0 * y - n, q + 1);
    if (y * a0 + q * t == 0) t++;
    a = y * a0 + q * t;
    b = y * b0 - (q + 1) * t;
    if (0 < a && a <= x && 0 <= b && b <= n - x) res = max(res, a + b);
  }
}
void solve2() {
  for (long long a = 0; a <= x; a++)
    for (long long b = 0; b <= n - x; b++) {
      if (k % (n + a + b) == 0 && n + a + b == x + a) res = max(res, a + b);
      if (k % (n + a + b) == a + x) res = max(res, a + b);
      if (a) {
        if ((k + 1) % (n + a + b) == 0 && n + a + b == x + a)
          res = max(res, a + b);
        if ((k + 1) % (n + a + b) == a + x) res = max(res, a + b);
      }
    }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> l >> r >> k;
  if (l > r) r += n;
  x = (r - l + 1);
  if ((x + 1) > 1e7 / (n - x + 1))
    solve1();
  else
    solve2();
  cout << res << '\n';
  return 0;
}
