#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007, oo = 1000000009;
void Emsawy() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
vector<vector<int> > adj;
const long long N = 50 + 5;
int n, m, k, w, a, b;
int g[N];
vector<long long> l, r, now;
void go(int id, long long sum, bool ok) {
  if (id >= now.size()) {
    if (ok)
      l.push_back(sum);
    else
      r.push_back(sum);
    return;
  }
  go(id + 1, (sum + now[id]) % m, ok);
  go(id + 1, sum, ok);
}
int main() {
  Emsawy();
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) cin >> g[i];
    now.clear();
    for (int i = 0; i < n / 2; i++) {
      now.push_back(g[i]);
    }
    go(0, 0, 1);
    now.clear();
    for (int i = n / 2; i < n; i++) {
      now.push_back(g[i]);
    }
    go(0, 0, 0);
    long long ans = max(*max_element(((l).begin()), ((l).end())),
                        *max_element(((r).begin()), ((r).end())));
    sort(((r).begin()), ((r).end()));
    for (int i = 0; i < l.size(); i++) {
      int pos =
          upper_bound(((r).begin()), ((r).end()), m - l[i] - 1) - r.begin() - 1;
      if (pos >= 0) {
        ans = max(ans, l[i] + r[pos]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
