#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
long long int t, n, m, k, l, r, x, y;
inline long long int check1(long long int &a, long long int &b,
                            long long int &c, int &i) {
  if (!(a + i * c >= 0 && a + i * c <= m)) return -1;
  if (!(b - a - (i + 1) * c >= 0 && b - a - (i + 1) * c <= n - m)) return -1;
  return b - c;
}
inline long long int check2(long long int &a, long long int &b,
                            long long int &c, int &i) {
  if (!(a + i * c >= 1 && a + i * c <= m)) return -1;
  if (!(b - a - (i + 1) * c >= 0 && b - a - (i + 1) * c <= n - m)) return -1;
  return b - c;
}
long long int solve() {
  long long int ans = -1;
  if (k < 0) return ans;
  if (n < 5e3) {
    for (int i = 0; i < n - m + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        if ((k + i + n) % (i + j + n) == 0 && k >= j) {
          ans = max(ans, (long long int)i + j);
          if (ans == 6007) {
            cout << "i"
                 << " = " << i << "\n";
            cout << "j"
                 << " = " << j << "\n";
          }
        }
        if (j > 0 && (k + 1 + i + n) % (i + j + n) == 0 && k + 1 >= j) {
          ans = max(ans, (long long int)i + j);
          if (ans == 6007) {
            cout << "i"
                 << " = " << i << "\n";
            cout << "j"
                 << " = " << j << "\n";
          }
        }
      }
    }
  } else {
    for (int i = max(k / (2 * n) - 1, 0LL); i < k / n + 2; i++) {
      t = k - i * n;
      if (t >= 0) {
        if (i > 0) {
          long long int alpha = t % i, beta = t / i;
          long long int gammaMn =
              max(ceil(-alpha / (double)i),
                  ceil((beta - alpha + m - n) / (double)(i + 1)));
          long long int gammaMx = min(floor((m - alpha) / (double)i),
                                      floor((beta - alpha) / (double)(i + 1)));
          if (gammaMx < gammaMn) continue;
          ans = max(ans, check1(alpha, beta, gammaMn, i));
          ans = max(ans, check1(alpha, beta, gammaMx, i));
          gammaMx++;
          gammaMn--;
          ans = max(ans, check1(alpha, beta, gammaMx, i));
          ans = max(ans, check1(alpha, beta, gammaMn, i));
        } else if (i == 0 && t <= m) {
          ans = max(ans, t + n - m);
        }
      }
      t++;
      if (t >= 0 && m > 0) {
        if (i > 0) {
          long long int alpha = t % i, beta = t / i;
          long long int gammaMn =
              max(ceil(-alpha / (double)i),
                  ceil((beta - alpha + m - n) / (double)(i + 1)));
          long long int gammaMx = min(floor((m - alpha) / (double)i),
                                      floor((beta - alpha) / (double)(i + 1)));
          if (gammaMx < gammaMn) continue;
          ans = max(ans, check2(alpha, beta, gammaMn, i));
          ans = max(ans, check2(alpha, beta, gammaMx, i));
          gammaMx++;
          gammaMn--;
          ans = max(ans, check2(alpha, beta, gammaMx, i));
          ans = max(ans, check2(alpha, beta, gammaMn, i));
        } else if (i == 0 && t > 0 && t <= m) {
          ans = max(ans, t + n - m);
        }
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> l >> r >> k;
  r -= l;
  if (r < 0) r += n;
  m = r + 1;
  k -= m;
  long long int ans = -1;
  ans = max(ans, solve());
  cout << ans << "\n";
  return 0;
}
