N,K,mod=map(int,input().split()) B=list(map(int,input().split())) C=list(map(int,input().split())) posa=0 resta=B[0] count=0 score=0 for i in range(N): if count+B[i]>=K: posb=i restb=count+B[i]-K score+=C[i]*(K-count) score%=mod break else: count+=B[i] score+=C[i]*B[i] score%=mod result=0 from math import gcd while True: d=(C[posa]-C[posb])%mod n0=resta n1=restb+1 if n1<=n0: if d==0: if score==0: result+=n1 else: y=gcd(d,mod) if score%y==0: w=score//y t=d//y mod2=mod//y x=(w*pow(t,-1,mod2))%mod2 result+=(n1-1-x)//mod2+1 score-=C[posa]*n1 score+=C[posb]*(n1-1) if posb==N-1: break posb+=1 score+=C[posb] score%=mod restb=B[posb]-1 resta-=n1 else: if d==0: if score==0: result+=n0 else: y=gcd(d,mod) if score%y==0: w=score//y t=d//y mod2=mod//y x=(w*pow(t,-1,mod2))%mod2 result+=(n0-1-x)//mod2+1 score-=C[posa]*n0 score+=C[posb]*n0 posa+=1 restb-=n0 resta=B[posa] print(result)