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()))) from bisect import * def check(target): cnt = 0 for i in range(N): t = target - A[i] if t >= 0: cnt += bisect_right(B, t) cnt += bisect_right(B, t+P) - bisect_right(B, 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)