#include using ll = long long; #define MOD 1000000007 #define Mod 998244353 const int MAX = 1000000005; const long long INF = 1000000000000000005LL; using namespace std; int main() { ios::sync_with_stdio(0);cin.tie(); int N, P; cin >> N >> P; vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; ll now = 1; int M = 0; while (now < 1000000000) { M++; now *= P; } vector> C(M+1); now = 1; for (int i = 0; i <= M; i++) { for (int j = 0; j < N; j++) C[i][A[j] % now]++; now *= P; } ll ans = 0; now = P; for (int i = 1; i < M; i++) { for (int j = 0; j < N; j++) { ans += (ll)i * (C[i][A[j] % now] - C[i+1][A[j] % (now * P)]); C[i][A[j] % now]--; C[i+1][A[j] % (now * P)]--; } for (int j = 0; j < N; j++) C[i+1][A[j] % (now * P)]++; now *= P; } cout << ans << endl; }