#include <bits/stdc++.h>
using namespace std;
long long n, m, a[50], maxx;
vector<long long> v1, v2, v3, v4;
map<long long, bool> mp;
void dfs(long long sum, long long deep, int type) {
  if (deep == (type ? v1.size() : v2.size())) {
    if (!mp[sum]) {
      mp[sum] = 1;
      type ? v3.push_back(sum) : v4.push_back(sum);
    }
    return;
  }
  dfs(sum, deep + 1, type);
  dfs((sum + (type ? v1[deep] : v2[deep])) % m, deep + 1, type);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    i > n / 2 ? v1.push_back(a[i]) : v2.push_back(a[i]);
  }
  dfs(0, 0, 1);
  mp.clear();
  dfs(0, 0, 0);
  sort(v3.begin(), v3.end());
  sort(v4.begin(), v4.end());
  maxx = max(v3[v3.size() - 1], v4[v4.size() - 1]);
  for (int i = 0; i < v3.size(); i++) {
    maxx = max(maxx, ((v3[i] + v4[v4.size() - 1]) % m));
    maxx =
        max(maxx,
            (v3[i] + *(lower_bound(v4.begin(), v4.end(), m - v3[i]) - 1)) % m);
  }
  cout << maxx;
}
