from bisect import bisect_left, bisect_right

n, k, p = map(int, input().split())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
alst.sort()
blst.sort()

def ok(x):
    cnt = 0
    for a in alst:
        b_min = p - a
        b_max = (x - a) % p
        p1 = bisect_right(blst, b_max)
        p2 = bisect_left(blst, b_min)
        if b_max >= b_min:
            cnt += p1 - p2
        else:
            cnt += p1 + n - p2
    
    return cnt >= k

l = -1
r = p - 1
while r - l > 1:
    mid = (r + l) // 2
    if ok(mid):
        r = mid
    else:
        l = mid
print(r)