#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int N, K;
int A[3 * MAXN];
int sum;
int P[3 * MAXN];
int ps[MAXN], psn;
vector<int> v[MAXN], u[MAXN];
int w[MAXN];
int cnt[MAXN];
bool chk[3 * MAXN];
bool check(int x) { return P[x] <= A[x] && -P[x + K - 2] <= A[x + K - 1]; }
void answer(int s, int ee) {
  if (s > ee) return;
  int e;
  for (e = ee; (e - s + 1) % (K - 1);) {
    A[++e + 1] = sum / N;
    P[e] = 0;
  }
  vector<int> ava;
  for (int i = s; i <= e; i += K - 1)
    if (check(i)) {
      chk[i] = true;
      ava.push_back(i);
    }
  while (!ava.empty()) {
    int t = ava.back();
    ava.pop_back();
    for (int i = t; i < t + K - 1; i++) {
      A[i] -= P[i];
      A[i + 1] += P[i];
    }
    cout << (t % N) << " ";
    for (int i = t; i < t + K; i++)
      cout << (i > ee + 1 ? A[i - N] : A[i]) << " ";
    cout << "\n";
    if (t != s && !chk[t - K + 1] && check(t - K + 1)) {
      chk[t - K + 1] = true;
      ava.push_back(t - K + 1);
    }
    if (t + K - 1 != e + 1 && !chk[t + K - 1] && check(t + K - 1)) {
      chk[t + K - 1] = true;
      ava.push_back(t + K - 1);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) sum += A[i];
  for (int i = 1; i < N; i++) P[i] = A[i] - sum / N + P[i - 1];
  for (int i = 0; i < N; i++) ps[i] = P[i];
  sort(ps, ps + N);
  psn = unique(ps, ps + N) - ps;
  for (int i = 0; i < N; i++)
    v[lower_bound(ps, ps + psn, P[i]) - ps].push_back(i);
  int mx = 0, mxi;
  for (int i = 0; i < psn; i++) {
    for (auto a : v[i]) u[(a % (K - 1) + K - 1) % (K - 1)].push_back(a);
    for (int j = (int)v[i].size() - 1; j >= 0; j--) {
      int t = (v[i][j] + 1) % (K - 1);
      int l =
          upper_bound(((u[t]).begin()), ((u[t]).end()), v[i][j]) - u[t].begin();
      w[v[i][j]] = l == u[t].size() ? -1 : u[t][l];
      cnt[v[i][j]] = l == u[t].size() ? 1 : (cnt[u[t][l]] + 1);
      if (cnt[v[i][j]] > mx) {
        mx = cnt[v[i][j]];
        mxi = v[i][j];
      }
    }
    for (auto a : v[i]) u[(a % (K - 1) + K - 1) % (K - 1)].clear();
  }
  cout << (N - mx + K - 2) / (K - 1) << "\n";
  for (int i = 0; i < N; i++)
    if (i != mxi) P[i] -= P[mxi];
  P[mxi] = 0;
  int t;
  for (t = mxi; w[t] != -1; t = w[t]) answer(t + 1, w[t] - 1);
  for (int i = N; i < 3 * N; i++) {
    A[i] = A[i % N];
    P[i] = P[i % N];
  }
  answer(t + 1, mxi - 1 + N);
  return 0;
}
