#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int N = 100003;
const long long mod = 1e9 + 7;
unordered_set<int> s1;
vector<int> s2;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int a[n + 1], i, j;
  for (i = 1; i <= n; ++i) cin >> a[i], a[i] %= m;
  int lt = (1 << (n / 2)), k, l, sum;
  s1.insert(0);
  for (i = 1; i < lt; ++i) {
    j = i;
    k = 0, sum = 0;
    while (j) {
      if (j & 1) {
        sum += a[k + 1];
        if (sum >= m) sum -= m;
      }
      j >>= 1;
      ++k;
    }
    s1.insert(sum);
  }
  s2.push_back(0);
  lt = (1 << (n - n / 2));
  for (i = 1; i < lt; ++i) {
    j = i;
    k = 0, sum = 0;
    while (j) {
      if (j & 1) {
        sum += a[k + 1 + n / 2];
        if (sum >= m) sum -= m;
      }
      j >>= 1;
      ++k;
    }
    s2.push_back(sum);
  }
  sort(s2.begin(), s2.end());
  int ma = 0;
  lt = s2.size();
  for (auto it : s1) {
    j = lower_bound(s2.begin(), s2.end(), m - it) - s2.begin();
    --j;
    if (j >= 0 and j < lt) ma = max(ma, it + s2[j]);
    j = lower_bound(s2.begin(), s2.end(), 2 * m - it) - s2.begin();
    --j;
    if (j >= 0 and j < lt) ma = max(ma, (it + s2[j]) % m);
  }
  cout << ma;
  return 0;
}
