#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; ll n,k,p; ll A[100010],B[100100],C[200200]; bool f(ll x){ ll cnt=0; rep(i,n){ cnt+=upper_bound(C,C+2*n,p-A[i]+x)-lower_bound(C,C+2*n,p-A[i]); } if(cnt>=k) return true; else return false; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cin>>n>>k>>p; rep(i,n) cin>>A[i]; rep(i,n) cin>>B[i]; sort(A,A+n); sort(B,B+n); rep(i,n){ C[i]=B[i],C[i+n]=B[i]+p; } ll ng=-1,ok=p+1; while(ok-ng>1){ ll mid=(ng+ok)/2; if(f(mid)) ok=mid; else ng=mid; } cout<