#include <bits/stdc++.h>
using namespace std;
int n, k, p;
int s[500005], f[500005][105], g[500005];
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  return x * f;
}
int main() {
  n = read(), k = read(), p = read();
  for (int i = 1; i <= n; i++) s[i] = (s[i - 1] + read()) % p;
  if (n >= k * p) {
    memset(g, 0x3f, sizeof(g));
    for (int i = 1; i <= n; i++) *upper_bound(g + 1, g + k + 1, s[i]) = s[i];
    cout << (g[k] <= s[n] ? s[n] : s[n] + p) << endl;
  } else {
    memset(f, 0x3f, sizeof(f)), f[0][0] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = k; j >= 1; j--)
        for (int l = 0; l < p; l++)
          f[s[i]][j] = min(f[s[i]][j], f[l][j - 1] + (s[i] - l + p) % p);
    cout << f[s[n]][k] << endl;
  }
  return 0;
}
