#include <bits/stdc++.h>
using namespace std;
namespace Solve {
const int MAXN = 1e5 + 10, LOGK = 20;
int n, k, data[MAXN], avg;
int sum[MAXN], ids[MAXN];
struct Node {
  int next;
  int value;
  Node() : next(-1), value(-1) {}
  Node(int next, int value) : next(next), value(value) {}
};
Node go[MAXN * 2][LOGK];
int valid_flag, valid[MAXN], lastpos[MAXN];
bool cmp(int ida, int idb) {
  return sum[ida] < sum[idb] || (sum[ida] == sum[idb] && ida < idb);
}
int distance(int pos1, int pos2) {
  if (pos1 < pos2)
    return pos2 - pos1;
  else
    return n - pos1 + pos2;
}
bool suitable(int pos1, int pos2) {
  return (distance(pos1, pos2) - 1) % (k - 1) == 0;
}
int meetings(int pos1, int pos2) {
  if (pos1 == pos2) return (n - 1 + k - 2) / (k - 1);
  return (distance(pos1, pos2) - 1 + k - 2) / (k - 1);
}
pair<int, vector<pair<int, int>>> solve(int* ids, int cnt) {
  valid_flag++;
  int ans = (n - 1 + k - 2) / (k - 1);
  int bestplan = -1;
  for (int i = 2 * cnt - 1; i >= 0; i--) {
    int thisv = ids[i % cnt] % (k - 1);
    if (valid[(thisv + 1) % (k - 1)] == valid_flag) {
      go[i][0] = Node(
          lastpos[(thisv + 1) % (k - 1)],
          meetings(ids[i % cnt], ids[lastpos[(thisv + 1) % (k - 1)] % cnt]));
    } else {
      go[i][0] = Node(-1, -1);
    }
    valid[thisv] = valid_flag;
    lastpos[thisv] = i;
    for (int w = 1; w < LOGK; w++) {
      if (go[i][w - 1].next >= 0 && go[go[i][w - 1].next][w - 1].next >= 0) {
        go[i][w] =
            Node(go[go[i][w - 1].next][w - 1].next,
                 go[i][w - 1].value + go[go[i][w - 1].next][w - 1].value);
      } else {
        go[i][w] = Node(-1, -1);
      }
    }
    if (i < cnt) {
      int cur = i;
      int curans = 0;
      for (int w = LOGK - 1; w >= 0; w--) {
        if (go[cur][w].next >= 0 && go[cur][w].next < i + cnt) {
          curans += go[cur][w].value;
          cur = go[cur][w].next;
        }
      }
      curans += meetings(ids[cur % cnt], ids[i % cnt]);
      if (ans > curans) {
        ans = curans;
        bestplan = i;
      }
    }
  }
  vector<pair<int, int>> plan;
  if (bestplan < 0) {
    plan.push_back(make_pair(0, 0));
  } else {
    int i = bestplan;
    int cur = i;
    while (go[cur][0].next >= 0 && go[cur][0].next < i + cnt) {
      plan.push_back(make_pair(ids[cur % cnt], ids[go[cur][0].next % cnt]));
      cur = go[cur][0].next;
    }
    plan.push_back(make_pair(ids[cur % cnt], ids[i % cnt]));
  }
  return make_pair(ans, plan);
}
void debug() {
  cout << "======debug" << endl;
  for (int i = 0; i < n; i++) {
    cout << data[i] << " ";
  }
  cout << endl;
}
void output(int L, int R, int extra) {
  if (R - L == 1) return;
  if (R - L <= k) {
    int sum = 0;
    for (int i = L; i < R; i++) {
      sum += data[i % n];
    }
    {
      if (!(sum - extra == (R - L) * avg)) {
        fprintf(stderr, "ERROR AT %s:%d", "<stdin>", 108);
        exit(1);
      }
    };
    for (int i = L; i < R; i++) {
      data[i % n] = avg;
    }
    data[L % n] += extra;
    cout << (L % n);
    for (int i = L; i < L + k; i++) {
      cout << " " << data[i % n];
    }
    cout << "\n";
    return;
  }
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += data[(i + L) % n];
  }
  if (sum >= extra + avg * (k - 1)) {
    for (int i = L; i < L + k - 1; i++) {
      data[i % n] = avg;
    }
    data[L % n] += extra;
    data[(L + k - 1) % n] = sum - avg * (k - 1) - extra;
    cout << (L % n);
    for (int i = L; i < L + k; i++) {
      cout << " " << data[i % n];
    }
    cout << "\n";
    output(L + k - 1, R, 0);
  } else {
    output(L + k - 1, R, extra + avg * (k - 1) - (sum - data[(L + k - 1) % n]));
    for (int i = L; i < L + k; i++) {
      data[i % n] = avg;
    }
    data[L % n] += extra;
    cout << (L % n);
    for (int i = L; i < L + k; i++) {
      cout << " " << data[i % n];
    }
    cout << "\n";
  }
}
void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> data[i];
    avg += data[i];
  }
  avg /= n;
  sum[0] = 0;
  for (int i = 1; i < n; i++) {
    sum[i] = sum[i - 1] + data[i - 1] - avg;
  }
  for (int i = 0; i < n; i++) {
    ids[i] = i;
  }
  sort(ids, ids + n, cmp);
  int ans = (n - 1 + k - 2) / (k - 1);
  vector<pair<int, int>> bestplan;
  bestplan.push_back(make_pair(0, 0));
  for (int pos = 0; pos < n;) {
    int nextpos = pos + 1;
    while (nextpos < n && sum[ids[pos]] == sum[ids[nextpos]]) nextpos++;
    pair<int, vector<pair<int, int>>> thisans = solve(ids + pos, nextpos - pos);
    if (ans > thisans.first) {
      ans = thisans.first;
      bestplan = thisans.second;
    }
    pos = nextpos;
  }
  cout << ans << endl;
  for (int i = 0; i < (int)bestplan.size(); i++) {
    int L = bestplan[i].first, R = bestplan[i].second;
    if (L >= R) R += n;
    int sum = 0;
    for (int x = L; x < R; x++) {
      sum += data[x % n];
    }
    {
      if (!(sum % (R - L) == 0 && sum / (R - L) == avg)) {
        fprintf(stderr, "ERROR AT %s:%d", "<stdin>", 189);
        exit(1);
      }
    };
  }
  for (int i = 0; i < (int)bestplan.size(); i++) {
    if (bestplan[i].first < bestplan[i].second) {
      output(bestplan[i].first, bestplan[i].second, 0);
    } else {
      output(bestplan[i].first, bestplan[i].second + n, 0);
    }
  }
}
}  // namespace Solve
int main() {
  Solve::solve();
  return 0;
}
