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