from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor,atan2,sin,cos from operator import mul from functools import reduce from pprint import pprint sys.setrecursionlimit(2147483647) INF = 10 ** 13 def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 10**9+7 n,k,p=LI() A=LI() B=LI() B=[b%p for b in B] A=[a%p for a in A] A.sort() B.sort() B+=[INF] ok=p ng=0 D={0:0} j=0 for i in range(n+1): while j
1: mid=(ok+ng)//2 r=n-1 m = 0 for i in range(n): if mid>A[i]: m+=n-(D[p-A[i]]-D[mid-A[i]+1]) else: m+=(D[p-A[i]+mid+1]-D[p-A[i]]) if m>=k: ok=mid else: ng=mid print(ok)