import sys input = lambda: sys.stdin.readline().strip() import bisect N,K,P = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) R = [0 for i in range(P)] for a in A: R[a] += 1 for i in range(P-1): R[i+1] += R[i] 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 if m - b >= 0: res += R[m-b] x = min(P + m - b,P - 1) res += (R[x] - R[P-1-b]) if res < K: l = m else: r = m print(r)