N,K=map(int,input().split()) A=list(map(int,input().split())) x=sum([A[-(i+1)]*(K**i) for i in range(N)]) y=sum(A) print(x%y)