import sys,os,io input = sys.stdin.readline #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline N, K, P = map(int, input().split()) A = sorted(list(map(int, input().split()))) B = sorted(list(map(int, input().split()))) lis = [0]*P for i in range(N): lis[B[i]] += 1 for i in range(1,P): lis[i] += lis[i-1] from bisect import * def check(target): cnt = 0 for i in range(N): t = target - A[i] if t >= 0: cnt += lis[t] cnt += lis[min(P-1, t+P)] - lis[P-1-A[i]] return cnt >= K low,high = -1,P-1 mid = (low+high)//2 while high-low>1: if check(mid): high = mid else: low = mid mid = (low+high)//2 ans = high print(ans)