n, k, p = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) cnt = [0] * (p + 1) for i in a: cnt[i + 1] += 1 for i in range(p): cnt[i + 1] += cnt[i] def count(l, r): return cnt[r] - cnt[l] def calc(m): res = 0 for i in b: res += count(0, max(m - i, 0)) + count(p - i, min(p, p + m - i)) return res ng, ok = p, -1 while ng - ok > 1: m = (ok + ng) // 2 if calc(m) < k: ok = m else: ng = m print(ok)