import sys input = sys.stdin.readline N, K, P = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) bc = [0] * P for x in b: bc[x] += 1 cs = [0] * (P + 1) for i in range(P): cs[i + 1] = cs[i] + bc[i] def check(x): c = 0 for y in a: l = (P - y) % P r = (P - y + x) % P if l <= r: c += cs[r + 1] - cs[l] else: c += cs[-1] - cs[l] + cs[r + 1] return c >= K ok = P - 1 ng = -1 while ok - ng > 1: m = (ok + ng) // 2 if check(m): ok = m else: ng = m print(ok)