#include <bits/stdc++.h>
using namespace std;
bool dbg = 0;
clock_t start_time = clock();
void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}
template <typename T>
string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}
template <typename T>
void amax(T& x, T y) {
  x = max(x, y);
}
template <typename T>
void amin(T& x, T y) {
  x = min(x, y);
}
template <typename T>
T input() {
  T ans = 0, m = 1;
  char c = ' ';
  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }
  if (c == '-') m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}
long long int gcd(long long int a, long long int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
template <typename T>
void read(T& a) {
  a = input<T>();
}
template <typename T>
void read(T& a, T& b) {
  read(a), read(b);
}
template <typename T>
void read(T& a, T& b, T& c) {
  read(a, b), read(c);
}
template <typename T>
void read(T& a, T& b, T& c, T& d) {
  read(a, b), read(c, d);
}
const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)2e3 + 12, base = 1e9 + 7;
const long long int llinf = 2e18 + 5;
template <typename T>
T binpow(T n, T second) {
  if (second <= 0) return 1LL;
  if (second % 2 == 0) {
    T b = binpow(n, second / 2);
    return (1LL * b * b) % base;
  } else {
    return (1LL * binpow(n, second - 1) * n) % base;
  }
}
long long int gcd(long long int a, long long int b, long long int& x,
                  long long int& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long int x1, y1;
  long long int d = gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
bool find_any_solution(long long int a, long long int b, long long int c,
                       long long int& x0, long long int& y0, long long int& g) {
  g = gcd(abs(a), abs(b), x0, y0);
  if (c % g != 0) return false;
  x0 *= c / g;
  y0 *= c / g;
  if (a < 0) x0 *= -1;
  if (b < 0) y0 *= -1;
  return true;
}
void shift_solution(long long int& x, long long int& y, long long int a,
                    long long int b, long long int cnt) {
  x += cnt * b;
  y -= cnt * a;
}
long long int be = -1;
void find_all_solutions(long long int a, long long int b, long long int c,
                        long long int minx, long long int maxx,
                        long long int miny, long long int maxy,
                        long long int add) {
  long long int x, y, g;
  if (!find_any_solution(a, b, c, x, y, g)) return;
  a /= g;
  b /= g;
  long long int sign_a = a > 0 ? +1 : -1;
  long long int sign_b = b > 0 ? +1 : -1;
  shift_solution(x, y, a, b, (minx - x) / b);
  if (x < minx) shift_solution(x, y, a, b, sign_b);
  if (x > maxx) return;
  long long int lx1 = x;
  shift_solution(x, y, a, b, (maxx - x) / b);
  if (x > maxx) shift_solution(x, y, a, b, -sign_b);
  long long int rx1 = x;
  shift_solution(x, y, a, b, -(miny - y) / a);
  if (y < miny) shift_solution(x, y, a, b, -sign_a);
  if (y > maxy) return;
  long long int lx2 = x;
  shift_solution(x, y, a, b, -(maxy - y) / a);
  if (y > maxy) shift_solution(x, y, a, b, sign_a);
  long long int rx2 = x;
  if (lx2 > rx2) swap(lx2, rx2);
  long long int lx = max(lx1, lx2);
  long long int rx = min(rx1, rx2);
  if (lx > rx) return;
  long long int ly = (c - a * lx) / b;
  long long int ry = (c - a * rx) / b;
  if (lx + ly + add > be) be = lx + ly + add;
  if (rx + ry + add > be) be = rx + ry + add;
  return;
}
long long int dist(long long int l, long long int r, long long int n) {
  if (l <= r) return r - l + 1;
  return r - l + 1 + n;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int n, l, r, k;
  cin >> n >> l >> r >> k;
  long long int d = dist(l, r, n);
  if (n <= 10000) {
    int be = -1;
    int N = n;
    int D = d;
    for (int a = 0; a <= D; a++)
      for (int b = 0; b <= N - D; b++) {
        if (k < a + D) break;
        if ((k - a - D) % (a + b + N) == 0) {
          if (be < a + b) be = a + b;
        }
      }
    for (int a = 0; a < D; a++)
      for (int b = 0; b <= N - D; b++) {
        if (k < a + D) break;
        if ((k - a - D) % (a + 1 + b + N) == 0) {
          if (be < (a + b + 1)) be = a + b + 1;
        }
      }
    cout << be << "\n";
    return 0;
  }
  if (d <= k && 2 * d >= k) {
    long long int a = min(d, k - d + 1);
    be = a + n - d;
  }
  long long int lmin = max(1ll, k / (2 * n) - 5);
  long long int lmax = k / n + 5;
  for (long long int l = lmin; l <= lmax; l++) {
    long long int c = k - d - l * n;
    if (c < 0) break;
    find_all_solutions(l + 1, l, c, 0, d, 0, n - d, 0);
    c -= l;
    if (c >= 0) {
      find_all_solutions(l + 1, l, c, 0, d - 1, 0, n - d, 1);
    }
  }
  cout << be << '\n';
  return 0;
}
