#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <functional>
#include <algorithm>
#include <random>
#include <cmath>
#include <climits>
#include <iomanip>
#include <cfloat>
#include <set>
int main() {
	int n, m; std::cin >> n >> m;
	while (n != 0) {
		std::vector<long long int> scone(n);
		for (auto &s : scone) std::cin >> s;
		scone[0] %= m;
		for (auto i = 1; i < n; ++i) scone[i] = (scone[i] + scone[i - 1]) % m;
		std::set<long long int> set;
		long long int result = 0;
		for (const auto s : scone) {
			set.insert(s);
			auto upper = set.upper_bound(s);
			if (upper != set.end()) {
				if (result < m - (*upper) + s) result = m - (*upper) + s;
			}
			else {
				if (result < s) result = s;
			}
		}
		std::cout << result << std::endl;
		std::cin >> n >> m;
	}
}
