#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, a;
  scanf("%lld %lld", &n, &m);
  vector<long long> a1, a2;
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &a);
    a %= m;
    if (i % 2)
      a2.push_back(a);
    else
      a1.push_back(a);
  }
  long long i, j, sum;
  set<long long> s1, s2;
  n = a1.size();
  for (i = 0; i < (1 << n); i++) {
    sum = 0;
    for (j = n - 1; j >= 0; j--) {
      if (i & (1 << j)) sum += a1[j];
    }
    s1.insert(sum % m);
  }
  n = a2.size();
  for (i = 0; i < (1 << n); i++) {
    sum = 0;
    for (j = n - 1; j >= 0; j--) {
      if (i & (1 << j)) sum += a2[j];
    }
    s2.insert(sum % m);
  }
  set<long long>::iterator it, pt;
  long long ans = 0;
  for (it = s1.begin(); it != s1.end(); it++) {
    sum = *it;
    pt = s2.lower_bound(m - sum);
    pt--;
    ans = max(ans, (sum + *pt) % m);
    pt = s2.end();
    pt--;
    ans = max(ans, (sum + *pt) % m);
  }
  cout << ans << endl;
  return 0;
}
