N, K, M = map(int, input().split()) P = list(map(int, input().split())) E = list(map(int, input().split())) A = list(map(int, input().split())) H = list(map(int, input().split())) P.sort() E.sort() A.sort() H.sort() D = 0 for i in range(N): D = (D + pow(max(P[0], E[0], A[0], H[0]) - min(P.pop(0), E.pop(0), A.pop(0), H.pop(0)), K, M)) % M print(D)