#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <queue>

using namespace std;

int main(){
	
	int n, m;
	
	while(true){
		
		cin >> n >> m;
		
		if(n == 0 && m == 0){
			break;
		}
		
		int p[200000];
		
		long long int sum = 0;
		
		for(int i = 0; i < n; i++){
			cin >> p[i];
			sum += p[i];
		}
		
		sort(p, p + n);
		
		for(int i = n - m; i >= 0; i -= m){
			sum -= p[i];
		}
		
		cout << sum << endl;
		
	}
	
	return 0;
}
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
    int N, M; cin >> N >> M;
    vector<int> A(N);
    REP(i, 0, N) cin >> A[i];

    int modsum = 0; unordered_map<int, int> m;
    REP(i, 0, N) {
        modsum = (modsum + A[i]) % M;
        m[modsum]++;
    }

    ll ans = 0; modsum = 0;
    REP(i, 0, N) {
        ans += m[modsum];
        modsum = (modsum + A[i]) % M;
        m[modsum]--;
    }
    cout << ans << endl;

    return 0;
}