#include <bits/stdc++.h>
using namespace std;
const int delta = (int)1e9 + 7;
long long n, m, x, la, lb, a[20], b[20], ans, p;
vector<int> v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    x %= m;
    if (la < (n / 2))
      a[la++] = x;
    else
      b[lb++] = x;
  }
  for (int i = 0; i < (1 << lb); ++i) {
    x = 0;
    for (int j = 0; j < lb; ++j) {
      if (i & 1 << j) x = (x + b[j]) % m;
    }
    v.push_back(x);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < (1 << la); ++i) {
    x = 0;
    for (int j = 0; j < la; ++j) {
      if (i & 1 << j) x = (x + a[j]) % m;
    }
    p = upper_bound(v.begin(), v.end(), m - x - 1) - v.begin() - 1;
    ans = max(ans, (x + v[p]) % m);
  }
  cout << ans;
  return cout << endl, 0;
}
