#include <bits/stdc++.h>
using namespace std;
long long ksm(long long a, long long b) {
  if (!b) return 1;
  long long ns = ksm(a, b >> 1);
  ns = ns * ns % 1000000007;
  if (b & 1) ns = ns * a % 1000000007;
  return ns;
}
int dp[105][500005];
int s[500005];
int n, k, p;
struct bt {
  int d[105];
  bt() {
    for (int i = 0; i < 105; i++) d[i] = 1e9;
  }
  void upd(int pl, int num) {
    while (pl < 105) d[pl] = min(d[pl], num), pl += (pl & -pl);
  }
  int q(int pl) {
    int ans = 1e9;
    while (pl) {
      ans = min(ans, d[pl]);
      pl -= (pl & (-pl));
    }
    return ans;
  }
} c[2];
void upd(int a, int b) {
  if (a == 0) a = p;
  c[0].upd(a, b + a + p);
  c[1].upd(p + 1 - a, b + a);
}
int q(int a) {
  if (a == 0) a += p;
  return min(-a + c[0].q(a), c[1].q(p + 1 - a) - a);
}
int main() {
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) scanf("%d", &s[i]), s[i] += s[i - 1], s[i] %= p;
  for (int i = 0; i <= k; i++) {
    c[0] = c[1] = bt();
    if (i != 0) dp[i][n + 1] = 1e9;
    for (int j = n; j >= 1; j--) {
      if (i == 0)
        dp[i][j] = 1e9;
      else {
        upd(s[j], dp[i - 1][j + 1]);
        dp[i][j] = q(s[j - 1]);
      }
    }
  }
  cout << dp[k][1] << endl;
  return 0;
}
