#include <bits/stdc++.h>
using namespace std;
vector<long long> x, y;
void solve() {
  long long m, n, i, j, k, l;
  cin >> n >> m;
  long long a[n];
  for (i = 0; i < n; ++i) {
    cin >> a[i];
    a[i] %= m;
  }
  k = n / 2;
  for (i = 0; i < (1 << k); ++i) {
    long long sum = 0;
    for (j = 0; j < k; ++j) {
      if (i & (1 << j)) sum = (sum + a[j]) % m;
    }
    x.push_back(sum);
  }
  k = n - n / 2;
  for (i = 0; i < (1 << k); ++i) {
    long long sum = 0;
    for (j = 0; j < k; ++j) {
      if (i & (1 << j)) sum = (sum + a[j + n / 2]) % m;
    }
    y.push_back(sum);
  }
  sort(y.begin(), y.end());
  long long mx = 0;
  for (auto e : x) {
    int r = m - 1 - e;
    auto p = lower_bound(y.begin(), y.end(), r);
    if (p == y.end() || *p != r) --p;
    mx = max(mx, (*p + e) % m);
    if (mx == m - 1) break;
  }
  cout << mx;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
