#include <bits/stdc++.h>
using namespace std;
long long int n;
long long int l;
long long int r;
long long int k;
pair<long long int, long long int> calc(long long int sm, long long int len) {
  long long int two = (sm - len);
  return make_pair(len - two, two);
}
pair<long long int, long long int> S(long long int val, long long int unit,
                                     long long int lef, long long int rig) {
  long long int cong = (val % unit) + unit;
  cong %= unit;
  if (rig < cong) {
    return make_pair(1, -1);
  }
  long long int want = cong + unit - (lef % unit);
  want %= unit;
  lef += want;
  want = (rig % unit) - cong + unit;
  want %= unit;
  rig -= want;
  return make_pair(lef, rig);
}
int main() {
  cin >> n >> l >> r >> k;
  long long int len = r + n - l + 1LL;
  long long int ot = n - len;
  len %= n;
  long long int maxi = -1;
  if (n <= 1000000) {
    for (long long int periphery = n; periphery <= 2LL * n; periphery++) {
      long long int f = k / periphery;
      long long int md = k % periphery;
      if (md < len) {
        continue;
      }
      if (md > len * 2LL) {
        continue;
      }
      auto overall = calc(periphery, n);
      auto rest = calc(md, len);
      if (rest.first > overall.first) {
        rest.first--;
        rest.second++;
      }
      if (rest.first <= overall.first && rest.second <= overall.second) {
        maxi = max(maxi, overall.second);
      }
    }
    printf("%lld\n", maxi);
    return 0;
  }
  if (k < len) {
    puts("-1");
    return 0;
  }
  if (len == 0) {
    len = n;
  }
  if (len * 2LL >= k) {
    auto z = calc(k, len);
    if (z.first) {
      z.second++;
    }
    maxi = max(maxi, z.second + (n - len));
  }
  len %= n;
  for (long long int i = 1; i <= 1000000; i++) {
    long long int walk = i * n + len;
    long long int R = k - walk;
    if (R < 0) continue;
    long long int lcm = i * (i + 1);
    long long int unit_a = lcm / i;
    long long int unit_b = lcm / (i + 1);
    auto ap = S(-R, unit_a, 0, n - len);
    auto ap2 = S(R, unit_b, 0, len);
    if (ap.first > ap.second || ap2.first > ap2.second) continue;
    ap2.first = (R - (i + 1) * ap2.first) / i;
    ap2.second = (R - (i + 1) * ap2.second) / i;
    if (ap2.first > ap2.second) swap(ap2.first, ap2.second);
    long long int lef = max(ap.first, ap2.first);
    long long int rig = min(ap.second, ap2.second);
    if (lef > rig) continue;
    maxi = max(maxi, lef + (R - lef * i) / (i + 1));
    maxi = max(maxi, rig + (R - rig * i) / (i + 1));
  }
  long long int vis = 0;
  for (long long int i = 1; i <= 1000000; i++) {
    long long int walk = i * n + len;
    long long int R = k - walk + 1LL;
    if (R < 0) continue;
    long long int lcm = i * (i + 1);
    long long int unit_a = lcm / i;
    long long int unit_b = lcm / (i + 1);
    auto ap = S(-R, unit_a, 0, n - len);
    auto ap2 = S(R, unit_b, 1, len);
    if (ap.first > ap.second || ap2.first > ap2.second) continue;
    ap2.first = (R - (i + 1) * ap2.first) / i;
    ap2.second = (R - (i + 1) * ap2.second) / i;
    if (ap2.first > ap2.second) swap(ap2.first, ap2.second);
    long long int lef = max(ap.first, ap2.first);
    long long int rig = min(ap.second, ap2.second);
    if (lef > rig) continue;
    maxi = max(maxi, lef + (R - lef * i) / (i + 1));
    maxi = max(maxi, rig + (R - rig * i) / (i + 1));
  }
  printf("%lld\n", maxi);
  return 0;
}
