import sys from itertools import accumulate sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N,K,P = MI() A = LI() B = LI() count = [0]*P for b in B: count[b] += 1 S_count = list(accumulate(count)) ng = -1 ok = P-1 while ng+1 < ok: mid = (ng+ok)//2 c = 0 for i in range(N): a = A[i] if mid >= a: c += S_count[mid-a] c += S_count[min(P-1,P+mid-a)]-S_count[P-a-1] if c >= K: ok = mid else: ng = mid print(ok)