#include <bits/stdc++.h>
using namespace std;
const int maxn = 222222, mod = 998244353;
inline long long read() {
  char ch = getchar();
  long long x = 0, f = 0;
  while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
inline int qpow(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) ans = 1ll * ans * a % mod;
  return ans;
}
int n, k, a[maxn], to[20][maxn], avg, ans = 1e9, id, pre[maxn];
map<int, int> mp[maxn];
inline int dv(int x) { return x ? (x - 1) / (k - 1) + 1 : 0; }
inline int qmo(int x) { return (x + n) % n; }
inline void cov(int x, int v) {
  a[qmo(x)] = a[qmo(x) + n] = v, printf("%d ", v + avg);
}
void solve(int l, int r, int res) {
  if (l == r) return;
  if (l + k - 1 >= r) {
    printf("%d ", qmo(l));
    cov(l, res);
    for (int i = (l + 1); i <= (r); i++) cov(i, 0);
    for (int i = (r + 1); i <= (l + k - 1); i++) cov(i, a[i]);
    puts("");
    return;
  }
  int sum = 0;
  for (int i = (l); i <= (l + k - 1); i++) sum += a[i];
  if (sum >= res) {
    printf("%d ", qmo(l));
    cov(l, res);
    for (int i = (l + 1); i <= (l + k - 2); i++) cov(i, 0);
    cov(l + k - 1, sum - res);
    puts("");
    solve(l + k - 1, r, 0);
  } else {
    solve(l + k - 1, r, a[l + k - 1] + res - sum);
    sum = 0;
    for (int i = (l); i <= (l + k - 1); i++) sum += a[i];
    printf("%d ", qmo(l));
    cov(l, res);
    for (int i = (l + 1); i <= (l + k - 2); i++) cov(i, 0);
    cov(l + k - 1, sum - res);
    puts("");
  }
}
int main() {
  n = read();
  k = read();
  for (int i = (0); i <= (n - 1); i++) avg += a[i] = read();
  avg /= n;
  for (int i = (0); i <= (n - 1); i++) a[i + n] = a[i] -= avg;
  pre[0] = a[0];
  for (int i = (1); i <= (2 * n - 1); i++) pre[i] = pre[i - 1] + a[i];
  for (int i = (2 * n - 1); i >= (0); i--) {
    to[0][i] = mp[(i + 1) % (k - 1)].count(pre[i])
                   ? mp[(i + 1) % (k - 1)][pre[i]]
                   : -1;
    mp[i % (k - 1)][pre[i]] = i;
  }
  for (int i = (1); i <= (19); i++)
    for (int j = (0); j <= (2 * n - 1); j++)
      to[i][j] = ~to[i - 1][j] ? to[i - 1][to[i - 1][j]] : -1;
  for (int i = (0); i <= (n - 1); i++) {
    int at = i, cnt = 0;
    for (int j = (19); j >= (0); j--)
      if (~to[j][at] && to[j][at] < i + n) cnt += 1 << j, at = to[j][at];
    int tmp = (at - i - cnt) / (k - 1) + dv(i + n - at - 1);
    if (tmp < ans) ans = tmp, id = i;
  }
  printf("%d\n", ans);
  int lst = id;
  for (int i = (id + 1); i <= (id + n - 1); i++)
    if ((i - lst - 1) % (k - 1) == 0 && pre[i] == pre[lst])
      solve(lst + 1, i, 0), lst = i;
  solve(lst + 1, id + n, 0);
}
