#include <bits/stdc++.h>
using namespace std;
void exgcd(long long x, long long y, long long &a, long long &b) {
  if (!y) {
    a = 1;
    b = 0;
  } else {
    exgcd(y, x % y, b, a);
    b -= x / y * a;
  }
}
long long my_div(long long x, long long y) {
  if (!y) {
    return 1ll << 60;
  } else {
    return x >= 0 ? (x + y - 1) / y : x / y;
  }
}
long long solve(long long x, long long y, long long n, long long m, int type) {
  if (m < x + type) {
    return -1;
  }
  m -= x;
  if (n <= 1e6) {
    for (int answer = n; ~answer; --answer) {
      int each = answer + n;
      long long t = my_div(m - min(x, m), each);
      while (t * each <= m - type) {
        long long a = m - t * each, b = answer - a;
        if (b >= 0 && b <= y) {
          return answer;
        }
        ++t;
      }
    }
    return -1;
  } else {
    long long answer = -1;
    for (int t = 0; n * t <= m; ++t) {
      long long c = m - n * t;
      if (!c) {
        if (!type) {
          answer = max(answer, t ? 0ll : y);
        }
      } else {
        long long a, b;
        exgcd(t + 1, t, a, b);
        a *= c;
        b *= c;
        if (!t) {
          if (a <= min(x, m) && a >= type) {
            answer = max(answer, a + y);
          }
          continue;
        }
        long long min_k = max(my_div(type - a, t), my_div(b - y, t + 1));
        if (a + min_k * t <= min(x, m) && b - min_k * (t + 1) >= 0) {
          answer = max(answer, a + b - min_k);
        }
      }
    }
    return answer;
  }
}
int main() {
  long long n, m, l, r;
  cin >> n >> l >> r >> m;
  long long x = (r - l + n) % n + 1, y = n - x;
  cout << max(solve(x, y, n, m, 0), solve(x, y, n, m + 1, 1)) << endl;
  return 0;
}
