#include <bits/stdc++.h>
using namespace std;
int n, m, a[50], b[50], ans;
set<int> s1, s2;
void solve(int a[50], int n, set<int> &S) {
  for (int s = 0; s < (1 << n); s++) {
    int sum = 0;
    for (int i = 0; i < (n); i++)
      if (s >> i & 1) sum = (sum + a[i + 1]) % m;
    S.insert(sum);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int n1 = n / 2, n2 = n - n1;
  for (int i = 1; i <= (n1); i++) scanf("%d", &a[i]);
  for (int i = 1; i <= (n2); i++) scanf("%d", &b[i]);
  if (n == 1) return 0 * printf("%d\n", b[1] % m);
  solve(a, n1, s1);
  solve(b, n2, s2);
  for (__typeof((s1).begin()) it = (s1).begin(); it != (s1).end(); it++) {
    int v1 = *it, v2 = 0;
    set<int>::iterator iter = s2.lower_bound(m - v1);
    iter--;
    v2 = *iter;
    ans = max(ans, v1 + v2);
  }
  printf("%d\n", ans);
  return 0;
}
