#include #define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i) #define rep(i, n) For(i, 0, (n)) #define rrep(i, n) rFor(i, (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair pii; typedef pair pll; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template struct coord_comp { vector v; bool sorted = false; coord_comp() {} int size() { return v.size(); } void add(T x) { v.push_back(x); } void build() { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); sorted = true; } int get_idx(T x) { assert(sorted); return lower_bound(v.begin(), v.end(), x) - v.begin(); } T &operator[](int i) { return v[i]; } }; constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 200010; constexpr int MAX2 = 3 * 50 * MAX; int n, P, a[MAX], b[MAX], cnt[MAX2]; lint K; bool check(int x) { lint tmp = 0; rep(i, n) tmp += cnt[x + P - a[i] + 1] - cnt[P - a[i]]; return tmp >= K; } int main() { scanf("%d%lld%d", &n, &K, &P); rep(i, n) scanf("%d", &a[i]); rep(i, n) { scanf("%d", &b[i]); ++cnt[b[i] + 1]; ++cnt[b[i] + 1 + P]; ++cnt[b[i] + 1 + P * 2]; } partial_sum(cnt, cnt + P * 3, cnt); if (check(0)) { printf("%d\n", 0); return 0; } int low = 0, high = P - 1; while (high - low > 1) { int mid = (high + low) / 2; (check(mid) ? high : low) = mid; } printf("%d\n", high); }