#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 15;
const int P = 105;
const int K = 105;
const int MAXI = 2e9;
int dp[K][P];
int pen[2][P][K];
void update(int id, int k, int pos, int val) {
  while (pos < P) {
    pen[id][k][pos] = min(pen[id][k][pos], val);
    pos += pos & (-pos);
  }
}
int query(int id, int k, int pos) {
  int ret = MAXI;
  while (pos > 0) {
    ret = min(ret, pen[id][k][pos]);
    pos = pos & (pos - 1);
  }
  return ret;
}
void update(int id, int pos, int l, int r, int ind, int val) {
  if (l == r) {
    dp[id][pos] = val;
    return;
  }
  int mid = (l + r) / 2;
  if (ind <= mid) {
    update(id, pos * 2, l, mid, ind, val);
  } else {
    update(id, pos * 2 + 1, mid + 1, r, ind, val);
  }
  dp[id][pos] = min(dp[id][pos * 2], dp[id][pos * 2 + 1]);
}
int query(int id, int pos, int l, int r, int ql, int qr) {
  if (qr < l || r < ql) {
    return MAXI;
  }
  if (ql <= l && r <= qr) {
    return dp[id][pos];
  }
  int mid = (l + r) / 2;
  return min(query(id, pos * 2, l, mid, ql, qr),
             query(id, pos * 2 + 1, mid + 1, r, ql, qr));
}
void solve() {
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  int sum = 0;
  for (int num = 1; num < K; ++num) {
    for (int mod = 1; mod < P; ++mod) {
      pen[0][num][mod] = pen[1][num][mod] = MAXI;
    }
  }
  for (int i = 0; i < K; ++i) {
    for (int j = 0; j < K; ++j) {
      dp[i][j] = MAXI;
    }
  }
  update(0, 0, 1, 0);
  update(0, 0, p, 0);
  dp[0][0] = 0;
  int tmp[K];
  tmp[k] = 2e9;
  int val;
  int i, num;
  for (i = 0; i < n; ++i) {
    scanf("%d", &val);
    sum = (sum + val) % p;
    int sz = min(i, k - 1);
    tmp[sz + 1] = 2e9;
    for (num = sz; num >= 0; --num) {
      int mini = query(0, num, sum + 1);
      mini += sum;
      int mini2 = MAXI;
      if (sum + 2 <= p) {
        mini2 = query(1, num, p - sum - 1);
        mini2 += p + sum;
      }
      mini = min(mini, mini2);
      if (i == n - 1) {
        printf("%d\n", mini);
        return;
      }
      if (tmp[num + 1] > mini - sum) {
        update(0, num + 1, sum + 1, mini - sum);
        update(1, num + 1, p - sum, mini - sum);
        tmp[num + 1] = mini - sum;
        dp[num + 1][sum] = mini - sum;
      }
      if (num != 0) {
        tmp[num] = dp[num][sum];
      }
    }
  }
}
int main() { solve(); }
