def General_Binary_Increase_Search_Integer(L,R,cond,default=None): """条件式が単調増加であるとき, 整数上で二部探索を行う. L: 解の下限 R: 解の上限 cond: 条件(1変数関数, 広義単調増加を満たす) default: Rで条件を満たさないときの返り値 """ if not(cond(R)): return default if cond(L): return L R+=1 while R-L>1: C=L+(R-L)//2 if cond(C): R=C else: L=C return R #================================================= def cum(A,l,r): if l: return A[r]-A[l-1] else: return A[r] #================================================= def check(x): M=0 for a in A: l=max(0,-a); r=min(P-1,x-a) if l<=r: M+=cum(B_cum,l,r) l=max(0,P-a); r=min(P-1,P+x-a) if l<=r: M+=cum(B_cum,l,r) return M>=K #================================================= N,K,P=map(int,input().split()) A=list(map(int,input().split())) B=list(map(int,input().split())) B_cnt=[0]*P for b in B: B_cnt[b]+=1 B_cum=[0]*(2*P); B_cum[0]=B[0] for i in range(2*P): B_cum[i]=B_cum[i-1]+B_cnt[i%P] print(General_Binary_Increase_Search_Integer(0,P-1,check))