#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int n, k, a[N], avg, sum[N];
long long b[N];
pair<int, int> go[N][20];
map<int, int> lst[N];
pair<int, int> ans = make_pair(1e9, 0);
vector<pair<int, vector<long long>>> Ans, tmp;
int cal(int len) {
  if (len <= 1) return 0;
  return (len - 1 + k - 2) / (k - 1);
}
void apply(int s, vector<long long> v) {
  for (int i = s, j = 0; i < s + k; i++, j++) b[i] = v[j];
}
void solve(int l, int r, long long t) {
  if (l == r) return;
  if (r - l + 1 <= k) {
    int L = l, R = r;
    while (L > 1 && R - L + 1 < k) --L;
    while (R < n && R - L + 1 < k) ++R;
    vector<long long> vec;
    for (int i = L; i < l; i++) vec.push_back(b[i]);
    vec.push_back(t);
    for (int i = l + 1; i <= r; i++) vec.push_back(0);
    for (int i = r + 1; i <= R; i++) vec.push_back(b[i]);
    Ans.push_back(make_pair(L, vec));
    apply(L, vec);
  } else {
    long long sum = 0;
    for (int i = l; i < l + k; i++) sum += b[i];
    if (sum >= t) {
      vector<long long> vec;
      vec.push_back(t);
      for (int i = l + 1; i < l + k - 1; i++) vec.push_back(0);
      vec.push_back(sum - t);
      Ans.push_back(make_pair(l, vec));
      apply(l, vec);
      solve(l + k - 1, r, 0);
    } else {
      solve(l + k - 1, r, t - sum + b[l + k - 1]);
      vector<long long> vec;
      vec.push_back(t);
      for (int i = l + 1; i <= l + k - 1; i++) vec.push_back(0);
      Ans.push_back(make_pair(l, vec));
      apply(l, vec);
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) a[i] = gi(), avg += a[i];
  avg /= n;
  for (int i = 0; i < n; i++) a[i] -= avg, a[i + n] = a[i];
  for (int i = 0; i < n + n; i++) sum[i + 1] = sum[i] + a[i];
  for (int i = 0; i < 20; i++) go[n + n][i] = make_pair(n + n, 0);
  for (int i = n + n - 1; ~i; i--) {
    if (lst[(i + 1) % (k - 1)].count(sum[i])) {
      int x = lst[(i + 1) % (k - 1)][sum[i]];
      go[i][0] = make_pair(x, cal(x - i));
    } else
      go[i][0] = make_pair(n + n, cal(n + n - i));
    for (int j = 1; j < 20; j++)
      go[i][j].first = go[go[i][j - 1].first][j - 1].first,
      go[i][j].second =
          go[i][j - 1].second + go[go[i][j - 1].first][j - 1].second;
    if (i < n) {
      int x = i, w = 0;
      for (int j = 19; ~j; j--)
        if (go[x][j].first < i + n) w += go[x][j].second, x = go[x][j].first;
      w += cal(n + i - x);
      ans = min(ans, make_pair(min(w, cal(n)), i));
    }
    lst[i % (k - 1)][sum[i]] = i;
  }
  cout << ans.first << '\n';
  for (int i = 0; i < n + n; i++) b[i] = a[i];
  solve(ans.second, ans.second + n - 1, 0);
  tmp = Ans;
  Ans.clear();
  int x = ans.second;
  for (int i = 0; i < n + n; i++) b[i] = a[i];
  while (go[x][0].first < ans.second + n)
    solve(x, go[x][0].first - 1, 0), x = go[x][0].first;
  solve(x, ans.second + n - 1, 0);
  if (int(tmp.size()) < int(Ans.size())) swap(tmp, Ans);
  for (auto x : Ans) {
    cout << x.first % n << ' ';
    for (auto y : x.second) cout << y + avg << ' ';
    cout << '\n';
  }
  return 0;
}
