#include <bits/stdc++.h>
const int maxn = 18;
int M, N, s1[1 << maxn], s2[1 << maxn], mid, arr[40], u1, u2, ans;
inline int max(int a, int b) { return a > b ? a : b; }
inline int query(int x) {
  int l = 0, r = u2, res = 0;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (x + s2[m] < M) {
      res = x + s2[m];
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  return res;
}
int main() {
  scanf("%d%d", &N, &M);
  mid = (N + 1) / 2;
  for (int i = 1; i <= N; i++) scanf("%d", &arr[i]);
  if (N == 1) {
    printf("%d\n", arr[1] % M);
  } else {
    u1 = (1 << mid) - 1;
    for (int i = 0; i <= u1; i++) {
      for (int j = 1; j <= mid; j++) {
        if (i & (1 << (j - 1))) {
          continue;
        }
        s1[i | (1 << (j - 1))] = ((long long)s1[i] + (long long)arr[j]) % M;
        ans = max(ans, s1[i | (1 << (j - 1))]);
      }
    }
    u2 = (1 << (N - mid)) - 1;
    for (int i = 0; i <= u2; i++) {
      for (int j = mid + 1; j <= N; j++) {
        if (i & (1 << (j - mid - 1))) {
          continue;
        }
        s2[i | (1 << (j - mid - 1))] =
            ((long long)s2[i] + (long long)arr[j]) % M;
        ans = max(s2[i | (1 << (j - mid - 1))], ans);
      }
    }
    std::sort(s1, s1 + 1 + u1), std::sort(s2, s2 + 1 + u2);
    for (int i = 0; i <= u1; i++) {
      ans = max(ans, query(s1[i]));
      ans = max(ans, ((long long)s1[i] + (long long)s2[u2]) % M);
      if (ans == M - 1) {
        break;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
