""" K番目に来る値はX以下か? NONONONONOYESYESYESYES ↑ X以下の値の数を求めてK以上か確かめればよい """ import bisect from sys import stdin import sys N,K,P = map(int,stdin.readline().split()) A = list(map(int,stdin.readline().split())) B = list(map(int,stdin.readline().split())) for i in range(N): A[i] %= P B[i] %= P A.sort() B.sort() #print (A,B,file=sys.stderr) l = -1 r = P-1 while r-l != 1: MID = (l+r)//2 ans = 0 for i in range(N): ZR = (P - A[i]) % P ZIND = bisect.bisect_left(B,ZR) MR = (MID-A[i]) % P MIND = bisect.bisect_right(B,MR) if ZIND == MIND: if (A[0]+B[0]) % P <= MID: ans += N else: ans += 0 else: ans += (MIND-ZIND) % N if ans >= K: r = MID else: l = MID print (r)