#include <bits/stdc++.h>
using namespace std;
long long solve1(long long p1, long long p2, long long candies, long long n) {
  long long ans = -1;
  for (long long a = 0; a <= p1; a++) {
    for (long long b = 0; b <= p2; b++) {
      long long rc = candies - a;
      long long v = rc % (n + a + b);
      if (rc >= 0 && (v == 1 || (v == 2 && b > 0))) {
        ans = max(ans, a + b);
      }
    }
  }
  return ans;
}
long long solve2(long long p1, long long p2, long long candies, long long n) {
  long long ans = -1;
  for (long long sum = p1 + p2; sum >= 0; sum--) {
    for (int rem = 1; rem <= 2; rem++) {
      if (rem >= n + sum) continue;
      long long mina = 0;
      long long maxa = p1;
      mina = max(mina, sum - p2);
      maxa = min(maxa, sum - (rem == 2));
      maxa = min(maxa, candies);
      long long minc = candies - maxa;
      long long maxc = candies - mina;
      if (minc > maxc) continue;
      long long c = ((maxc - rem) / (n + sum)) * (n + sum) + rem;
      if (minc <= c && c <= maxc) {
        ans = max(ans, sum);
      }
    }
  }
  return ans;
}
long long solve3(long long p1, long long p2, long long candies, long long n) {
  long long ans = -1;
  long long maxcyc = candies / n;
  for (long long cyc = 0; cyc <= maxcyc; cyc++) {
    for (long long rem = 1; rem <= 2; rem++) {
      long long lhs = candies - rem;
      if (lhs < 0) continue;
      if (cyc == 0) {
        long long a = lhs;
        if (0 <= a && a <= p1) {
          long long b = p2;
          if (rem < n + a + b) {
            if (rem == 1 || b > 0) {
              ans = max(ans, a + b);
            }
          }
        }
        continue;
      }
      long long diffmin = n + (rem == 2);
      long long tmod = lhs % cyc;
      long long g = lhs - cyc * (n + p2);
      g = max(0LL, g);
      g = (g + cyc) / (cyc + 1);
      g = max(g, tmod);
      long long t = ((g - tmod + cyc - 1) / cyc) * cyc + tmod;
      if (t > p1 || t > lhs) continue;
      long long s = (lhs - t) / cyc;
      if (s - t < diffmin) continue;
      if (s - t > n + p2) continue;
      if (s <= rem) continue;
      ans = max(ans, s - n);
    }
  }
  return ans;
}
void test() {
  for (long long p1 = 0; p1 < 50; p1++) {
    for (long long p2 = 0; p2 < 50; p2++) {
      if (p1 == 0 && p2 == 0) continue;
      for (long long candies = 0; candies < 150; candies++) {
        long long ans1 = solve1(p1, p2, candies, p1 + p2);
        long long ans2 = solve2(p1, p2, candies, p1 + p2);
        long long ans3 = solve3(p1, p2, candies, p1 + p2);
        if (ans1 != ans2 || ans1 != ans3) {
          cerr << p1 << " " << p2 << " " << candies << " " << p1 + p2 << endl;
          cerr << ans1 << " " << ans2 << " " << ans3 << endl;
          exit(0);
        }
      }
    }
  }
  cerr << "done test" << endl;
}
int main() {
  cin.sync_with_stdio(0);
  long long n, l, r, candies;
  cin >> n >> l >> r >> candies;
  l--;
  r--;
  r -= l;
  l -= l;
  if (r < 0) r += n;
  long long p1 = r - l;
  long long p2 = n - p1;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  candies -= p1;
  long long ans = -1;
  if (p1 + p2 <= 3e6) {
    ans = solve2(p1, p2, candies, n);
  } else {
    ans = solve3(p1, p2, candies, n);
  }
  cout << ans << endl;
  cin.sync_with_stdio(0);
}
