#include <bits/stdc++.h>
using namespace std;
int A[2][35];
vector<int> V[2];
void f(int idx, int m, int N) {
  for (int i = 1; i <= (1 << N) - 1; i++) {
    int s = 0;
    for (int j = 0; j < N; j++) {
      if (i & (1 << j)) s = (s + A[idx][j]) % m;
    }
    V[idx].push_back(s);
  }
  sort(V[idx].begin(), V[idx].end());
}
int main() {
  int N, m;
  scanf("%d %d", &N, &m);
  int N0 = N / 2;
  int N1 = N - N0;
  for (int i = 0; i < N0; i++) {
    scanf("%d", &A[0][i]);
    A[0][i] = A[0][i] % m;
  }
  for (int i = 0; i < N1; i++) {
    scanf("%d", &A[1][i]);
    A[1][i] = A[1][i] % m;
  }
  f(0, m, N0 + 1);
  f(1, m, N1 + 1);
  int ans = max(V[0][V[0].size() - 1], V[1][V[1].size() - 1]);
  for (int i = 0; i < V[0].size(); i++) {
    int idx = lower_bound(V[1].begin(), V[1].end(), m - V[0][i]) - V[1].begin();
    if (--idx) {
      ans = max(ans, V[1][idx] + V[0][i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
