import bisect N,K,P = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) A.sort() B.sort() l = -1 r = P - 1 while r - l > 1: m = (l + r) // 2 res = 0 for b in B: # a + b は 0 ~ 2*P - 1. m,m+P c = bisect.bisect_right(A,m-b) res += c c = bisect.bisect_right(A,m+P-b) - bisect.bisect_right(A,P-1-b) res += c if res < K: l = m else: r = m print(r)