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