#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, a, b, temp, sum;
  cin >> n >> m;
  int arr[n];
  for (i = 0; i < n; i++) {
    cin >> temp;
    arr[i] = temp % m;
  }
  a = n / 2, b = n - a;
  set<int> s1, s2;
  for (i = 0; i < (1 << a); i++) {
    sum = 0;
    for (j = 0; j < a; j++) {
      if (i & (1 << j)) {
        sum = (sum + arr[j]) % m;
      }
    }
    s1.insert(sum);
  }
  for (i = 0; i < (1 << b); i++) {
    sum = 0;
    for (j = 0; j < b; j++) {
      if (i & (1 << j)) {
        sum = (sum + arr[a + j]) % m;
      }
    }
    s2.insert(sum);
  }
  int ans = 0, res;
  for (auto x : s1) {
    auto it = s2.lower_bound(m - 1 - x);
    if (*it != (m - 1 - x) && it != s2.begin() || it == s2.end()) {
      it--;
    }
    res = (x + (*it)) % m;
    if (res > ans) ans = res;
  }
  cout << ans << '\n';
}
