#include <bits/stdc++.h>
using namespace std;
int n, k, p;
const int N = 600000;
const int INF = 1e9;
int su[N], Pl[N], Pr[N], F[N], G[N];
int main() {
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; ++i) {
    int a;
    scanf("%d", &a);
    su[i] = (su[i - 1] + a) % p;
  }
  for (int j = 1; j <= n; ++j) G[j] = INF;
  for (int i = 1; i <= k; ++i) {
    for (int x = 1; x <= p; ++x) Pl[x] = Pr[x] = INF;
    for (int j = 0; j <= n; ++j) {
      F[j] = INF;
      for (int x = su[j] + 1; x; x -= x & (-x)) F[j] = min(F[j], Pl[x]);
      for (int x = su[j] + 2; x <= p; x += x & (-x))
        F[j] = min(F[j], Pr[x] + 1);
      for (int x = su[j] + 1; x <= p; x += x & (-x)) Pl[x] = min(Pl[x], G[j]);
      for (int x = su[j] + 1; x; x -= x & (-x)) Pr[x] = min(Pr[x], G[j]);
    }
    for (int j = 0; j <= n; ++j) swap(F[j], G[j]);
  }
  printf("%d\n", G[n] * p + su[n]);
}
