########関数部分############## def Base_n_to_10(X,n): out = 0 for i in range(len(X)): x = X[i] out += x*(n**i) return out#int out ############################ N,K = map(int,input().split()) A = list(map(int,input().split())) sumA = sum(A) A.reverse() numA = Base_n_to_10(A,K) print(numA%sumA)