N, K, P = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) def func(n): SUM = 0 for i in range(N): if A[i] <= n: SUM += cum[n-A[i]+1] if 1 <= A[i]: SUM += cum[-1]-cum[P-A[i]] if n < A[i]-1: SUM -= cum[-1]-cum[(n-A[i])%P+1] return SUM cum = [0]*(P+1) for i in range(N): cum[B[i]+1] += 1 for i in range(2, P+1): cum[i] += cum[i-1] left = -1 right = P-1 while left+1 < right: mid = (left+right)//2 if func(mid) < K: left = mid else: right = mid print(right)