import sys input = sys.stdin.readline def judge(x): cnt = 0 for Ai in A: if x>=Ai: cnt += acc[x-Ai+1]+acc[P]-acc[P-Ai] else: cnt += acc[P-Ai+x+1]-acc[P-Ai] return cnt>=K def binary_search(): l, r = 0, P-1 while l<=r: m = (l+r)//2 if judge(m): r = m-1 else: l = m+1 return l N, K, P = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [0]*P for Bi in B: C[Bi] += 1 acc = [0] for Ci in C: acc.append(acc[-1]+Ci) print(binary_search())