from bisect import bisect_left, bisect_right N, K, P = map(int, input().split()) As = sorted(map(int, input().split())) Bs = sorted(map(int, input().split())) def isok(m): cnt = 0 p = N q = N r = N for A in As: while p > 0 and m - A < Bs[p - 1]: p -= 1 while q > 0 and m - A + P < Bs[q - 1]: q -= 1 while r > 0 and - A + P <= Bs[r - 1]: r -= 1 cnt += p + q - r # print(m ,cnt) return cnt < K ok = -1 ng = P while ng - ok > 1: mid = (ok + ng) // 2 if isok(mid): ok = mid else: ng = mid print(ok + 1)