#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
set<unsigned long long> val[2];
unsigned long long n, m;
void sos(vector<unsigned long long>& arr, unsigned long long n,
         unsigned long long idx, unsigned long long sum, unsigned long long t) {
  if (idx == n) {
    val[t].insert(sum);
    return;
  }
  sos(arr, n, idx + 1, sum, t);
  sos(arr, n, idx + 1, (sum + arr[idx]) % m, t);
}
int main() {
  fast();
  cin >> n >> m;
  vector<unsigned long long> arr(n);
  for (unsigned long long i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i] %= m;
  }
  sos(arr, n / 2 + n % 2, 0, 0, 0);
  sos(arr, n, n / 2 + n % 2, 0, 1);
  unsigned long long ans = 0;
  for (set<unsigned long long>::iterator itr = val[0].begin();
       itr != val[0].end(); ++itr) {
    unsigned long long ele = *itr;
    set<unsigned long long>::iterator it = val[1].lower_bound(m - ele);
    if (it == val[1].begin()) continue;
    it--;
    ans = max(ans, (ele + *it) % m);
  }
  cout << ans;
}
