import bisect def find_kth_smallest(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]); idx +=1 K = int(input[idx]); idx +=1 P = int(input[idx]); idx +=1 A = list(map(int, input[idx:idx+N])) idx +=N B = list(map(int, input[idx:idx+N])) idx +=N B.sort() low = 0 high = P - 1 ans = P - 1 while low <= high: mid = (low + high) // 2 total = 0 for a in A: # Case 1: B_j <= mid - a (if mid >= a) if mid >= a: target = mid - a case1 = bisect.bisect_right(B, target) else: case1 = 0 # Case 2: B_j in [P - a, mid + P - a] (clamped to P-1) lower = P - a upper = mid + P - a upper = min(upper, P - 1) if lower > upper: case2 = 0 else: left = bisect.bisect_left(B, lower) right = bisect.bisect_right(B, upper) case2 = right - left total += case1 + case2 if total > K * 2: # Early exit if possible to avoid overflow break if total >= K: ans = mid high = mid - 1 else: low = mid + 1 print(ans) find_kth_smallest()