#include <bits/stdc++.h>   using namespace std;   long long n, x, k, lb, ub, ans = -1;   int main() {     long long l, r;     scanf( %lld%lld%lld%lld , &n, &l, &r, &k);     x = (r >= l ? r - l + 1 : r + n - l + 1) % n;     for (long long i = k, j, r; i; i = j) {       j = k / (k / i + 1);       r = k / i;       ub = min(2 * n, i);       lb = max(n, j + 1);       ub = min(ub, min((2 * n + k - 2 * x + 1) / (r + 1), (k - x) / r));       lb = max(lb, (k - 2 * x + r - 1) / r);       if (ub >= lb) ans = max(ans, ub);     }     if (x <= k && k <= x * 2) ans = max(ans, n + min(k - x, x - 1) + n - x + 1);     if (!x && k == n) ans = max(ans, n + 1);     printf( %lld n , ~ans ? ans - n : ans);   }   