#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
int f[105][11005];
int nxt[500005][105];
int a[500005], pre[500005];
inline void Upd(int &x, int y) { x = (x <= y) ? x : y; }
int main() {
  memset(f, 63, sizeof(f));
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &a[i]);
    a[i] %= p;
    pre[i] = (pre[i - 1] + a[i]) % p;
  }
  for (int i = (0); i <= (p - 1); i++) {
    int nt = n + 1;
    for (int j = (n); j >= (0); j--) {
      nxt[j][i] = nt;
      if (pre[j] == i) nt = j;
    }
  }
  f[0][0] = 0;
  for (int i = (0); i <= (k - 2); i++)
    for (int j = (0); j <= ((i + 1) * p); j++)
      if (f[i][j] < n) {
        for (int k = (0); k <= (p - 1); k++) {
          Upd(f[i + 1][j + k], nxt[f[i][j]][(k + pre[f[i][j]] + p) % p]);
        }
      }
  int ans = inf;
  for (int i = (0); i <= (k * p); i++)
    if (f[k - 1][i] < n) {
      Upd(ans, i + ((pre[n] - pre[f[k - 1][i]] + p) % p));
    }
  printf("%d\n", ans);
  return 0;
}
