N, K, P = map(int, input().split()) A = tuple(int(x) % P for x in input().split()) B = sorted(int(x) % P for x in input().split()) B_counter = [0] * P for b in B: B_counter[b] += 1 for i in range(1, P): B_counter[i] += B_counter[i - 1] def lower(x): ct = 0 for a in A: b1 = B_counter[x - a] if x - a >= 0 else 0 b2 = B_counter[P + x - a] if P + x - a < P else N assert 0 <= P - a - 1 < P ct += b1 + b2 - B_counter[P - a - 1] return ct l, r = -1, P while r - l > 1: m = (r + l) // 2 if lower(m) >= K: r = m else: l = m print(r)