#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int n, m;
	while(cin >> n >> m, n || m) {
		set<int> s;
		int sum = 0, res = 0;

		s.insert(0);
		for(int i = 0; i < n; i++) {
			unsigned int a;
			set<int>::iterator it;
			cin >> a;
			sum = (sum + a) % m;
			it = s.upper_bound(sum);
			if(it == s.end())
				res = max(res, sum);
			else
				res = max(res, (sum - *it + m) % m);
			s.insert(sum);
		}
		cout << res << endl;
	}
}