from itertools import count n,k,p = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) sA = [a%p for a in A] cum = [0]*p for b in B: cum[b%p] += 1 for i in range(p-1): cum[i+1] += cum[i] def count(x): ans = 0 for a in sA: ans += cum[(-a%p+x)%p] if (-a)%p != 0: ans -= cum[-a%p-1] if (-a%p+x)%p < -a%p: ans += cum[-1] return ans l = -1 r = p-1 while r > l+1: m = (r+l)//2 if count(m) < k: l = m else: r = m print(r)