Heidi is now just one code away from breaking the encryption of the Death Star plans. The screen that should be presenting her with the description of the next code looks almost like the previous one, though who would have thought that the evil Empire engineers would fill this small screen with several million digits! It is just ridiculous to think that anyone would read them all...

Heidi is once again given a sequence A and two integers k and p. She needs to find out what the encryption key S is.

Let X be a sequence of integers, and p a positive integer. We define the score of X to be the sum of the elements of X modulo p.

Heidi is given a sequence A that consists of N integers, and also given integers k and p. Her goal is to split A into k parts such that: 

  * Each part contains at least 1 element of A, and each part consists of contiguous elements of A. 
  * No two parts overlap. 
  * The total sum S of the scores of those parts is minimized (not maximized!). 



Output the sum S, which is the encryption code.

Input

The first line of the input contains three space-separated integers N, k and p (k ≤ N ≤ 500 000, 2 ≤ k ≤ 100, 2 ≤ p ≤ 100) – the number of elements in A, the number of parts A should be split into, and the modulo for computing scores, respectively.

The second line contains N space-separated integers that are the elements of A. Each integer is from the interval [1, 1 000 000].

Output

Output the number S as described in the problem statement.

Examples

Input

4 3 10
3 4 7 2


Output

6


Input

10 5 12
16 3 24 13 9 8 7 5 12 12


Output

13

Note

In the first example, if the input sequence is split as (3), (4, 7), (2), the total score would be <image>. It is easy to see that this score is the smallest possible.

In the second example, one possible way to obtain score 13 is to make the following split: (16, 3), (24), (13), (9, 8), (7, 5, 12, 12).
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
const int MAXK = 1e2 + 5;
const int INF = 0x3f3f3f3f;
int N, K, P;
int a[MAXN];
vector<int> pos[MAXK];
int dp[MAXK][MAXK][MAXK];
int pref[MAXK][MAXK][MAXK], suff[MAXK][MAXK][MAXK];
void load() {
  scanf("%d%d%d", &N, &K, &P);
  for (int i = 1; i <= N; i++) scanf("%d", a + i);
}
int get_nxt(int val, int idx) {
  auto it = lower_bound(pos[val].begin(), pos[val].end(), idx + 1);
  return it == pos[val].end() ? INF : *it;
}
int solve() {
  for (int i = 1; i <= N; i++) {
    a[i] = (a[i] + a[i - 1]) % P;
    pos[a[i]].push_back(i);
  }
  memset(dp, INF, sizeof dp);
  memset(pref, INF, sizeof pref);
  memset(suff, INF, sizeof suff);
  for (int i = 0; i < P; i++) dp[0][0][i] = pref[0][0][i] = suff[0][0][i] = 0;
  int sol = INF;
  for (int i = 1; i < K; i++)
    for (int k = 0; k < K; k++) {
      for (int j = 0; j < P; j++) {
        suff[i][k][j] = pref[i][k][j] = dp[i][k][j] = get_nxt(
            j, min(pref[i - 1][k][j], k ? suff[i - 1][k - 1][j + 1] : INF));
        if (j) pref[i][k][j] = min(pref[i][k][j], pref[i][k][j - 1]);
        if (i == K - 1 && dp[i][k][j] < N) sol = min(sol, k + (j > a[N]));
      }
      for (int j = P - 1; j >= 0; j--)
        suff[i][k][j] = min(suff[i][k][j], suff[i][k][j + 1]);
    }
  return a[N] + sol * P;
}
int main() {
  load();
  printf("%d\n", solve());
  return 0;
}
