#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5;
const int inf = 200000000;
int n, m, p;
int s[maxn + 5];
int c[2][105];
int dp[2][maxn + 5];
int lowbit(int x) { return x & (-x); }
void add(int *c, int x, int data) {
  for (; x <= p; x += lowbit(x)) c[x] = min(c[x], data);
}
int query(int *c, int x) {
  int ans = inf;
  for (; x; x -= lowbit(x)) ans = min(ans, c[x]);
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    s[i] = (s[i - 1] + x) % p;
  }
  for (int i = 1; i <= n; ++i) ++s[i];
  int now = 0;
  for (int i = 1; i <= n; ++i) dp[now][i] = s[i] - 1;
  for (int k = 2; k <= m; ++k) {
    now ^= 1;
    for (int i = 0; i <= p; ++i) c[0][i] = c[1][i] = inf;
    for (int i = 1; i <= n; ++i) {
      dp[now][i] = s[i] + query(c[0], s[i]);
      dp[now][i] = min(dp[now][i], s[i] + query(c[1], p - s[i]));
      add(c[0], s[i], dp[now ^ 1][i] - s[i]),
          add(c[1], p + 1 - s[i], dp[now ^ 1][i] + p - s[i]);
    }
  }
  printf("%d\n", dp[now][n]);
  return 0;
}
