#include <bits/stdc++.h>
using namespace std;
int const N = 200000, W = sqrt(N);
int x[N], y[N], nxt[N], n, k, ano, need;
vector<int> mp, in[N], an, can;
pair<int, int> pp(-1, 0);
vector<int> g[N];
int inc[N];
bool vis[N];
void upan(int w) {
  if (can.size() < an.size()) an = can, ano = w, pp.first = -1;
}
void upb(int w, int st) {
  can.clear();
  int j = st;
  st += n >> 1;
  if (x[j] == w) j = nxt[j];
  while (j < st) {
    can.push_back(j);
    j += k;
    if (x[j] == w) j = nxt[j];
  }
  upan(w);
}
void ups(int w, int st) {
  can.clear();
  int j = in[w][st] + 1, i = st + 1, pan = 0, ed = j + n / 2;
  while (j < ed) {
    int t = (in[w][i] - j + k - 1) / k;
    pan += t;
    j += t * k;
    while (i < (int)in[w].size() && j >= in[w][i]) {
      if (j == in[w][i]) ++j;
      ++i;
    }
  }
  if (pan < (int)an.size()) {
    pp = make_pair(w, st);
    an.resize(pan);
  }
}
void upsa(int w, int st) {
  can.clear();
  int j = in[w][st] + 1, i = st + 1;
  st = j + n / 2;
  while (i < (int)in[w].size() && j >= in[w][i]) {
    if (j == in[w][i]) ++j;
    ++i;
  }
  while (j < st) {
    can.push_back(j);
    j += k;
    while (i < (int)in[w].size() && j >= in[w][i]) {
      if (j == in[w][i]) ++j;
      ++i;
    }
  }
  an.resize(can.size() + 1);
  upan(w);
}
void gob(int w) {
  for (int i = 0; i < (int)(k); ++i) upb(w, i);
}
void gos(int w) {
  for (int i = 0; i < (int)(in[w].size() >> 1); ++i) ups(w, i);
}
void go(int w) {
  if ((int)in[w].size() >> 1 > W)
    gob(w);
  else
    gos(w);
}
void fn() {
  n >>= 1;
  ++k;
  for (int u : an)
    for (int z : g[u]) ++inc[z];
  stack<int> q;
  for (int u : an)
    if (inc[u] == 0) q.push(u);
  while (!q.empty()) {
    int v = q.top();
    q.pop();
    for (int u : g[v])
      if (--inc[u] == 0) q.push(u);
    if (v >= n) v -= n;
    printf("%d", v);
    if (!vis[v]) {
      int t = v ? v - 1 : n - 1;
      y[v] = need - x[t];
    }
    int t = v + k - 1;
    if (t >= n) t -= n;
    if (!vis[t]) y[t] = need + x[t];
    for (int i = v; i < (int)(v + k); ++i) {
      int j = i >= n ? i - n : i;
      if (vis[j] || (i != v && i + 1 != v + k)) y[j] = need;
      vis[j] = true;
    }
    for (int i = 0; i < (int)(k); ++i)
      printf(" %d", y[i + v >= n ? i + v - n : i + v]);
    printf("\n");
  }
}
int main() {
  scanf("%d%d", &n, &k);
  --k;
  for (int i = 0; i < (int)(n); ++i) scanf("%d", y + i);
  for (int i = 0; i < (int)(n); ++i) need += y[i];
  need /= n;
  int car = 0;
  for (int i = 0; i < (int)(n); ++i) {
    x[i] = y[i] + car - need;
    car = x[i];
  }
  mp.resize(n);
  for (int i = 0; i < (int)(n); ++i) mp[i] = x[i];
  sort(mp.begin(), mp.end());
  mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
  if (mp.size() == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < (int)(n); ++i)
    x[i] = lower_bound(mp.begin(), mp.end(), x[i]) - mp.begin();
  for (int i = 0; i < (int)(n); ++i) x[i + n] = x[i];
  n <<= 1;
  for (int i = 0; i < (int)(n); ++i) in[x[i]].push_back(i);
  nxt[n - 1] = n;
  for (int i = n - 2; i >= 0; --i)
    if (x[i] == x[i + 1])
      nxt[i] = nxt[i + 1];
    else
      nxt[i] = i + 1;
  an.resize(n);
  for (int i = 0; i < (int)(mp.size()); ++i) go(i);
  if (pp.first != -1) upsa(pp.first, pp.second);
  ano = mp[ano];
  for (int i = 0; i < (int)(n); ++i) x[i] = mp[x[i]] - ano;
  for (int i = 1; i < (int)(an.size()); ++i)
    if (an[i] == an[i - 1] + k) {
      if (x[an[i] - 1] > 0 && x[an[i]] > 0)
        g[an[i - 1]].push_back(an[i]);
      else if (x[an[i]] < 0 && x[an[i] - 1] < 0)
        g[an[i]].push_back(an[i - 1]);
    }
  int t = an[0] + n / 2;
  if (t == an.back() + k) {
    if (x[t - 1] > 0 && x[t] > 0)
      g[an.back()].push_back(an[0]);
    else if (x[t] < 0 && x[t - 1] < 0)
      g[an[0]].push_back(an.back());
  }
  printf("%d\n", (int)an.size());
  fn();
}
