mod = 1000000007 eps = 10**-9 def main(): import sys from bisect import bisect_left input = sys.stdin.readline N, K, P = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) A = [a % P for a in A] B = [b % P for b in B] A.sort() B.sort() ok = P-1 ng = -1 mid = (ok + ng) // 2 while ok - ng > 1: cnt = 0 for a in A: cnt += bisect_left(B, mid - a + 1) cnt += bisect_left(B, P + mid - a + 1) - bisect_left(B, P - a) if cnt >= K: ok = mid else: ng = mid mid = (ok + ng) // 2 print(ok) if __name__ == '__main__': main()