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])) idx += n B.sort() len_B = n low = 0 high = p - 1 while low < high: mid = (low + high) // 2 total = 0 for a in A: L = (p - a) % p R = (mid - a) % p if R >= L: left = bisect.bisect_left(B, L) right = bisect.bisect_right(B, R) total += right - left else: left = bisect.bisect_left(B, L) part1 = len_B - left part2 = bisect.bisect_right(B, R) total += part1 + part2 if total >= k: break # Early exit if possible if total >= k: high = mid else: low = mid + 1 print(low) if __name__ == "__main__": main()