#include <bits/stdc++.h>
using namespace std;
unsigned long long int expo_fast(long long int a, long long int b) {
  a = a;
  long long int result = 1;
  while (b) {
    if (b & 1) result = (result * a);
    b >>= 1;
    a = (a * a);
  }
  return (result);
}
void take_in(vector<long long int> *arr) {
  for (int i = 0; i < arr->size(); i++) cin >> (*(arr))[i];
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
unsigned long long int power(long long int x, unsigned long long int y,
                             long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<long long int> arr1, arr2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  arr1.resize(n / 2);
  arr2.resize(0);
  for (int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    if (i < n / 2)
      arr1[i] = x;
    else
      arr2.push_back(x);
  }
  vector<long long int> sos;
  set<long long int> rhs;
  bitset<18> b;
  for (long long int i = 0; i < expo_fast(2, (n / 2)); i++) {
    b = i;
    long long int sum = 0;
    for (int j = 0; j < n / 2; j++) {
      sum += (b[j] * arr1[j]);
    }
    sos.push_back(sum % m);
  }
  for (long long int i = 0; i < expo_fast(2, n - (n / 2)); i++) {
    b = i;
    long long int sum = 0;
    for (int j = 0; j < arr2.size(); j++) {
      sum += (b[j] * arr2[j]);
    }
    rhs.insert(sum % m);
  }
  int n1 = sos.size();
  long long int ans = 0;
  for (long long int i = 0; i < n1; i++) {
    long long int x = sos[i];
    auto it = rhs.upper_bound(m - x - 1);
    it--;
    if (it == rhs.end()) {
      ans = max(ans, x);
      continue;
    }
    ans = max(ans, (x + *it) % m);
  }
  cout << ans;
}
