#include using namespace std; using ll = long long; using ld = long double; using pll = pair; using tlll = tuple; constexpr ll MOD = 1e9 + 7; //constexpr ll MOD = 998244353; //constexpr ll MOD = ; ll mod(ll A, ll M) {return (A % M + M) % M;} constexpr ll INF = 1LL << 60; template bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;} template bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;} ll divceil(ll A, ll B) {return (A + (B - 1)) / B;} #define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false) ll N, K, P; vector A, B, S; void accum(vector &s) { s.resize(2 * P, 0); for (ll i = 0; i < N; i++) { s.at(A.at(i))++; s.at(P + A.at(i))++; } /* for (ll i = 0; i < 2 * P; i++) { cerr << i << " " << s.at(i) << endl; } //*/ for (ll i = 0; i < 2 * P - 1; i++) { s.at(i + 1) += s.at(i); } } ll count(ll t) { ll ret = 0; for (ll i = 0; i < N; i++) { ll tmp = S.at(P + t - B.at(i)) - S.at(P - B.at(i) - 1); ret += tmp; } return ret; } bool isok(ll t) { return count(t) >= K; } int main() { cin >> N >> K >> P; A.resize(N), B.resize(N); for (ll i = 0; i < N; i++) { cin >> A.at(i); } for (ll i = 0; i < N; i++) { cin >> B.at(i); } accum(S); /* for (ll i = 0; i < 2 * P; i++) { cerr << i << " " << S.at(i) << endl; } //*/ ll ng = -1, ok = P - 1; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isok(mid)) ok = mid; else ng = mid; } cout << ok << endl; /* for (ll p = 0; p < P; p++) { cerr << count(p) << endl; } //*/ }