#include <bits/stdc++.h>
using namespace std;
namespace solve1 {
const int maxn = 10005, maxm = 105;
int f[2][maxm][maxm], a[maxn], cur, best = 1e9;
void main(int n, int K, int P) {
  for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
  memset(f, 0x3f, sizeof f);
  f[1][1][a[1] % P] = a[1] % P;
  for (int i = (2); i <= (n); i++) {
    cur = i & 1;
    memset(f[cur], 0x3f, sizeof f[cur]);
    for (int j = (1); j <= (K); j++)
      for (int k = (0); k <= (P - 1); k++) {
        ((f[cur ^ 1][j][k] - k + (k + a[i]) % P < f[cur][j][(k + a[i]) % P])
             ? f[cur][j][(k + a[i]) % P] = f[cur ^ 1][j][k] - k + (k + a[i]) % P
             : 0);
        if (j < K)
          ((f[cur ^ 1][j][k] + a[i] % P < f[cur][j + 1][a[i] % P])
               ? f[cur][j + 1][a[i] % P] = f[cur ^ 1][j][k] + a[i] % P
               : 0);
      }
  }
  for (int j = (0); j <= (P - 1); j++)
    ((f[n & 1][K][j] < best) ? best = f[n & 1][K][j] : 0);
  printf("%d\n", best);
}
}  // namespace solve1
namespace solve2 {
const int maxn = 500005;
int a[maxn], c[maxn], f;
void update(int x, int y) {
  for (++x; x <= 100; x += ((x) & (-(x)))) ((y > c[x]) ? c[x] = y : 0);
}
int query(int x) {
  int res = 0;
  for (++x; x; x -= ((x) & (-(x)))) ((c[x] > res) ? res = c[x] : 0);
  return res;
}
void main(int n, int K, int P) {
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &a[i]);
    a[i] = (a[i] + a[i - 1]) % P;
  }
  for (int i = (1); i <= (n); i++) {
    f = query(a[i]) + 1;
    update(a[i], f);
  }
  if (f >= K - 1)
    printf("%d\n", a[n]);
  else
    printf("%d\n", a[n] + P);
}
}  // namespace solve2
int main() {
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  if (n < k * p)
    solve1::main(n, k, p);
  else
    solve2::main(n, k, p);
  return 0;
}
