#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
struct Op {
  int idx;
  vector<int> vals;
};
int n, sz;
int a[MAXN];
vector<Op> ans;
int asum[MAXN + 1];
int each;
int bal[MAXN];
pair<int, int> o[MAXN];
int x[2 * MAXN], nx;
int y[2 * MAXN];
int last[MAXN];
int nxt[2 * MAXN];
vector<int> jump[2 * MAXN];
int bestval, bestx, bestidx;
int cur[MAXN];
void calc() {
  int den = sz - 1;
  for (int i = (0); i < (nx); ++i) x[nx + i] = x[i] + n;
  for (int i = (0); i < (2 * nx); ++i) y[i] = x[i] % den;
  for (int i = 2 * nx - 1; i >= 0; --i) {
    nxt[i] = last[(y[i] + 1) % den];
    last[y[i]] = i;
  }
  for (int i = 2 * nx - 1; i >= 0; --i) {
    last[y[i]] = -1;
  }
  for (int i = 2 * nx - 1; i >= 0; --i) {
    jump[i].clear();
    if (nxt[i] == -1) continue;
    jump[i].push_back(nxt[i]);
    for (int k = 1;; ++k) {
      int mid = jump[i][k - 1];
      if (k - 1 >= ((int)(jump[mid]).size())) break;
      jump[i].push_back(jump[mid][k - 1]);
    }
  }
  for (int i = (0); i < (nx); ++i) {
    int j = i, steps = 0;
    for (int k = ((int)(jump[i]).size()) - 1; k >= 0; --k)
      if (k < ((int)(jump[j]).size()) && jump[j][k] < i + nx)
        j = jump[j][k], steps += 1 << k;
    int cur = (n - (steps + 1) + den - 1) / den;
    if (cur < bestval) bestval = cur, bestx = x[i], bestidx = i;
  }
}
void fixrange(int l, int r, int extra) {
  int totcnt = r - l + 1, fstcnt = min(totcnt, sz), fstoff = l;
  int fstsum = 0;
  for (int i = (0); i < (fstcnt); ++i) fstsum += cur[(fstoff + i) % n];
  if (fstcnt == 1) {
    assert(fstsum == each + extra);
  } else if (fstsum >= (fstcnt - 1) * each + extra) {
    int rem = fstsum - (fstcnt - 1) * each - extra;
    if (rem != each) assert(fstcnt < totcnt);
    Op op;
    op.idx = fstoff % n;
    op.vals = vector<int>(sz, -1);
    for (int i = (0); i < (sz); ++i)
      op.vals[i] = i == 0            ? each + extra
                   : i < fstcnt - 1  ? each
                   : i == fstcnt - 1 ? rem
                                     : cur[(fstoff + i) % n];
    ans.push_back(op);
    int before = 0, after = 0;
    for (int i = (0); i < (sz); ++i) {
      int idx = (fstoff + i) % n;
      before += cur[idx], after += op.vals[i];
      cur[idx] = op.vals[i];
    }
    assert(before == after);
    if (fstcnt < totcnt) fixrange(l + fstcnt - 1, r, 0);
  } else {
    assert(fstcnt < totcnt);
    fixrange(
        l + fstcnt - 1, r,
        (fstcnt - 1) * each + extra - fstsum + cur[(fstoff + fstcnt - 1) % n]);
    fixrange(l, l + fstcnt - 1, extra);
  }
}
void solve() {
  assert(sz >= 2);
  asum[0] = 0;
  for (int i = (0); i < (n); ++i) asum[i + 1] = asum[i] + a[i];
  assert(asum[n] % n == 0);
  each = asum[n] / n;
  for (int i = (0); i < (n); ++i) bal[i] = asum[i] - i * each;
  for (int i = (0); i < (n); ++i) o[i] = make_pair(bal[i], i);
  sort(o, o + n);
  bestval = INT_MAX, bestx = -1, bestidx = -1;
  memset(last, -1, sizeof(last));
  for (int l = 0, r = l; l < n; l = r) {
    while (r < n && o[r].first == o[l].first) ++r;
    nx = 0;
    for (int i = (l); i < (r); ++i) x[nx++] = o[i].second;
    calc();
  }
  nx = 0;
  for (int i = (0); i < (n); ++i)
    if (bal[i] == bal[bestx]) x[nx++] = i;
  calc();
  ans.clear();
  for (int i = (0); i < (n); ++i) cur[i] = a[i];
  int at = bestidx;
  while (at < bestidx + nx) {
    if (nxt[at] == -1 || nxt[at] >= bestidx + nx) {
      fixrange(x[at], x[bestidx + nx] - 1, 0);
      break;
    }
    fixrange(x[at], x[nxt[at]] - 1, 0);
    at = nxt[at];
  }
  assert(((int)(ans).size()) == bestval);
  for (int i = (0); i < (n); ++i) assert(cur[i] == each);
}
void run() {
  scanf("%d%d", &n, &sz);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  solve();
  printf("%d\n", ((int)(ans).size()));
  for (int i = (0); i < (((int)(ans).size())); ++i) {
    printf("%d", ans[i].idx);
    for (int j = (0); j < (((int)(ans[i].vals).size())); ++j)
      printf(" %d", ans[i].vals[j]);
    puts("");
  }
}
void stress() {
  int mxn = 10, mxeach = 10;
  for (int rep = (0); rep < (1000000); ++rep) {
    n = rand() % (mxn - 2 + 1) + 2;
    sz = rand() % (n - 2 + 1) + 2;
    int each = rand() % (mxeach + 1);
    for (int i = (0); i < (n); ++i) a[i] = 0;
    for (int i = (0); i < (n * each); ++i) {
      int idx = rand() % n;
      ++a[idx];
    }
    solve();
    printf(".");
  }
}
void stresstime() {
  for (int rep = (0); rep < (3); ++rep) {
    n = MAXN;
    sz = MAXN - 1;
    for (int i = (0); i < (n); ++i) a[i] = rand() % 1001;
    int sum = 0;
    for (int i = (0); i < (n); ++i) sum += a[i];
    int need = (n - sum % n) % n;
    for (int i = (0); i < (need); ++i) {
      int idx = rand() % n;
      a[idx]++;
    }
    solve();
    printf(".");
  }
}
int main() {
  run();
  return 0;
}
