import bisect from functools import cache N,K,P = list(map(int,input().split())) A = list(map(int,input().split())) B = list(map(int,input().split())) A.sort() B.sort() @cache def memo_bisect_left(p): return bisect.bisect_left(B,p) @cache def memo_bisect_right(p): return bisect.bisect_right(B,p) def check(n): count = 0 for i in A: left = memo_bisect_left(P-i) right = memo_bisect_right(n+P-i) count += right - left right2 = memo_bisect_right(n-i) count += right2 return count >= K ng,ok = -1,P-1 while(ok - ng > 1): mid = (ok+ng)//2 if(check(mid)): ok = mid else: ng = mid print(ok)