#include <bits/stdc++.h>
using namespace std;
long long int cons;
long long int check(long long int a) {
  if (a >= cons) a %= cons;
  return a;
}
long long int check2(long long int a) {
  a %= cons;
  if (a < 0) a += cons;
  return a;
}
long long int fast_check(long long int a) {
  if (a >= cons) a -= cons;
  return a;
}
long long int GCD(long long int a, long long int b) {
  while (a && b) {
    a = a % b;
    if (a != 0) b = b % a;
  }
  return a + b;
}
long long int exp(long long int a, long long int n) {
  long long int ans = 1;
  a = check(a);
  while (n) {
    if (n & 1) ans = check(ans * a);
    a = check(a * a);
    n = (n >> 1);
  }
  return ans;
}
void print_pair(pair<int, int> p) {
  cout << p.first << " " << p.second << endl;
}
long long int nc2(long long int a) { return (a * (a - 1)) / 2; }
const int N = 2002;
long long int n, l, r, s;
long long int ans = -1;
long long int term;
long long int other;
bool consistent(long long int t, long long int rem) {
  if (rem > t) return false;
  t -= rem;
  if (other < t) return false;
  return true;
}
void solve1() {
  if (s < 0) return;
  for (long long int t = 0; t <= n; t++) {
    long long int rem = s % (n + t);
    if (rem <= term && consistent(t, rem)) {
      ans = max(ans, t);
      continue;
    }
    rem++;
    if (rem <= term && consistent(t, rem)) {
      ans = max(ans, t);
      continue;
    }
  }
}
void solve2() {
  if (s < 0) return;
  long long int max_cycle = s / n;
  if (s <= term) {
    if (s < term) s++;
    ans = s + other;
    return;
  }
  for (long long int k = 1; k <= max_cycle; k++) {
    long long int it_rem = s - k * n;
    long long int maxt = it_rem / k;
    long long int mint = it_rem / (k + 1);
    long long int bestt = -1;
    while (mint <= maxt) {
      long long int mid = (mint + maxt) / 2;
      long long int rem = it_rem - k * mid;
      if (rem > term) {
        mint = mid + 1;
        continue;
      }
      if (rem <= mid && mid - rem <= other) {
        bestt = mid;
        mint = mid + 1;
        continue;
      }
      rem++;
      if (rem <= mid && mid - rem <= other) {
        bestt = mid;
        mint = mid + 1;
        continue;
      } else
        maxt = mid - 1;
    }
    if (bestt != -1) ans = max(ans, bestt);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cons = 1e9 + 7;
  srand(time(NULL));
  cin >> n >> l >> r >> s;
  if (l > r)
    term = n - (l - r - 1);
  else
    term = r - l + 1;
  s -= term;
  other = n - term;
  if (n <= 5 * 1000000)
    solve1();
  else
    solve2();
  cout << ans;
  return 0;
}
