n,k=map(int,input().split()) A=list(map(int,input().split())) x=0 for i in range(n): x+=A[i]*(pow(k,n-i-1)) ans=x%sum(A) print(ans)