#include // #include // #include using namespace std; // using namespace atcoder; // using bint = boost::multiprecision::cpp_int; using ll = long long; using ull = unsigned long long; using P = pair; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 int main(){ ll n,K,p; cin >> n >> K >> p; vector a(n),b(n); rep(i,n)cin >> a[i],a[i] %= p; rep(i,n)cin >> b[i],b[i] %= p; sort(ALL(a)); sort(ALL(b)); ll l = -1,r = p; while(r-l > 1){ ll mid = (l+r)/2; ll cnt = 0; rep(i,n){ int mn = mid-a[i],mx = p + mid - a[i]; { auto iter = lower_bound(ALL(b),mn+1); cnt += iter - b.begin(); } { auto ml = lower_bound(ALL(b),p-a[i]); auto mr = lower_bound(ALL(b),p+mid-a[i]+1); cnt += mr-ml; } } if(cnt >= K)r = mid; else l = mid; } cout << r << "\n"; return 0; }