from itertools import accumulate N, K, P = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) ctA = [0]*P ctB = [0]*P for a, b in zip(A, B): ctA[a] += 1 ctB[b] += 1 acumB = [0]+list(accumulate(ctB)) def solve(v): ct = 0 for a in A: if v >= a: ct += acumB[v-a+1] ct += acumB[min(P, P+v-a+1)]-acumB[P-a] return ct >= K l, r = -1, P-1 while r-l > 1: m = (l+r)//2 if solve(m): r = m else: l = m print(r)