#include using namespace std; typedef long long int ll; typedef pair P; typedef vector VI; typedef vector VVI; #define REP(i,n) for(ll i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() template bool chmax(T &x, const T &y) {return (x bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;}; constexpr ll MOD=1000000007; constexpr ll INF=2e18; int main(){ ll n, k, p; cin >> n >> k >> p; VI a(n), b(n); REP(i,n) cin >> a[i], a[i]%=p; REP(i,n) cin >> b[i], b[i]%=p; sort(ALL(b)); ll l=-1, r=p; while(r-l>1){ ll m=(l+r)/2; ll sum=0; REP(i,n){ if(a[i]<=m) sum+=upper_bound(ALL(b),m-a[i])-b.begin(); sum+=upper_bound(ALL(b),p-max(0LL,a[i]-m))-lower_bound(ALL(b),p-a[i]); //cout << a[i] << " " << p-max(0LL,a[i]-m) << " " << p-a[i] << endl; } //cout << m << " " << sum << endl; if(sum>=k) r=m; else l=m; } cout << r << endl; return 0; }