#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int *arr = new int[n];
  int maxim = 0;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    arr[i] %= m;
    maxim = max(maxim, arr[i]);
  }
  set<int> S1;
  for (int i = 0; i < (1 << (n / 2)); ++i) {
    int subset = i;
    int sum = 0;
    int bit = 0;
    while (subset > 0) {
      if (subset & 1) sum = (sum + arr[bit]) % m;
      subset >>= 1;
      ++bit;
    }
    S1.insert(sum);
  }
  set<int> S2;
  for (int i = 0; i < (1 << (n - n / 2)); ++i) {
    int subset = i;
    int sum = 0;
    int bit = 0;
    while (subset > 0) {
      if (subset & 1) sum = (sum + arr[n / 2 + bit]) % m;
      subset >>= 1;
      ++bit;
    }
    S2.insert(sum);
  }
  for (int first : S1) {
    auto second = S2.upper_bound(m - 1 - first);
    second--;
    if (second != S2.begin()) {
      maxim = max(maxim, first + *second);
    }
  }
  cout << maxim;
  return 0;
}
