import bisect def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 K = int(data[idx]) idx += 1 P = int(data[idx]) idx += 1 A = list(map(int, data[idx:idx+N])) idx += N B = list(map(int, data[idx:idx+N])) B.sort() def count_le(X): total = 0 for a in A: # Case 1: B_j <= X - a and B_j <= P-1 -a cnt1 = 0 if X >= a: max_case1 = min(X - a, (P-1) - a) if max_case1 >= 0: cnt1 = bisect.bisect_right(B, max_case1) # Case 2: B_j >= P - a and B_j <= X + P - a low_case2 = P - a high_case2 = X + P - a high_clamped = min(high_case2, P-1) cnt2 = 0 if low_case2 <= high_clamped: left = bisect.bisect_left(B, low_case2) right = bisect.bisect_right(B, high_clamped) cnt2 = right - left total += cnt1 + cnt2 return total low = 0 high = P - 1 answer = high while low <= high: mid = (low + high) // 2 cnt = count_le(mid) if cnt >= K: answer = mid high = mid - 1 else: low = mid + 1 print(answer) if __name__ == "__main__": main()