#include <bits/stdc++.h>
using namespace std;
template <typename T>
void debug_out(T t) {
  cerr << t;
}
template <typename A, typename B>
void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}
template <typename T>
void debug_out(vector<T> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1) cerr << ", ";
  }
}
template <typename T>
void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1) cerr << endl;
  }
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}
int n, k;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  vector<int> a(n);
  vector<int> p(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sum /= n;
  map<int, vector<int>> pos;
  for (int i = 0; i < n; i++) {
    p[i] = a[i] - sum;
    if (i) p[i] += p[i - 1];
    pos[p[i]].push_back(i);
  }
  int res = (n + k - 3) / (k - 1);
  vector<int> seg = {0};
  for (auto it : pos) {
    vector<int> s = it.second;
    int sz = s.size();
    for (int i = 0; i < sz; i++) s.push_back(s[i] + n);
    sz *= 2;
    map<int, vector<int>> cur;
    for (int i = 0; i < sz; i++) cur[s[i] % (k - 1)].push_back(i);
    const int MAX = 20;
    vector<vector<int>> to(sz, vector<int>(MAX, sz));
    for (int i = 0; i < sz; i++) {
      int nxt = (s[i] + 1) % (k - 1);
      auto pt = upper_bound(cur[nxt].begin(), cur[nxt].end(), i);
      if (pt != cur[nxt].end()) to[i][0] = *pt;
    }
    for (int i = 1; i < MAX; i++) {
      for (int j = 0; j < sz; j++) {
        if (to[j][i - 1] == sz)
          to[j][i] = sz;
        else
          to[j][i] = to[to[j][i - 1]][i - 1];
      }
    }
    int st = -1;
    int ft = 0;
    for (int i = 0; i < sz / 2; i++) {
      int ps = i;
      int total = 0;
      for (int j = MAX - 1; j > -1; j--) {
        if (to[ps][j] > i + (sz / 2)) continue;
        ps = to[ps][j];
        total += 1 << j;
      }
      if (ps == i + (sz / 2) && total > ft) {
        ft = total;
        st = i;
      }
    }
    if (st != -1 && ft > (int)seg.size()) {
      seg.clear();
      while ((int)seg.size() < ft) {
        seg.push_back(s[st]);
        st = to[st][0];
      }
      res = (n - ft) / (k - 1);
    }
  }
  vector<vector<int>> ans;
  function<void(int, int, int)> solve = [&](int l, int r, int head) {
    int cnt = ((r - l + n) % n) + 1;
    if (cnt == 1) {
      assert(sum == head);
      assert(a[l] == sum);
      return;
    }
    if (cnt <= k) {
      vector<int> c;
      c.push_back(l);
      c.push_back(head);
      for (int i = 0; i < cnt - 1; i++) c.push_back(sum);
      for (int i = (r + 1) % n; i != (r + k - cnt + 1); i = (i + 1) % n)
        c.push_back(a[i]);
      a[l] = head;
      for (int i = (l + 1) % n; i != (r + 1) % n; i = (i + 1) % n) a[i] = sum;
      ans.push_back(c);
      return;
    }
    int ps = 0;
    for (int i = l; i != (l + k) % n; i = (i + 1) % n)
      ps += a[i] - (i == l ? head : sum);
    if (ps >= 0) {
      vector<int> c;
      c.push_back(l);
      c.push_back(head);
      while ((int)c.size() < k) c.push_back(sum);
      c.push_back(sum + ps);
      ans.push_back(c);
      for (int i = l; i != (l + k) % n; i = (i + 1) % n)
        a[i] = (i == l ? head : sum);
      a[(l + k + n - 1) % n] = sum + ps;
      solve((l + k + n - 1) % n, r, sum);
    } else {
      solve((l + k + n - 1) % n, r, a[(l + k + n - 1) % n] - ps);
      vector<int> c;
      c.push_back(l);
      c.push_back(head);
      while ((int)c.size() < k + 1) c.push_back(sum);
      ans.push_back(c);
      for (int i = l; i != (l + k) % n; i = (i + 1) % n)
        a[i] = (i == l ? head : sum);
    }
  };
  int sz = seg.size();
  for (int i = 0; i < sz; i++) {
    solve((seg[i] + 1) % n, seg[(i + 1) % sz] % n, sum);
  }
  assert((int)ans.size() == res);
  cout << res << endl;
  for (auto i : ans) {
    for (int j : i) cout << j << " ";
    cout << endl;
  }
  return 0;
}
