#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int maxn = 200005;
const int maxk = 19;
const int inf = 1e9;
ll a[2 * maxn];
ll psum[2 * maxn];
pair2<int> go[maxk][2 * maxn];
map<ll, int> lst[maxn];
int id[2 * maxn];
int n, k;
ll need;
vector<pair<int, vector<ll>>> answer;
void perform(int l) {
  int fn = min(l + k, n);
  assert(psum[fn] >= 0);
  ll wassum = psum[fn];
  vector<ll> exchange = vector<ll>(k, need);
  int start = min(n - k, l);
  for (int i = start; i < l; i++) exchange[i - start] = need + a[i];
  exchange[l - start] += -psum[l];
  exchange.back() += psum[fn];
  answer.push_back({start, exchange});
  for (int i = l; i < fn; i++) a[i] = 0;
  a[l] += -psum[l];
  a[fn - 1] += psum[fn];
  for (int i = start; i < start + k; i++) psum[i + 1] = psum[i] + a[i];
  assert(wassum == psum[fn]);
}
void restore(int l) {
  if (l >= n) return;
  if (a[l] == 0 && psum[l] == 0) {
    restore(l + 1);
    return;
  }
  int fn = min(l + k, n);
  if (psum[fn] >= 0) {
    perform(l);
    restore(l + k - (psum[fn] > 0));
    return;
  } else {
    restore(l + k - 1);
    assert(psum[fn] == 0);
    perform(l);
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
  need = accumulate(a, a + n, 0LL);
  assert(need % n == 0);
  need /= n;
  for (int i = 0; i < n; i++) a[i] -= need;
  for (int i = 0; i < n; i++) a[i + n] = a[i];
  partial_sum(a, a + 2 * n, psum + 1);
  pair2<int> ans = {inf, -1};
  for (int j = 0; j < maxk; j++) go[j][2 * n] = {2 * n, 0};
  for (int i = 2 * n - 1; i >= 0; i--) {
    int curid = id[i + 1];
    if (lst[curid].count(psum[i])) {
      int to = lst[curid][psum[i]];
      go[0][i] = {to, (to - i - 1) / (k - 1)};
    } else {
      int to = 2 * n;
      go[0][i] = {to, (to - i + k - 2) / (k - 1)};
    }
    for (int j = 1; j < maxk; j++)
      go[j][i] = {go[j - 1][go[j - 1][i].first].first,
                  go[j - 1][i].second + go[j - 1][go[j - 1][i].first].second};
    if (i < n) {
      int to = n + i;
      int cur = i;
      int curans = 0;
      for (int j = maxk - 1; j >= 0; j--)
        if (go[j][cur].first <= to) {
          curans += go[j][cur].second;
          cur = go[j][cur].first;
        }
      if (cur < to) curans += (to - cur - 1 + k - 2) / (k - 1);
      ans = min(ans, {curans, i});
    }
    id[i] = (2 * n - i) % (k - 1);
    lst[id[i]][psum[i]] = i;
  }
  rotate(a, a + ans.second, a + n);
  partial_sum(a, a + n, psum + 1);
  restore(0);
  assert((int)answer.size() == ans.first);
  printf("%d\n", (int)answer.size());
  for (auto &t : answer) {
    printf("%d", (t.first + ans.second) % n);
    for (auto tt : t.second) printf(" %lld", tt);
    printf("\n");
  }
  return 0;
}
