N,K,P = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) A.sort() B.sort() from collections import Counter c = Counter(B) F = [0]*(P+1) for i in range(1,P): F[i+1] = F[i] + c[i] def is_ok(x): tmp = 0 for a in A: if 0<=x-a<=P-1: tmp += F[x-a+1] - F[0] if P-a <= P-1: tmp += F[min(P-1,P+x-a)+1] - F[P-a] return tmp >= K y = P-1 x = -1 while y-x>1: mid = (y+x)//2 if is_ok(mid): y = mid else: x = mid print(y)