#include <bits/stdc++.h>
using namespace std;
int M;
int add(int a, int b) { return a + b < M ? a + b : a + b - M; }
void subsets(vector<int> &a, vector<int> &b, int n, int m, int s) {
  for (int i = 0; i < int(n); i++) b[1 << i] = a[i + s];
  for (int i = 0; i < int(m); i++) b[i] = add(b[i ^ (i & -i)], b[i & -i]);
}
int max_subset_sum(vector<int> &a, int v) {
  int n = int((a).size()), m = n / 2;
  int sx = 1 << m, sy = 1 << (n - m);
  vector<int> x(sx), y(sy);
  subsets(a, x, m, sx, 0);
  subsets(a, y, n - m, sy, m);
  sort(y.begin(), y.end());
  int res = 0;
  for (int i = 0; i < int(sx); i++)
    if (x[i] <= v) {
      int p = int(upper_bound(y.begin(), y.end(), v - x[i]) - y.begin() - 1);
      res = max(res, x[i] + y[p]);
    }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n >> M;
  vector<int> a(n);
  for (int i = 0; i < int(n); i++) cin >> a[i], a[i] %= M;
  cout << max_subset_sum(a, M - 1) << endl;
  return 0;
}
