#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
template <class T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(48 + x % 10);
}
template <class T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
long long n, k, l, r, x, t, s, p, ans, y;
long long const inf = 1LL << 50;
long long calc(long long p, long long s, int a, int b) {
  if (a == 0 || b == 1) {
    if (k - s - x >= 0 && (k - s - x) % (n + p) == 0)
      return p;
    else
      return -1;
  } else {
    if (k - s + 1 - x >= 0 && (k - s + 1 - x) % (n + p) == 0)
      return p;
    else
      return -1;
  }
}
long long exgcd(long long &x, long long &y, long long a, long long b) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  long long t = exgcd(y, x, b, a % b);
  y -= a / b * x;
  return t;
}
void solve(long long t1, long long t2, long long c, long long add) {
  if (t2 == 0) {
    if (2 * x >= k && k >= x) chkmax(ans, min(x, k - x + 1) + y);
    return;
  }
  long long a, b;
  long long g = exgcd(a, b, t1, t2);
  a = a * c;
  b = b * c;
  long long l = -inf, r = inf;
  a -= add;
  if (a >= 0)
    r = min(r, a / t2);
  else
    r = min(r, (a - t2 + 1) / t2);
  a += add;
  if (a - x >= 0)
    l = max(l, (a - x + t2 - 1) / t2);
  else
    l = max(l, (a - x) / t2);
  if (y - b >= 0)
    r = min(r, (y - b) / t1);
  else
    r = min(r, (y - b - t1 + 1) / t1);
  if (-b >= 0)
    l = max(l, (-b + t1 - 1) / t1);
  else
    l = max(l, -b / t1);
  if (l <= r) {
    chkmax(ans, a + b + l);
    chkmax(ans, a + b + r);
  }
}
int main() {
  read(n);
  read(l);
  read(r);
  read(k);
  x = (r - l + n) % n + 1;
  y = n - x;
  ans = -1;
  if (k / n >= 5000000) {
    for (int p = 0; p <= n; p++)
      for (int s = max(0LL, x - (n - p)); s <= min((int)x, p); s++) {
        if (s < x) chkmax(ans, calc(p, s, 0, 0));
        if (s) chkmax(ans, calc(p, s, 1, 0));
        if (s) chkmax(ans, calc(p, s, 1, 1));
      }
  } else {
    for (int t = 0; t <= k / n; t++) {
      solve(t + 1, t, k - t * x - x - t * y, 0);
      solve(t + 1, t, k - t * x - x - t * y + 1, 1);
    }
  }
  write(ans);
  return 0;
}
