import numpy as np N, K, P = map(int, input().split()) A = np.array(input().split(), dtype=np.int32) % P B = np.array(input().split(), dtype=np.int32) % P Fa = np.bincount(A) Fb = np.bincount(B) sz = 2 * P fft_len = 1 << (sz - 1).bit_length() Ca = np.fft.rfft(Fa, fft_len) Cb = np.fft.rfft(Fb, fft_len) G = np.fft.irfft(Ca * Cb, fft_len)[:sz] G = np.rint(G).astype(np.int64) G = (G[:P] + G[P:]).cumsum() ans = np.searchsorted(G, K) print(ans)