#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=301000;
int n,k,a[N],s[N],avg,ps[N],ss[N],val[N],off[N];
map<int,VI> hs;
PII bf[N];
int q[N],vis[N],nval[N],p[N][20],rem[N],pr,T,mark[N];
VI ret;

void print(int x,VI y) {
	assert(SZ(y)==k);
	int s1=0,s2=0;
	printf("%d",x); for (auto p:y) printf(" %d",p);
	puts("");
	rep(i,0,k) s1+=a[(i+x)%n];
	rep(i,0,k) s2+=y[i];
	assert(s1==s2);
	rep(i,0,k) assert(y[i]>=0);
	rep(i,0,k) a[(i+x)%n]=y[i];
}

void gao(int l,int r) {
//	printf("gao %d %d\n",l,r);
	int len=(r-l+k-2)/(k-1);
	int cutoff=r-l;
	//printf()
	rep(i,0,len*(k-1)+1) {
		if (l+i>r) val[i]=0;
		else val[i]=a[(l+i)%n]-avg;
		off[i]=(a[(l+i)%n]-avg)-val[i];
	}
	int m=len*(k-1)+1;
	ps[0]=val[0]; ss[m-1]=val[m-1];
	rep(i,1,m) {
		ps[i]=ps[i-1]+val[i];
	}
	per(i,0,m-1) {
		ss[i]=ss[i+1]+val[i];
	}
	int t=0;
	auto add=[&](int bl) {
		if (vis[bl]) return;
		if (bf[bl].fi>=-avg&&bf[bl].se>=-avg) {
			q[t++]=bl;
			vis[bl]=1;
		}
	};
	rep(bl,0,len) {
		int pl=bl*(k-1);
		int pr=(bl+1)*(k-1);
		bf[bl]=mp(ss[pl],ps[pr]);
	//	printf("ooo %d %d\n",bf[bl].fi,bf[bl].se);
		vis[bl]=0;
		add(bl);
	}
	rep(i,0,t) {
		int bl=q[i];
		int pl=bl*(k-1);
		int pr=(bl+1)*(k-1);
		nval[pl]=bf[bl].fi; nval[pr]=bf[bl].se;
		rep(k,pl+1,pr) nval[k]=0;
		//printf("gg %d %d\n",pl,pr);
		VI xx;
		rep(k,pl,pr+1) {
			if (k>cutoff) xx.pb(a[(l+k)%n]);
			else {
				xx.pb(nval[k]+avg);
			}
			val[k]=nval[k];
		}
		print((pl+l)%n,xx);
		if (bl>0) {
			bf[bl-1].se=0;
			add(bl-1);
		}
		if (bl+1<len) {
			bf[bl+1].fi=0;
			add(bl+1);
		}
	}
}
int main() {
	scanf("%d%d",&n,&k);
	rep(i,0,n) {
		scanf("%d",a+i);
		avg+=a[i];
	}
	assert(avg%n==0);
	avg/=n;
	rep(i,0,n) {
		s[i+1]=s[i]+a[i]-avg;
		hs[s[i]].pb(i);
	}
	int mr=n+1;
	for (auto q:hs) {
		VI z=q.se;
		int ss=0;
		int m=SZ(z);
		static int s[N],f[N];
		rep(i,0,m) {
			f[i]=z[i],f[i+m]=z[i]+n;
		}
		f[m+m]=z[0]+2*n;
		rep(i,0,m+m+1) s[i]=f[i]%(k-1);
		T++;
		per(i,0,2*m) {
			int w=(s[i]+1)%(k-1);
			if (mark[w]!=T) mark[w]=T,rem[w]=2*m;
			p[i][0]=rem[w];
			rem[s[i]]=i,mark[s[i]]=T;
		}
		rep(j,0,19) p[m+m][j]=m+m;
		rep(j,1,19) {
			rep(i,0,m+m) p[i][j]=p[p[i][j-1]][j-1];
		}
		rep(i,0,m) {
			int c=0,v=i;
			per(j,0,19) if (f[p[v][j]]<f[i]+n) {
				v=p[v][j]; c+=(1<<j);
			}
			ss=(f[v]-f[i]-c)/(k-1)+(f[i]+n-f[v]-1+k-2)/(k-1);
			if (ss<mr) {
				mr=ss;
				pr=z[i];
			}
		}
	}
	printf("%d\n",mr);
	int pv=0;
	ret.pb(pr);
	rep(j,1,n) {
		if (s[(pr+j)%n]==s[pr]&&(j-pv-1)%(k-1)==0) {
			ret.pb(pr+j);
			pv=j;
		}
	}
	ret.pb(pr+n);
	int zz=0;
	rep(i,0,SZ(ret)-1) {
		int l=ret[i+1]-ret[i];
		zz+=(l-1+k-2)/(k-1);
	}
	assert(zz==mr);
	rep(i,0,SZ(ret)-1) {
		gao(ret[i],ret[i+1]-1);
	}
	rep(i,0,n) assert(a[i]==avg);
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		LL hh = -s[x - 1]; a[x] = hh;
		for(reg i = x; i >= n - k + 1; i--) {
			if(a[i] + V < 0) a[i - 1] += V + a[i], a[i] = -V;
			else break;
		} for(reg i = n - k + 1; i <= x; i++) ans[tp].push_back(V + a[i]), s[i] = s[i - 1] + a[i];
		for(reg i = x + 1; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include<bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

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...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__), cerr << endl;

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;
  range(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  sum /= n;
  map<int, vector<int>> pos;
  range(i, 0, n) {
    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();
    range(i, 0, sz) s.push_back(s[i] + n);
    sz *= 2;
    map<int, vector<int>> cur;
    range(i, 0, sz) cur[s[i] % (k - 1)].push_back(i);
    const int MAX = 20;
    vector<vector<int>> to(sz, vector<int>(MAX, sz));
    range(i, 0, sz) {
      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;
    }
    range(i, 1, MAX) {
      range(j, 0, sz) {
        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;
    range(i, 0, sz / 2) {
      int ps = i;
      int total = 0;
      husk(j, MAX - 1, -1) {
        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);
      range(i, 0, cnt - 1) 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();
  range(i, 0, sz) {
    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;
}

#include<bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 1.1e5;
int N, K;
int A[MAXN * 4];

int pref[MAXN * 4];

pair<ll, ll> dist[MAXN][20];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> N >> K;
	int tot = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		tot += A[i];
	}
	assert(tot % N == 0);
	int avg = tot / N;
	for (int i = 0; i < N; i++) {
		A[i] -= avg;
	}

	for (int i = 0; i < N; i++) {
		A[i+3*N] = A[i+2*N] = A[i+N] = A[i];
	}

	pref[0] = 0;
	for (int i = 0; i < 4 * N; i++) {
		pref[i+1] = pref[i] + A[i];
	}

	{
		vector<map<int, int>> prefMatches(N);

		for (int i = 2 * N; i >= 0; i--) {
			if (i < N) {
				if (prefMatches[(i+1)%(K-1)].count(pref[i]) && prefMatches[(i+1) % (K-1)][pref[i]] <= i+N) {
					int l = prefMatches[(i+1) % (K-1)][pref[i]] - i;
					dist[i][0] = {l, (l-1) / (K-1)};
				} else {
					dist[i][0] = {N, (N-1 + (K-2)) / (K-1)};
				}
			}
			prefMatches[i % (K-1)][pref[i]] = i;
		}
	}

	for (int z = 0; z < 19; z++) {
		for (int i = 0; i < N; i++) {
			int j = int((i + dist[i][z].first) % N);
			dist[i][z+1] = {
				dist[i][z].first + dist[j][z].first,
				dist[i][z].second + dist[j][z].second,
			};
		}
	}
	int bestStart = -1;
	int ans = N+1;
	for (int i = 0; i < N; i++) {
		int cur = i;
		int cost = 0;
		for (int z = 19; z >= 0; z--) {
			if (cur + dist[cur % N][z].first < i + N) {
				cost += int(dist[cur % N][z].second);
				cur += int(dist[cur % N][z].first);
			}
		}
		assert(cur < i + N);
		cost += (i + N - cur - 1 + (K-2)) / (K-1);
		if (cost < ans) {
			ans = cost;
			bestStart = i;
		}
	}

	cerr << bestStart << '\n';
	cout << ans << '\n';

	for (int st = bestStart; st < bestStart + N; ) {
		int en = st + int(dist[st%N][0].first);
		en = min(en, bestStart + N);

		assert(pref[en] == pref[st]);
		while ((en - st - 1) % (K-1) != 0) {
			assert(en >= bestStart + N);
			pref[en+1] = pref[en];
			en++;
		}
		assert(pref[en] == pref[st]);
		cerr << st << ' ' << en << '\n';

		unordered_set<int> moved;
		auto canMove = [&](int a) {
			if (moved.count(a)) return false;
			int b = a + K;
			assert(st <= a && b <= en);
			assert((a - st) % (K-1) == 0);
			return (pref[a] - pref[st] <= 0 || pref[a+1] - pref[st] <= 0) && (pref[b] - pref[st] >= 0 || pref[b-1] - pref[st] >= 0);
		};
		unordered_set<int> moves;
		for (int a = st; a+K <= en; a += K-1) {
			if (canMove(a)) {
				moves.insert(a);
			}
		}
		while (!moves.empty()) {
			auto it = moves.begin();
			int a = *it;
			moves.erase(it);
			moved.insert(a);
			int b = a+K;
			for (int i = 1; i <= K-1; i++) {
				pref[a+i] = pref[st];
			}

			cout << (a%N);
			for (int i = 0; i < K; i++) {
				if (a+i < bestStart + N) {
					A[(a+i)%N] = pref[a+i+1] - pref[a+i];
				}
				cout << " " << A[(a+i)%N]+avg;
			}
			cout << '\n';

			if (a > st) {
				if (canMove(a - (K-1))) {
					moves.insert(a - (K-1));
				} else {
					moves.erase(a - (K-1));
				}
			}
			if (b < en) {
				if (canMove(a + (K-1))) {
					moves.insert(a + (K-1));
				} else {
					moves.erase(a + (K-1));
				}
			}
		}

		st = en;
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 100005;

int n, k;
int A = 0;
int a[2 * MX];
int need[2 * MX];

void Print(int L) {
	printf("%d ", (L - 1) % n);
	for (int i = L; i < L + k; i++) printf("%d ", a[i]);
	printf("\n");
}
void Set(int i, int x) {
	if (i <= n) {
		a[i] = a[i + n] = x;
	}
	else {
		a[i] = a[i - n] = x;
	}
}
void SetNeed(int i, int x) {
	if (i <= n) {
		need[i] = need[i + n] = x;
	}
	else {
		need[i] = need[i - n] = x;
	}
}
void Do(int L, int R)
{
	if (L > n) L -= n, R -= n;

	//printf("[%d %d]\n", L, R);

	if (R - L + 1 <= k) {
		bool ok = true;
		for (int i = L; i <= R; i++) {
			if (a[i] != need[i]) ok = false;
			Set(i, need[i]);
		}
		if (ok) return;

		Print(L);
		return;
	}

	int s = 0;
	for (int i = L; i < L + k; i++) s += a[i];
	int x = 0;
	for (int i = L; i < L + (k - 1); i++) x += need[i];

	//printf("s=%d, x=%d\n", s, x);

	if (s >= x) {
		for (int i = L; i < L + (k - 1); i++) {
			Set(i, need[i]);
		}
		Set(L + k - 1, s - x);
		Print(L);
		Do(L + k - 1, R);
	}
	else {
		SetNeed(L + k - 1, a[L + k - 1] + need[L + k - 1] + x - s);
		//printf("need[%d] = %d\n", L + k - 1, need[L + k - 1]);
		Do(L + k - 1, R);
		for (int i = L; i < L + k; i++) {
			Set(i, A);
		}
		Set(L, need[L]);
		Print(L);
	}
}

int p[2 * MX];

map<int, vector<int>> M;

int up[2 * MX][20];
int last[2 * MX];

int main()
{
	memset(up, -1, sizeof up);
	memset(last, -1, sizeof last);

	ignore = scanf("%d %d", &n, &k);

	for (int i = 1; i <= n; i++) {
		ignore = scanf("%d", a + i);
		A += a[i];
	}
	A /= n;
	for (int i = 1; i <= n; i++) {
		p[i] = p[i - 1] + a[i] - A;
		a[i + n] = a[i];
		p[i + n] = p[i];
		M[p[i]].push_back(i);
	}

	int ans = n;
	int from = 1;

	for (auto& P : M) {
		vector<int>& v = P.second;
		//printf("v = { "); for (int i : v) printf("%d ", i); printf("}\n");

		int m = (int)v.size();
		for (int i = 0; i < m; i++) v.push_back(v[i] + n);

		for (int i = (int)v.size() - 1; i >= 0; i--) {
			int t = v[i];
			up[t][0] = last[(t + 1) % (k - 1)];
			for (int j = 0; j + 1 < 20 && up[t][j] != -1; j++) {
				up[t][j + 1] = up[up[t][j]][j];
			}
			last[t % (k - 1)] = t;
		}

		for (int i = 0; i < m; i++) {
			int cnt = 0;
			int t = v[i];
			for (int j = 19; j >= 0; j--) {
				if (up[t][j] == -1) continue;
				if (up[t][j] >= v[i] + n) continue;
				cnt += (up[t][j] - t - (1 << j)) / (k - 1);
				t = up[t][j];
			}
			//printf("v[i] = %d, t = %d\n", v[i], t);
			cnt += (v[i] + n - t - 1 + (k - 2)) / (k - 1);
			//printf("cnt=%d\n", cnt);
			if (cnt <= ans) {
				ans = cnt;
				from = v[i];
			}
		}

		for (int i = 0; i < (int)v.size(); i++) {
			for (int j = 0; j < 20; j++) up[v[i]][j] = -1;
			last[v[i] % (k - 1)] = -1;
		}
	}

	//printf("from=%d, ans=%d\n", from, ans);
	printf("%d\n", ans);
	//for (int i = 1; i <= 2 * n; i++) printf("%d ", p[i]); printf("\n");

	for (int i = 1; i <= 2 * n; i++) need[i] = A;
	for (int i = from + 1, pr = from; i <= from + n; i++) {
		if (p[i] == p[pr] && (i == from + n || (i - pr - 1) % (k - 1) == 0)) {
			//printf("[%d %d]\n", pr + 1, i);
			Do(pr + 1, i);
			pr = i;
		}
	}
}

#include <cstdio>
#include <algorithm>

int n,k,a[100001],ave,ans,mx,Last[100001],len[100001],nxt[100001],val[100001];
struct point{
	int sum,ord;
}b[100001];
bool cmp(point a,point b){return a.sum<b.sum||(a.sum==b.sum&&a.ord<b.ord);}
void solve(int l,int r,long long res=0){
	int len=(l<=r)?r-l+1:(r-l+1+n);
//	printf("solve %d %d %d %lld\n",l,r,len,res);
	if(len==1)return;
	if(len<=k){
		printf("%d ",l);
		for(int i=0;i<len;i++){
			printf("%d ",a[(i+l)%n]=1ll*res*(i==0)+ave);
		}
		for(int i=len;i<k;i++)
			printf("%d ",a[(i+l)%n]);
		putchar('\n');
		return;
	}
	long long sum=0;
	for(int i=0;i<k;i++)
		sum+=a[(i+l)%n];
//	printf("%d %d\n",sum,res+1ll*(k-1)*ave);
	if(sum>=res+1ll*(k-1)*ave){
		printf("%d ",l);
		for(int i=0;i<k-1;i++)
			printf("%d ",a[(i+l)%n]=1ll*res*(i==0)+ave);
		printf("%d ",a[(k-1+l)%n]=sum-res-1ll*(k-1)*ave);
		putchar('\n');
		solve((l+k-1)%n,r,0);
		return;
	}
	else{
		solve((l+k-1)%n,r,len*ave+res-(len-k+1)*ave-sum+a[(l+k-1)%n]);
		printf("%d ",l);
		for(int i=0;i<k;i++)
			printf("%d ",a[(i+l)%n]=1ll*res*(i==0)+ave);
		putchar('\n');
		return;
	}
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)scanf("%d",a+i),ave+=a[i];
	ave/=n;
	for(int i=0;i<n;i++){
		b[i].sum=a[i]-ave;
		if(i)b[i].sum+=b[i-1].sum;
		b[i].ord=i;
	}
	mx=(n-1+k-1-1)/(k-1);
	std::sort(b,b+n,cmp);
	for(int i=0;i<=n;i++)val[i]=0x7f7f7f7f;
	for(int i=n%(k-1);i<=n;i+=k-1)
		if(i)val[i]=(n-i)/(k-1);
	for(int i=1;i<=n;i++)val[i]=std::min(val[i],val[i-1]);
	for(int i=0,last=0;i<n;i++)
		if(i==n-1||b[i+1].sum!=b[i].sum){
			for(int j=i;j>=last;j--){
				int u=(b[j].ord+1)%(k-1);
				nxt[b[j].ord]=Last[u];
				if(Last[u])len[b[j].ord]=len[Last[u]];
				++len[b[j].ord];
//				printf("%d %d %d %d\n",b[j].ord,nxt[b[j].ord],len[b[j].ord],val[len[b[j].ord]]);
				if(mx>val[len[b[j].ord]]){
					mx=val[len[b[j].ord]];
					ans=b[j].ord;
				}
				Last[b[j].ord%(k-1)]=b[j].ord;
			}
			for(int j=i;j>=last;j--)Last[b[j].ord%(k-1)]=0;
			last=i+1;
		}
	printf("%d\n",mx);
	if(mx<(n-1+k-1-1)/(k-1)){
		int p=0;
		for(int i=n%(k-1);i<=n;i+=k-1)
			if(val[i]==mx)p=i;
		for(int i=ans;p;i=nxt[i],--p){
//			printf("%d\n",i);
			solve((i==n-1)?0:(i+1),nxt[i]?nxt[i]:ans);
		}
	}
	else solve(0,n-1);
}

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 1.1e5;
int N, K;
int A[MAXN * 4];

int pref[MAXN * 4];

pair<ll, ll> dist[MAXN][20];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> N >> K;
	int tot = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		tot += A[i];
	}
	assert(tot % N == 0);
	int avg = tot / N;
	for (int i = 0; i < N; i++) { A[i] -= avg; }

	for (int i = 0; i < N; i++) { A[i + 3 * N] = A[i + 2 * N] = A[i + N] = A[i]; }

	pref[0] = 0;
	for (int i = 0; i < 4 * N; i++) { pref[i + 1] = pref[i] + A[i]; }

	{
		vector<map<int, int>> prefMatches(N);

		for (int i = 2 * N; i >= 0; i--) {
			if (i < N) {
				if (prefMatches[(i + 1) % (K - 1)].count(pref[i]) && prefMatches[(i + 1) % (K - 1)][pref[i]] <= i + N) {
					int l = prefMatches[(i + 1) % (K - 1)][pref[i]] - i;
					dist[i][0] = {l, (l - 1) / (K - 1)};
				} else {
					dist[i][0] = {N, (N - 1 + (K - 2)) / (K - 1)};
				}
			}
			prefMatches[i % (K - 1)][pref[i]] = i;
		}
	}

	for (int z = 0; z < 19; z++) {
		for (int i = 0; i < N; i++) {
			int j = int((i + dist[i][z].first) % N);
			dist[i][z + 1] = {
				dist[i][z].first + dist[j][z].first,
				dist[i][z].second + dist[j][z].second,
			};
		}
	}
	int bestStart = -1;
	int ans = N + 1;
	for (int i = 0; i < N; i++) {
		int cur = i;
		int cost = 0;
		for (int z = 19; z >= 0; z--) {
			if (cur + dist[cur % N][z].first < i + N) {
				cost += int(dist[cur % N][z].second);
				cur += int(dist[cur % N][z].first);
			}
		}
		assert(cur < i + N);
		cost += (i + N - cur - 1 + (K - 2)) / (K - 1);
		if (cost < ans) {
			ans = cost;
			bestStart = i;
		}
	}

	cerr << bestStart << '\n';
	cout << ans << '\n';

	for (int st = bestStart; st < bestStart + N;) {
		int en = st + int(dist[st % N][0].first);
		en = min(en, bestStart + N);

		assert(pref[en] == pref[st]);
		while ((en - st - 1) % (K - 1) != 0) {
			assert(en >= bestStart + N);
			pref[en + 1] = pref[en];
			en++;
		}
		assert(pref[en] == pref[st]);
		cerr << st << ' ' << en << '\n';

		unordered_set<int> moved;
		auto canMove = [&](int a) {
			if (moved.count(a)) return false;
			int b = a + K;
			assert(st <= a && b <= en);
			assert((a - st) % (K - 1) == 0);
			return (pref[a] - pref[st] <= 0 || pref[a + 1] - pref[st] <= 0) &&
				(pref[b] - pref[st] >= 0 || pref[b - 1] - pref[st] >= 0);
		};
		unordered_set<int> moves;
		for (int a = st; a + K <= en; a += K - 1) {
			if (canMove(a)) { moves.insert(a); }
		}
		while (!moves.empty()) {
			auto it = moves.begin();
			int a = *it;
			moves.erase(it);
			moved.insert(a);
			int b = a + K;
			for (int i = 1; i <= K - 1; i++) { pref[a + i] = pref[st]; }

			cout << (a % N);
			for (int i = 0; i < K; i++) {
				if (a + i < bestStart + N) { A[(a + i) % N] = pref[a + i + 1] - pref[a + i]; }
				cout << " " << A[(a + i) % N] + avg;
			}
			cout << '\n';

			if (a > st) {
				if (canMove(a - (K - 1))) {
					moves.insert(a - (K - 1));
				} else {
					moves.erase(a - (K - 1));
				}
			}
			if (b < en) {
				if (canMove(a + (K - 1))) {
					moves.insert(a + (K - 1));
				} else {
					moves.erase(a + (K - 1));
				}
			}
		}

		st = en;
	}

	return 0;
}

#include<bits/stdc++.h>

#define mp make_pair
#define PI pair<int,int>
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
inline char gc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read()
{
    ll x = 0;
    char ch = gc();
    bool positive = 1;
    for (; !isdigit(ch); ch = gc())
        if (ch == '-')
            positive = 0;
    for (; isdigit(ch); ch = gc())
        x = x * 10 + ch - '0';
    return positive ? x : -x;
}
inline void write(ll a)
{
    if(a<0)
    {
        a=-a;
        putchar('-');
    }
    if(a>=10)
        write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a)
{
    write(a);
    puts("");
}
inline void wri(ll a)
{
    write(a);
    putchar(' ');
}
inline ull rnd()
{
    return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=100005;
int n,k,a[N],b[N<<1],c[N<<1],dp[N],sum,ans;
map<int,int> M[N];
int chu(int x)
{
    return (x-1)%n;
}
void Solve(int l,int r)
{
    c[l-1]=0;
    For(i,l,r)c[i]=c[i-1]+b[i]-sum;
    int i=l,j=l+(k-1);
    while(j-(k-2)<=r)
    {
        if(c[j]>=0)
        {
            for(int o=j-(k-1); o>=i; o-=k-1)
            {
                wri(chu(o));
                int su=-c[o];
                wri(b[o]+=su);
                For(p,o+1,o+(k-2))
                {
                    if(p<=n)
                    {
                        su+=sum-b[p];
                        wri(sum);
                        b[p]=sum;
                    }
                    else
                        wri(b[p-n]);
                }
                if(o+k-1>n)
                    writeln(b[o+k-1-n]);
                else
                    writeln(b[o+(k-1)]-=su);
            }
            i=j;
        }
        j+=k-1;
    }
}
void solve(int l,int r)
{
    c[l-1]=0;
    For(i,l,r)c[i]=c[i-1]+b[i]-sum;
    int i=l,j=l+(k-1);
    while(j<=r)
    {
        if(c[j]>=0)
        {
            for(int o=j-(k-1); o>=i; o-=k-1)
            {
                wri(chu(o));
                int su=-c[o];
                wri(b[o]+=su);
                For(p,o+1,o+(k-2))
                {
                    su+=sum-b[p];
                    wri(sum);
                }
                writeln(b[o+(k-1)]-=su);
            }
            i=j;
        }
        j+=k-1;
    }
}
int main()
{
    n=read(),k=read();
    For(i,1,n)
    {
        b[i+n]=b[i]=a[i]=read();
        sum+=a[i];
    }
    sum/=n;
    For(i,1,n)a[i]=a[i]-sum+a[i-1];
    For(i,1,n)
    {
        auto it=M[(i-1)%(k-1)].find(a[i]);
        if(it!=M[(i-1)%(k-1)].end())
            dp[i]=it->second+1;
        else
            dp[i]=1;
        M[i%(k-1)][a[i]]=max(M[i%(k-1)][a[i]],dp[i]);
        ans=max(ans,dp[i]);
    }
    int jb=(n-ans+k-2)/(k-1);
    writeln(jb);
    if((n-1+k-2)/(k-1)==jb)
    {
        Solve(1,n);
        return 0;
    }
    int an=n-1-jb*(k-1);
    Rep(i,n,1)if(dp[i]==ans)
    {
        int j=i;
        For(o,1,an)
        {
            int O=j-1;
            while((j-O-1)%(k-1)||a[j]!=a[O]||dp[O]+1!=dp[j])
                O--;
            solve(O+1,j);
            j=O;
        }
        solve(i+1,n+j);
        return 0;
    }
}

#include <cstdio>
#include <iostream>
#include <map>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define pos(x) ((x-1)%n)
using namespace std;
inline char nc()
{
    return getchar();
    static char buf[100000],*l=buf,*r=buf;
    return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x)
{
    x=0; int f=1,ch=nc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
    while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
    x*=f;
}
template<class T> bool Cmax(T &x,T y) {return x<y?x=y,1:0;}
const int maxn=1e5+50;
int n,k;
int a[maxn<<1];
int A;
int f[maxn];
int p[maxn<<1];
map<int,int> val[maxn];
void sol(int L,int R)
{
    // debug("%d %d\n",L,R);
    p[L]=a[L]-A;
    for(int i=L+1;i<=R;++i) p[i]=p[i-1]+a[i]-A;
    for(int i=L,j=L+k-1;j<=R;j+=k-1) if(p[j]>=0)
    {
        for(int l=j-k+1,r=j;l>=i;l-=k-1,r-=k-1)
        {
            printf("%d ",pos(l));
            int sum=-p[l];
            printf("%d ",a[l]+=sum);
            for(int k=l+1;k<r;++k)
            {
                sum+=A-a[k];
                printf("%d ",a[k]=A);
            }
            printf("%d\n",a[r]-=sum);
        }
        i=j;
    }
}
int main()
{
    read(n),read(k);
    for(int i=1;i<=n;++i)
    {
        read(a[i]);
        a[i+n]=a[i];
        A+=a[i];
    }
    A/=n;
    for(int i=1;i<=n;++i)
    {
        p[i]=p[i-1]+a[i]-A;
    }
    int mx=0;
    for(int i=1;i<=n;++i)
    {
        f[i]=val[(i-1)%(k-1)][p[i]]+1;
        val[i%(k-1)][p[i]]=f[i];
        Cmax(mx,f[i]);
    }
    int an=(n-mx+k-2)/(k-1);
    printf("%d\n",an);
    if((n-1+k-2)/(k-1)==an)
    {
        // debug("---\n");
        for(int i=1,j=k;i<n;j+=k-1) if(p[j]>=0)
        {
            for(int l=j-k+1,r=j;l>=i;l-=k-1,r-=k-1)
            {
                printf("%d ",pos(l));
                int sum=-p[l];
                printf("%d ",a[l]+=sum);
                for(int k=l+1;k<r;++k)
                {
                    if(k<=n)
                    {
                        sum+=A-a[k];
                        printf("%d ",a[k]=A);
                    }
                    else
                    {
                        printf("%d ",a[k-n]);
                    }
                }
                if(r>n) printf("%d\n",a[r-n]);
                else printf("%d\n",a[r]-=sum);
            }
            i=j;
        }
        return 0;
    }
    an=n-1-an*(k-1);
    // debug("%d\n",an);
    for(int i=n;i>=1;--i) if(f[i]==mx)
    {
        // debug("---\n");
        int j=i;
        for(int o=1;o<=an;++o)
        {
            int h=j-1;
            while(!((j-h-1)%(k-1)==0&&p[j]==p[h]&&f[h]+1==f[j])) --h;
            sol(h+1,j);
            j=h;
        }
        sol(i+1,n+j);
        return 0;
    }
}

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
//#define NDEBUG
#include <cassert>

const int MN = 1e5 + 100;

int l2(int x)
{
	return 31 - __builtin_clz(x);
}
int N, K, A, a[MN], p[MN*2], x[MN], X, l[MN*2], q[MN*2][20], F, C;
std::vector<int> v[MN];

int md(int x) {
	if(x<N) return x;
	if((x-=N)<N) return x;
	return x%N;
}
void solve(int p, int e = 0)
{
	int s = 0;
	if(p+K > F+N)
	{
		int k = F+N-p;
		for(int i = 0;i < k;++i)
			s += a[md(p+i)];
		assert(s==k*A+e);
		printf("%d ", p%N);
		printf("%d ", a[md(p)] = A+e);
		for(int i = 1;i < k;++i)
			printf("%d ", a[md(p+i)] = A);
		for(int i = k;i < K;++i)
			printf("%d%c", a[md(p+i)], " \n"[i+1==K]);
		return;
	}
	for(int i = 0;i < K;++i)
		s += a[md(p+i)];
	if((s -= A*(K-1) + e) < 0)
		solve(p+K-1, -(s-=a[md(p+K-1)])), s += a[md(p+K-1)];
	printf("%d ", p%N);
	printf("%d ", a[md(p)] = A+e);
	for(int i = 1;i < K-1;++i)
		printf("%d ", a[md(p+i)] = A);
	printf("%d\n", a[md(p+K-1)] = s);
}

int main(void)
{
	memset(l, -1, sizeof(l));
	memset(q, -1, sizeof(q));
	scanf("%d%d", &N, &K);
	C = N;
	for(int i = 0;i < N;++i) scanf("%d", a+i), A+=a[i];
	A/=N;
	for(int i = 0;i < N;++i) p[i+1] = p[i]+a[i]-A, x[i] = p[i+1];
	std::sort(x, x+N);
	X = std::unique(x, x+N)-x;
	for(int i = 0;i < N;++i)
		p[i] = std::lower_bound(x, x+X, p[i])-x, v[p[i]].push_back(i);//cut from i-1 -> i
	for(int y = 0, V;y < X;++y)
	{
		std::vector<int> &v = ::v[y];
		V = v.size();
		for(int i = 0;i < V;++i) v.push_back(v[i]+N);
		for(int i = v.size()-1;i >= 0;--i)
		{
			q[i][0] = l[(v[i]+1)%(K-1)];
			for(int j = 0;~q[i][j]&&~q[q[i][j]][j];++j)
				q[i][j+1] = q[q[i][j]][j];
			l[v[i]%(K-1)] = i;
		}
		for(int i = 0, n, c;i < V;++i)
		{
			n = i, c = 0;
			for(int j = l2(V), k;j >= 0;--j)
				if(~q[n][j] && (k=q[n][j])<i+V)
					c += (v[k]-v[n]-(1<<j))/(K-1), n = k;
			c += (v[i]+N-v[n]+K-3)/(K-1);
			if(c < C || !(C < c) && v[i] < F)
				C = c, F = v[i];
		}
		for(int i = 0;i < v.size();++i)
			for(int j = 0;~q[i][j];++j)
				q[i][j] = -1;
		for(int x : v)
			l[x%(K-1)] = -1;
	}

	printf("%d\n", C);
	for(int i = F;i < F+N;++i)
		if(a[md(i)] != A)
			solve(i);
	return 0;
}

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int cnt = 0, i, already = 0;
      for (i = tans.second; cnt < n; ) {
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second && ans.size() < tans.first; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        cnt += nxt[i][0].second.second;
        i = nxt[i][0].first;
      }
      for (int i = 0; i < tans.first; i++) {
    	  for (int a : ans[i]) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,M=20; vector<int>v[N]; bool fc[N];
int n,k,lf,rf,len,ans,pos,opt,les,s[N],q[N],w[N],c[N],nxt[N],f[N][M],g[N][M]; map<int,int>ft[N];
struct node {int l,r,x,t;}e[N],h[N];
inline bool operator < (node a,node b){
	if(a.x==b.x) return a.t<b.t; return a.x<b.x;
}
set<node>rt; set<int>gt;
inline int read(){
	int num=0; char g=getchar(); while(g<48||57<g) g=getchar();
	while(47<g&&g<58) num=(num<<1)+(num<<3)+g-48,g=getchar(); return num;
}
inline void write(long long u){
	if(u<0) putchar('-'),u=-u; if(u>9) write(u/10); putchar(u%10+'0');
}
inline int lowbit(int u){
	return (u&(-u));
}
inline void addit(int u,int x){
	while(u<=n) s[u]+=x,u+=lowbit(u);
}
inline int getit(int u){
	int ans=0; while(u>0) ans+=s[u],u-=lowbit(u); return ans;
}
inline int getans(int l,int r){
	while(l>=n&&r>=n) l-=n,r-=n;
	if(r<l) return 0; if(r<n) return getit(r+1)-getit(l); return getit(n)-getit(l)+getit(r-n+1);
}
inline void change(int u,int x){
	u=u%n; addit(u+1,x-w[u]),w[u]=x;
}
inline int getpos(int u){
	return w[u%n];
}
inline void dele(int u){
	gt.insert(u);
	node g=h[u]; g.x=getans(h[u].l,h[u].r); rt.erase(g);
	gt.erase(u);
}
inline void solve(int l,int r,int t){
	if(t>les) return;
	int a=getans(lf,h[t].l-1),b=getans(h[t].r+1,rf);
	if(opt-a<0||opt-b<0){
		solve(h[t+1].l,r,t+1);
		q[++q[0]]=h[t].l%n; v[q[0]].resize(k+1);
		for(int x=h[t].l;x<=h[t].r;x++) change(x,0);
		change(h[t].l,-a);
		for(int x=h[t].l;x<h[t].l+k;x++) v[q[0]][x-h[t].l+1]=getpos(x);
	}
	else{
		for(int x=h[t].l;x<=h[t].r;x++) change(x,0);
		change(h[t].l,-a),change(h[t].r,-b);
		q[++q[0]]=h[t].l%n; v[q[0]].resize(k+1);
		for(int x=h[t].l;x<h[t].l+k;x++) v[q[0]][x-h[t].l+1]=getpos(x);
		solve(h[t+1].l,r,t+1);
	}
}
inline void delit(int l,int r){
	if(l==r) return; lf=l,rf=r;
	int u=l,len=0; u=min(u+k-1,r),len++,h[len].l=l,h[len].r=u;
	while(u<r) len++,h[len].l=u,u=min(u+k-1,r),h[len].r=u; les=len;
	for(int i=1;i<=len;i++){
		h[i].t=i;
		node v; v.l=h[i].l,v.r=h[i].r,v.t=i,v.x=getans(h[i].l,h[i].r); rt.insert(v);
	}
	solve(lf,rf,1);
}
int main(){
	n=read(),k=read(); int sum=0; ans=19260817,pos=0;
	for(int i=0;i<n;i++) w[i]=w[i+n]=read(),sum+=w[i]; sum=sum/n;
	for(int i=0;i<(n<<1);i++) w[i]-=sum; c[0]=w[0]; opt=sum;
	for(int i=1;i<(n<<1);i++) c[i]=c[i-1]+w[i];
	for(int i=1;i<=n;i++) addit(i,w[i-1]);
	for(int x=0;x<=18;x++) f[(n<<1)][x]=(n<<1);
	for(int i=(n<<1)-1;i>=0;i--){
		nxt[i]=ft[i%(k-1)][c[i]]; if(!nxt[i]) nxt[i]=(n<<1);
		f[i][0]=nxt[i],g[i][0]=(nxt[i]-i-1)/(k-1);
		for(int x=1;x<=18;x++) f[i][x]=f[f[i][x-1]][x-1],g[i][x]=g[i][x-1]+g[f[i][x-1]][x-1];
		if(i<n){
			int to=i+n-1,u=i,sum=0;
			for(int x=18;x>=0;x--) while(f[u][x]<=to) sum+=g[u][x],u=f[u][x];
			sum=sum+(to-u)/(k-1)+((to-u)%(k-1)>0);
			if(sum<=ans) ans=sum,pos=i;
		}
		if(i>0) ft[(i-1)%(k-1)][c[i]]=i;
	}
	int to=pos+n-1;
	while(nxt[pos]<=to) len++,e[len].l=pos+1,e[len].r=nxt[pos],pos=nxt[pos];
	len++,e[len].l=pos+1,e[len].r=to+1;
	for(int i=1;i<=len;i++){
		delit(e[i].l,e[i].r);
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		write(q[i]),putchar(' ');
		for(int x=1;x<=k;x++) write(v[i][x]+sum),putchar(' '); putchar('\n');
	}
	return 0;
}
//6 2
//0 1 0 1 4 0

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rsz resize
#define ins insert

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

namespace input {
	template<class T> void re(complex<T>& x);
	template<class T1, class T2> void re(pair<T1,T2>& p);
	template<class T> void re(vector<T>& a);
	template<class T, size_t SZ> void re(array<T,SZ>& a);

	template<class T> void re(T& x) { cin >> x; }
	void re(double& x) { string t; re(t); x = stod(t); }
	void re(ld& x) { string t; re(t); x = stold(t); }
	template<class T, class... Ts> void re(T& t, Ts&... ts) {
		re(t); re(ts...);
	}

	template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
	template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
	template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
	template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

using namespace input;

namespace output {
	void pr(int x) { cout << x; }
	void pr(long x) { cout << x; }
	void pr(ll x) { cout << x; }
	void pr(unsigned x) { cout << x; }
	void pr(unsigned long x) { cout << x; }
	void pr(unsigned long long x) { cout << x; }
	void pr(float x) { cout << x; }
	void pr(double x) { cout << x; }
	void pr(ld x) { cout << x; }
	void pr(char x) { cout << x; }
	void pr(const char* x) { cout << x; }
	void pr(const string& x) { cout << x; }
	void pr(bool x) { pr(x ? "true" : "false"); }
	template<class T> void pr(const complex<T>& x) { cout << x; }

	template<class T1, class T2> void pr(const pair<T1,T2>& x);
	template<class T> void pr(const T& x);

	template<class T, class... Ts> void pr(const T& t, const Ts&... ts) {
		pr(t); pr(ts...);
	}
	template<class T1, class T2> void pr(const pair<T1,T2>& x) {
		pr("{",x.f,", ",x.s,"}");
	}
	template<class T> void pr(const T& x) {
		pr("{"); // const iterator needed for vector<bool>
		bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0;
		pr("}");
	}

	void ps() { pr("\n"); } // print w/ spaces
	template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
		pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
	}

	void pc() { pr("]\n"); } // debug w/ commas
	template<class T, class... Ts> void pc(const T& t, const Ts&... ts) {
		pr(t); if (sizeof...(ts)) pr(", "); pc(ts...);
	}
	#define dbg(x...) pr("[",#x,"] = ["), pc(x);
}

using namespace output;

namespace io {
	void setIn(string s) { freopen(s.c_str(),"r",stdin); }
	void setOut(string s) { freopen(s.c_str(),"w",stdout); }
	void setIO(string s = "") {
		cin.sync_with_stdio(0); cin.tie(0); // fast I/O
		// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
		if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
	}
}

using namespace io;

typedef decay<decltype(MOD)>::type T;
struct mi {
	T val;
	explicit operator T() const { return val; }
	mi() { val = 0; }
	mi(const ll& v) {
		val = (-MOD <= v && v <= MOD) ? v : v % MOD;
		if (val < 0) val += MOD;
	}
	// friend ostream& operator<<(ostream& os, const mi& a) {
		// return os << a.val; }
	friend void pr(const mi& a) { pr(a.val); }
	friend void re(mi& a) { ll x; re(x); a = mi(x); }

	friend bool operator==(const mi& a, const mi& b) {
		return a.val == b.val; }
	friend bool operator!=(const mi& a, const mi& b) {
		return !(a == b); }
	friend bool operator<(const mi& a, const mi& b) {
		return a.val < b.val; }

	mi operator-() const { return mi(-val); }
	mi& operator+=(const mi& m) {
		if ((val += m.val) >= MOD) val -= MOD;
		return *this; }
	mi& operator-=(const mi& m) {
		if ((val -= m.val) < 0) val += MOD;
		return *this; }
	mi& operator*=(const mi& m) {
		val = (ll)val*m.val%MOD; return *this; }
	friend mi pow(mi a, ll p) {
		mi ans = 1; assert(p >= 0);
		for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans;
	}
	friend mi inv(const mi& a) {
		assert(a != 0); return pow(a,MOD-2); }
	mi& operator/=(const mi& m) { return (*this) *= inv(m); }

	friend mi operator+(mi a, const mi& b) { return a += b; }
	friend mi operator-(mi a, const mi& b) { return a -= b; }
	friend mi operator*(mi a, const mi& b) { return a *= b; }
	friend mi operator/(mi a, const mi& b) { return a /= b; }
};

typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

int k,n,avg;
vi a;
vi cum = {0};
pi ans = {0,0};
vector<pair<int,vi>> meet;
map<int,vi> oc;
vi nex[MX];

int getNum(int t) {
	int rem = n%(k-1);
	int cur = t, res = 0;
	for (int ind = sz(nex[t])-1; ind >= 0; --ind) {
		if (ind < sz(nex[cur]) && nex[cur][ind] <= t+n) {
			res ^= 1<<ind;
			cur = nex[cur][ind];
		}
	}
	if (res >= rem) {
		res -= (res-rem)%(k-1);
		return res;
	} else return -1;
}

void solve(vi v) {
	int s = sz(v);
	FOR(i,s,2*s) v.pb(v[i-s]+n);
	reverse(all(v));
	map<int,int> stor;
	// ps("HA",v);
	trav(t,v) {
		int r = t%(k-1);
		int R = (r+1)%(k-1);
		nex[t].clear();
		if (stor.count(R)) nex[t].pb(stor[R]);
		else nex[t].pb(MOD);
		while (nex[t].back() != MOD) {
			int s = sz(nex[t])-1;
			if (s < sz(nex[nex[t][s]])) nex[t].pb(nex[nex[t][s]][s]);
			else nex[t].pb(MOD);
		}
		if (t < n) ckmax(ans,{getNum(t),t});
		stor[r] = t;
	}
}

void ok(vi& v, int l, int r, int des) {
	int sum = 0; FOR(i,l,r+1) sum += a[v[i]];
	sum -= des+(r-1-l)*avg;
	assert(sum >= 0);
	meet.eb(); meet.back().f = v[l];
	meet.back().s.pb(des);
	F0R(i,r-1-l) meet.back().s.pb(avg);
	meet.back().s.pb(sum);
	while (sz(meet.back().s) < k) {
		meet.back().s.pb(a[(meet.back().f+sz(meet.back().s))%n]);
	}
	F0R(i,k) a[(meet.back().f+i)%n] = meet.back().s[i];
	// l should have des
	// l+1 to r-1 should have avg
}

void solve(vi& v, int l, int des) {
	if (l == sz(v)-1) {
		if(a[v[l]] != des) {
			ps("HUH",v);
			ps(l,a[v[l]],des);
			exit(0);
		}
		return;
	}
	int r = min(l+k,sz(v))-1;
	int tot = 0; FOR(i,l,r+1) tot += a[v[i]];
	// l+1 to r-1
	int need = (r-l-1)*avg+des;
	if (tot < need) { // need+avg = tot+(?-a[v[r]])
		solve(v,r,avg+need-tot+a[v[r]]);
		ok(v,l,r,des);
	} else {
		ok(v,l,r,des);
		solve(v,r,avg);
	}
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	re(n,k); a.rsz(n); re(a);
	trav(t,a) avg += t;
	assert(avg%n == 0);
	avg /= n;
	trav(t,a) cum.pb(cum.back()+t-avg);
	F0R(i,n) oc[cum[i]].pb(i);
	trav(t,oc) solve(t.s);
	vector<vi> res; res.eb();
	int sum = 0, cur = ans.s;
	F0R(i,n) {
		res.back().pb(cur); sum += a[cur]; cur = (cur+1)%n;
		if (sz(res) < ans.f && sum == avg*(i+1) && (sz(res.back())%(k-1) == 1 || k == 2)) res.eb();
	}
	// ps("HUH",ans,sz(res),res); exit(0);
	if (ans.f) assert(sz(res) == ans.f);
	trav(t,res) solve(t,0,avg);
	// ps(a);
	ps(sz(meet));
	trav(t,meet) {
		pr(t.f);
		trav(z,t.s) pr(' ',z);
		ps();
	}
	// you should actually read the stuff at the bottom
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?), slow multiset operations
	* do smth instead of nothing and stay organized
*/

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,k,ave,a[N],sa[N],i,j,anc[20][N],step[N],mn=N,mnid,u,v,l;
unordered_map<int,int>lst[N];
vector<vector<int>>ans;
int getsum(int s,int k){int ans=0;for(int i=0;i<k;++i)ans+=a[s+i];return ans;}
int RR;
void opt(int s,int v,int k){
	int tot=0,i;
assert(k>1);
	a[s+k-1]=getsum(s,k)-v-ave*(k-2);a[s]=v;for(i=1;i+1<k;++i)a[s+i]=ave;
	vector<int>ve={(s-1+n-(::k-k))%n};int kk=k;
	for(;kk<::k;++kk)ve.push_back(a[RR--]);reverse(ve.begin()+1,ve.end());
	ve.insert(ve.end(),a+s,a+s+k);
	ans.push_back(ve);
}
void solve(int l,int r){
	int i,tot=getsum(max(r-k+1,l),min(k,r-l+1));
	for(i=r-k+1;i>=l && tot*1.0/(r-i+1)<ave;i-=k-1,tot+=getsum(i,k-1));
int tt=i;
	if(i<l){
		if(i+k-1>l)RR=mnid,opt(l,ave,i+k-1-l+1);
	}else{
		opt(i,getsum(i,r-i+1)-ave*(r-i),k);
	}
	for(;i+=k-1,i<r;)opt(i,ave,k);
	if(l<tt)solve(l,tt);
}
int oa[N];
void spj(){
	for(auto u:ans){
		int s1=0,s2=0;
		assert(u.size()==k+1);
		for(int i=0;i<k;++i)s1+=oa[(u[0]+i)%n],s2+=u[1+i],assert(u[1+i]>=0);
		assert(s1==s2);
		for(int i=0;i<k;++i)oa[(u[0]+i)%n]=u[1+i];
	}
	for(int i=0;i<n;++i)assert(oa[i]==ave);
}
int main(){
	//freopen("1","r",stdin);
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;++i)scanf("%d",a+i),ave+=a[i+n]=a[i];
	ave/=n;
memcpy(oa,a+1,n<<2);
	for(i=1;i<=n*2;++i){
		sa[i]=sa[i-1]+a[i]-ave;
		anc[0][i]=lst[(i-1)%(k-1)][sa[i]];
		step[i]=anc[0][i]?step[anc[0][i]]+(i-anc[0][i]-1)/(k-1):0;
		lst[i%(k-1)][sa[i]]=i;
	}
	for(i=1;i<20;++i)for(j=1;j<=n*2;++j)anc[i][j]=anc[i-1][anc[i-1][j]];
	for(i=n+1;i<=n*2;++i){
		for(u=i,j=19;j>=0;--j)if(anc[j][u]>=i-n)u=anc[j][u];
		v=step[i]-step[u];l=u-(i-n);
		if(l)v+=l<=k?1:(l-k+k-2)/(k-1)+1;
		if(v<mn)mn=v,mnid=i;
	}
	for(i=mnid;i>mnid-n;i=anc[0][i])if(anc[0][i]+1<i)solve(max(mnid-n,anc[0][i])+1,i);
	printf("%d\n",int(ans.size()));
	assert(ans.size()==mn);
	for(auto u:ans){for(int x:u)printf("%d ",x);puts("");}
spj();
	return 0;
}

#ifndef _GLIBCXX_DEBUG
#define NDEBUG
#endif

#include<iostream>
#include<algorithm>
#include<cassert>
#include<vector>
#include<unordered_map>

std::vector<int> a;
int maxd;
int n,k;

auto print_op(int x){
	std::cout<<x%n;
	for(int i=0;i<k;++i)
		std::cout<<' '<<a[(x+i)%n]-a[(x+i+n-1)%n]+maxd;
	std::cout<<'\n';
}

void process(int l,int r){
	assert(std::cerr<<"process "<<l<<' '<<r<<'\n');
	assert(a[l%n]==0&&a[r%n]==0&&l<r);
	if(l+1==r)
		return;

	int x=r;
	do{
		x-=k-1;
	}while(x>l&&a[x%n]>0);

	if(x>l){
		for(int i=0;i<k-1;++i)
			a[(x+i)%n]=0;

		print_op(x);

		process(x+k-2,r); // all high. contains some redundant check
		process(l,x);
		return;
	}

	x+=k-1;
	if(l+1<x){
		for(int i=l+1;i<x;++i)
			a[i%n]=0;
		print_op(l+1);
	}

	for(;x!=r;x+=k-1){
		for(int i=0;i<k-1;++i)
			a[(x+i)%n]=0;
		print_op(x);
	}
}

int main(){
	std::ios::sync_with_stdio(0);std::cin.tie(0);
	std::cin>>n>>k;
	a.resize(n);
	for(int& x:a)std::cin>>x;

	//auto olda=a;

	for(int i=1;i<n;++i)
		a[i]+=a[i-1];

	maxd=a.back()/n;
	for(int i=0;i<n;++i)
		a[i]-=(i+1)*maxd;

	std::vector<int> f(n),g(n);
	// f[i] = min fi s.t. i+1 == fi mod k-1, a[i] == a[fi%n]
	// g[i] = min gi > i s.t. a[gi] != a[i]

	std::vector<std::unordered_map<int,int>> colmod(k-1);
	// colmod[mod][col] = min index i > current index, == mod (mod k-1), a[i]==col
	for(int i=2*n,
			lastsplit=2*n; // a[lastsplit] != a[lastsplit-1]
			i--;){

		colmod[i%(k-1)][a[i%n]]=i;

		auto iter=colmod[i%(k-1)].find(a[(i+n-1)%n]);
		if(i<n)
			f[i]=iter==end(colmod[i%(k-1)]) ? 2*n : iter->second;

		if(a[i%n]!=a[(i+1)%n])
			lastsplit=i+1;

		if(i<n)
			g[i]=lastsplit;
	}

	struct jumpentry{
		int dlen,nblock;
	};
	std::vector<std::vector<jumpentry>> jt;
	jt.resize(1);

	//g.resize(n+1);g[n]=n; // placeholder

	{
		auto& jump=jt[0];
		//jump.resize(n+1);jump[n]={n+1,n+1}; // placeholder
		jump.resize(n);
		for(int i=0;i<n;++i){
			int fi=f[i];
			if(fi<2*n){
				assert((fi-i)%(k-1)==0);
				jump[i].nblock=(fi-i)/(k-1);
				jump[i].dlen= ( fi>=n ? g[fi-n]+n : g[fi] ) - i;
			}else{
				jump[i]={2*n+1,2*n+1};
			}
		}
	}

	while(true){
		auto const& a=jt.back();
		auto b=a;
		bool useful=false;
		for(int i=0;i<n;++i){
			jumpentry& e=b[i];
			jumpentry nexte=a[(e.dlen+i)%n];

			e.dlen+=nexte.dlen;
			e.nblock+=nexte.nblock;
			if(e.dlen<=n)
				useful=true;
			else
				e.dlen=n+1;
		}
		if(useful)
			jt.push_back(std::move(b));
		else
			break;
	}

	auto const ceildiv=[](int a,int b){return (a+b-1)/b;};
	int ans=ceildiv(n-1,k-1);
	int besti=-1;

	for(int i=0;i<n;++i){
		int nblock=0;
		int dlen=0;
		int x=i;
		for(auto j=jt.size();j--;){
			auto e=jt[j][x];
			if(e.dlen+dlen<n){
				dlen+=e.dlen;
				x=(x+e.dlen)%n;
				nblock+=e.nblock;
			}
		}
		assert(dlen<n);
		dlen+=jt[0][x].dlen;
		nblock+=jt[0][x].nblock;
		assert(dlen>=n);
		if(nblock<ans){
			ans=nblock;
			besti=i;
		}
	}

	std::cout<<ans<<'\n';

	if(besti<0){
		std::transform(begin(a),end(a),begin(a),[x=a[0]](int y){return y-x;});
		process(0,n);
	}else{
		auto const& jump=jt[0];
		std::transform(begin(a),end(a),begin(a),[x=a[(besti+n-1)%n]](int y){return y-x;});

		int nblock=0;
		int dlen=0;
		int x=besti;

		while(nblock<ans){
			auto e=jump[x];
			if(nblock+e.nblock<=ans){
				process(x+n-1, x+n+e.nblock*(k-1));
				dlen+=e.dlen;
				x=(x+e.dlen)%n;
				nblock+=e.nblock;
			}
		}
		assert(ans==nblock);
		assert(ans==0||dlen>=n);
	}
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define REP(i,s,t) for(int i=s;i<=t;i++)
using namespace std;
const int maxn=2e5+5;
typedef pair<int,int> pii;
pii seq[maxn];
int A,p[maxn],n,siz,a[maxn],pos[maxn],f[maxn][20],ans=0x7fffffff,ans_pos;
void upd(int val,int p){
	if(val<ans) ans=val,ans_pos=p;
}
int Ceil(int a,int b){
	int c=a/b;
	if(a==b*c) return c;
	return c+1;
}
void print(int nl,int nr,int add){
	if(nl==nr) return;
	int cnt=(nr+1)%n==nl?n:(nr-nl+1+n)%n;
	if(cnt<=siz){
		cout<<nl<<' '<<(a[nl]=A+add)<<' ';
		REP(i,1,cnt-1) cout<<(a[(nl+i)%n]=A)<<' ';
		REP(i,1,siz-cnt) cout<<a[(nr+i)%n]<<' ';
		cout<<'\n';
		return;
	}
	int sum=0;
	REP(i,0,siz-1) sum+=a[(nl+i)%n];
	if(sum>=A*(siz-1)+add){
		cout<<nl<<' '<<(a[nl]=A+add)<<' ';
		REP(i,1,siz-2) cout<<(a[(nl+i)%n]=A)<<' ';
		cout<<(a[(nl+siz-1)%n]=sum-A*(siz-1)-add)<<'\n';
		print((nl+siz-1)%n,nr,0);
	}else{
		print((nl+siz-1)%n,nr,A*(siz-1)+add-(sum-a[(nl+siz-1)%n]));
		cout<<nl<<' '<<(a[nl]=A+add)<<' ';
		REP(i,1,siz-1) cout<<(a[(nl+i)%n]=A)<<' ';
		cout<<'\n';
	}
}
int main(){
#ifdef LOCAL
	freopen("G.in","r",stdin);
#endif
	cin>>n>>siz;
	REP(i,0,n-1) cin>>a[i],A+=a[i];
	A/=n;
	p[0]=a[0];
	REP(i,1,n-1) p[i]=p[i-1]+a[i];
	REP(i,0,n-1) p[i]-=A*(i+1);
	REP(i,0,n-1) seq[i]=make_pair(p[i],i);
	sort(seq,seq+n);
	upd(Ceil(n-1,siz-1),-1);
	REP(i,0,n-1){
		int j=i;
		while(j<n-1&&seq[j+1].fi==seq[j].fi) j++;
		int c=j-i+1;
		REP(k,1,c-1) pos[k]=pos[k-1]+seq[i+k].se-seq[i+k-1].se;
		REP(k,c,c*2-1) pos[k]=pos[k-c]+n;
		map<int,int> mp;
		for(int k=c*2-1;k>=0;k--){
			if(mp.find((pos[k]+1)%(siz-1))==mp.end()) f[k][0]=-1;
			else f[k][0]=mp[(pos[k]+1)%(siz-1)];
			mp[pos[k]%(siz-1)]=k;
		}
		REP(w,1,16) REP(k,0,c*2-1)
			if(f[k][w-1]==-1) f[k][w]=-1;
			else f[k][w]=f[f[k][w-1]][w-1];
		REP(k,0,c-1){
			int p=k,tot=0;
			for(int w=16;w>=0;w--) if(f[p][w]!=-1&&f[p][w]<=k+c) tot+=1<<w,p=f[p][w];
			if(p==k+c) upd(Ceil(n-tot,siz-1),seq[i+k].se);
		}
		i=j;
	}
	cout<<ans<<'\n';
	if(ans_pos==-1){print(0,n-1,0); return 0;}
	int i=ans_pos;
	do{
		int j=(i+1)%n;
		while(p[j]!=p[i]||(j-i-1+n)%n%(siz-1))
			j=(j+1)%n;
		print((i+1)%n,j,0);
		i=j;
	}while(i!=ans_pos);
	return 0;
}

#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = 200500, LOG = 20, inf = 1e9;

int n, k, a[N], up[LOG][N], cost[LOG][N];
int sum, A, pref[N];
pll ans;
map<int, int> lst[N];
int get(int i){
	i %= n;
	if(i <= ans.s)
		i += n;
	return a[i];
}
void gg(int l, int r, ll x, ll y){
	//cerr << l << " " << r << endl;
	a[l] = x, a[r] = y;
	for(int i = l + 1; i < r; i++)
		a[i] = 0;
	printf("%d ", l % n);
	for(int i = l; i <= r; i++)
		printf("%d ", a[i] + A);
	printf("\n");
}
void ff(int l, int r, ll need){
	if(l == r && a[l] == need)
		return;
	if(r - l + 1 <= k){
		a[l] = need;
		for(int i = l + 1; i <= r; i++)
			a[i] = 0;
		printf("%d ", l % n);
		for(int i = l; i <= l + k - 1; i++)
			printf("%d ", get(i) + A);
	   	printf("\n");
		return;
	}
    ll s = 0;
	for(int i = l; i <= l + k - 1; i++)
		s += a[i];
	if(s < need){
		ff(l + k - 1, r, need - s + a[l + k - 1]);
		gg(l, l + k - 1, need, 0);
	}else{
		gg(l, l + k - 1, need, s - need);
		ff(l + k - 1, r, 0);
	}
}
int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		sum += a[i];
	}
	A = sum / n;
	for(int i = 0; i < n; i++)
		a[i + n] = (a[i] -= A);
	for(int i = 1; i < n + n; i++)
		pref[i] = pref[i - 1] + a[i];
	for(int i = 0; i < LOG; i++)
		for(int j = 0; j < N; j++)
			up[i][j] = n + n;
	for(int i = n + n - 1; i >= 0; i--){
		int rem = (i + 1) % (k - 1);
		if(lst[rem].count(pref[i]))
			up[0][i] = lst[rem][pref[i]], cost[0][i] = (lst[rem][pref[i]] - i + k - 3) / (k - 1);
		lst[i % (k - 1)][pref[i]] = i;
	}
	for(int j = 1; j < LOG; j++)
		for(int i = 0; i < n + n; i++)
            up[j][i] = up[j - 1][ up[j - 1][i] ], cost[j][i] = cost[j - 1][i] + cost[j - 1][ up[j - 1][i] ];
	ans = {inf, inf};
	for(int i = 0; i < n; i++){
		int v = i;
		int res = 0;
		for(int j = LOG - 1; j >= 0; j--)
			if(up[j][v] <= n + i)
				res += cost[j][v], v = up[j][v];
		if(v <= n + i)
			res += (n + i - v + k - 2) / (k - 1);
		ans = min(ans, {res, i});
	}
	printf("%d\n", ans.f);
	int v = ans.s;
	while(v < ans.s + n){
		if(up[0][v] < ans.s + n)
			ff(v + 1, up[0][v], 0);
		else
			ff(v + 1, ans.s + n, 0);
		v = up[0][v];
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 200000, W = sqrt(N);
int x[N], y[N], nxt[N], n, k, ano, need;
vector<int> mp, in[N], an, can;
pair<int, int> pp(-1, 0);

vector<int> g[N];
int inc[N];
bool vis[N];

void upan(int w) { if (can.size() < an.size())an = can, ano = w, pp.first = -1; }

void upb(int w, int st){
	can.clear();
	int j = st;
	st += n >> 1;
	if (x[j] == w)j = nxt[j];
	while (j < st){
		can.push_back(j);
		j += k;
		if (x[j] == w)j = nxt[j];
	}
	upan(w);
}

void ups(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st + 1, pan = 0, ed = j + n / 2;
	while (j < ed){
		int t = (in[w][i] - j + k - 1) / k;
		pan += t;
		j += t * k;
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
	}
	if (pan < (int)an.size()){
		pp = make_pair(w, st);
		an.resize(pan);
	}
}

void upsa(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st + 1;
	st = j + n / 2;
	while (i < (int)in[w].size() && j >= in[w][i]){
		if (j == in[w][i])++j;
		++i;
	}
	while (j < st){
		can.push_back(j);
		j += k;
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
	}
	an.resize(can.size() + 1);
	upan(w);
}

void gob(int w){
	f(i, 0, k)upb(w, i);
}

void gos(int w){
	f(i, 0, in[w].size() >> 1)ups(w, i);
}

void go(int w){
	if ((int)in[w].size() >> 1 > W)gob(w);
	else gos(w);
}

void fn(){
	n >>= 1;
	++k;
	for (int u: an)for (int z: g[u])++inc[z];
	stack<int> q;
	for (int u: an)if (inc[u] == 0)q.push(u);
	while (!q.empty()){
		int v = q.top();
		q.pop();
		for (int u: g[v])if (--inc[u] == 0)q.push(u);
		if (v >= n)v -= n;
		printf("%d", v);

		if (!vis[v]){
			int t = v ? v - 1 : n - 1;
			y[v] = need - x[t];
		}

		int t = v + k - 1;
		if (t >= n)t -= n;
		if (!vis[t])y[t] = need + x[t];

		f(i, v, v + k){
			int j = i >= n ? i - n : i;
			if (vis[j] || (i != v && i + 1 != v + k))y[j] = need;
			vis[j] = true;
		}

		f(i, 0, k)printf(" %d", y[i + v >= n ? i + v - n : i + v]);
		printf("\n");
	}
}

int main(){
	scanf("%d%d", &n, &k);
	--k;
	f(i, 0, n)scanf("%d", y + i);
	f(i, 0, n)need += y[i];
	need /= n;
	int car = 0;
	f(i, 0, n){
		x[i] = y[i] + car - need;
		car = x[i];
	}
	mp.resize(n);
	f(i, 0, n)mp[i] = x[i];
	sort(mp.begin(), mp.end());
	mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
	if (mp.size() == 1){
		printf("0\n");
		return 0;
	}
	f(i, 0, n)x[i] = lower_bound(mp.begin(), mp.end(), x[i]) - mp.begin();
	f(i, 0, n)x[i + n] = x[i];
	n <<= 1;
	f(i, 0, n)in[x[i]].push_back(i);
	nxt[n - 1] = n;
	for (int i = n - 2; i >= 0; --i)if (x[i] == x[i + 1])nxt[i] = nxt[i + 1]; else nxt[i] = i + 1;

	an.resize(n);
	f(i, 0, mp.size())go(i);
	if (pp.first != -1)upsa(pp.first, pp.second);

	ano = mp[ano];
	f(i, 0, n)x[i] = mp[x[i]] - ano;
	f(i, 1, an.size())if (an[i] == an[i - 1] + k){
		if (x[an[i] - 1] > 0 && x[an[i]] > 0)g[an[i - 1]].push_back(an[i]);
		else if (x[an[i]] < 0 && x[an[i] - 1] < 0)g[an[i]].push_back(an[i - 1]);
	}
	int t = an[0] + n / 2;
	if (t == an.back() + k){
		if (x[t - 1] > 0 && x[t] > 0)g[an.back()].push_back(an[0]);
		else if (x[t] < 0 && x[t - 1] < 0)g[an[0]].push_back(an.back());
	}

	printf("%d\n", (int)an.size());
	fn();
}

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute(int l, int len, int need){
    //cout << "dis " << l << " " << len << " " << need << endl;
    //for(int i = 0; i < len; i += 1) cout << a[(l + i) % n] << " ";
    //cout << endl;
    if(len <= 1) return;
    if(a[l] == avg and need == 0){
        distribute((l + 1) % n, len - 1, 0);
        return;
    }
    int sum = 0, m = min(len, k);
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    if(sum >= avg * (m - 1) + need){
        sum -= a[l] = avg + need;
        for(int i = 1; i < m - 1; i += 1) sum -= a[(l + i) % n] = avg;
        a[(l + m - 1) % n] = sum;
        cout << l;
        for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
        cout << "\n";
        distribute((l + m - 1) % n, len - m + 1, 0);
        return;
    }
    distribute((l + m - 1) % n, len - m + 1, avg * (m - 1) + need - sum + a[(l + m - 1) % n]);
    sum = 0;
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    for(int i = 1; i < m; i += 1) sum -= a[(l + i) % n] = avg;
    a[l] = sum;
    cout << l;
    for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
    cout << "\n";
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        map<int, int> pos;
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; ~i; i -= 1){
            if(pos.count((v[i] + 1) % (k - 1)))
                next[i][0] = pos[(v[i] + 1) % (k - 1)];
            else next[i][0] = 2 * c;
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        //cout << "cut" << " " << cut[i] << endl;
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        if(r <= l) r += n;
        interval.push_back({l, r - l});
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute(p.first, p.second, 0);
    for(int i = 0; i < n; i += 1) if(a[i] != avg) cout << i << " " << a[i] << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute(int l, int len, int need){
    if(len <= 1) return;
    if(a[l] == avg and need == 0){
        distribute((l + 1) % n, len - 1, 0);
        return;
    }
    int sum = 0, m = min(len, k);
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    if(sum >= avg * (m - 1) + need){
        sum -= a[l] = avg + need;
        for(int i = 1; i < m - 1; i += 1) sum -= a[(l + i) % n] = avg;
        a[(l + m - 1) % n] = sum;
        cout << l;
        for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
        cout << "\n";
        distribute((l + m - 1) % n, len - m + 1, 0);
        return;
    }
    distribute((l + m - 1) % n, len - m + 1, avg * (m - 1) + need - sum + a[(l + m - 1) % n]);
    sum = 0;
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    for(int i = 1; i < m; i += 1) sum -= a[(l + i) % n] = avg;
    a[l] = sum;
    cout << l;
    for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
    cout << "\n";
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        map<int, int> pos;
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; ~i; i -= 1){
            if(pos.count((v[i] + 1) % (k - 1)))
                next[i][0] = pos[(v[i] + 1) % (k - 1)];
            else next[i][0] = 2 * c;
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        if(r <= l) r += n;
        interval.push_back({l, r - l});
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute(p.first, p.second, 0);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())

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];
ll b[N];
pii go[N][20];
map<int,int> lst[N];
pii ans=mp(1e9,0);
vector<pair<int,vl>> Ans,tmp;

int cal(int len) {
	if(len<=1) return 0;
	return (len-1+k-2)/(k-1);
}

void apply(int s,vl v) {
	for(int i=s,j=0;i<s+k;i++,j++) b[i]=v[j];
}

void solve(int l,int r,ll t) {
	//cerr<<l<<' '<<r<<' '<<t<<endl;
	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;
		vl vec;
		for(int i=L;i<l;i++) vec.pb(b[i]);
		vec.pb(t);
		for(int i=l+1;i<=r;i++) vec.pb(0);
		for(int i=r+1;i<=R;i++) vec.pb(b[i]);
		Ans.pb(mp(L,vec));apply(L,vec);
	}
	else {
		ll sum=0;
		for(int i=l;i<l+k;i++) sum+=b[i];
		//cerr<<l<<' '<<r<<' '<<sum<<endl;
		if(sum>=t) {
			vl vec;vec.pb(t);
			for(int i=l+1;i<l+k-1;i++) vec.pb(0);
			vec.pb(sum-t);
			Ans.pb(mp(l,vec));apply(l,vec);
			solve(l+k-1,r,0);
		}
		else {
			//cerr<<l<<' '<<r<<endl;
			solve(l+k-1,r,t-sum+b[l+k-1]);
			vl vec;vec.pb(t);
			for(int i=l+1;i<=l+k-1;i++) vec.pb(0);
			Ans.pb(mp(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]=mp(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]=mp(x,cal(x-i));
		}
		else go[i][0]=mp(n+n,cal(n+n-i));
		//cerr<<i<<' '<<go[i][0].fi<<' '<<go[i][0].se<<endl;
		for(int j=1;j<20;j++) go[i][j].fi=go[go[i][j-1].fi][j-1].fi,go[i][j].se=go[i][j-1].se+go[go[i][j-1].fi][j-1].se;
		if(i<n) {
			int x=i,w=0;
			for(int j=19;~j;j--)
				if(go[x][j].fi<i+n) w+=go[x][j].se,x=go[x][j].fi;
			//cerr<<i<<' '<<w<<' '<<x<<endl;
			w+=cal(n+i-x);
			ans=min(ans,mp(min(w,cal(n)),i));
		}
		lst[i%(k-1)][sum[i]]=i;
	}
	cout<<ans.fi<<'\n';//cerr<<ans.se<<endl;
	for(int i=0;i<n+n;i++) b[i]=a[i];//,cerr<<b[i]<<' ';
	solve(ans.se,ans.se+n-1,0);tmp=Ans;Ans.clear();//exit(0);
	//for(auto x:Ans) cerr<<x.fi<<endl;
	//cerr<<sz(Ans)<<endl;
	//exit(0);
	int x=ans.se;for(int i=0;i<n+n;i++) b[i]=a[i];
	while(go[x][0].fi<ans.se+n) solve(x,go[x][0].fi-1,0),x=go[x][0].fi;
	solve(x,ans.se+n-1,0);//cerr<<x<<' '<<ans.se+n-1<<endl;
	if(sz(tmp)<sz(Ans)) swap(tmp,Ans);
	for(auto x:Ans) {
		cout<<x.fi%n<<' ';
		for(auto y:x.se) cout<<y+avg<<' ';
		cout<<'\n';
	}
	return 0;
}

    #include <bits/stdc++.h>
    using namespace std;

    #define N 200200

    int n, K, a[N], b[N];

    map <int, int> mp;

    int tot, s;

    map <int, int> lst[N];

    int nxt[N][20];

    void solve(int l, int r, int rem = 0) {
        if (l + K > r) {
            int sum = 0;
            for (int i = l; i <= r; i ++) sum += b[i%n] - s, b[i%n] = s;
            b[l%n] += rem, b[r%n] += sum - rem;
            printf("%d", (l - 1) % n);
            for (int i = l; i < l + K; i ++) printf(" %d", b[i%n]);
            puts(""); return;
        }
        int sum = 0;
        for (int i = l; i < l + K; i ++) sum += b[i%n] - s;
        if (sum >= rem) {
            solve(l, l + K - 1, rem);
            solve(l + K - 1, r, 0);
        }
        else {
            sum -= b[(l+K-1)%n] - s;
            solve(l + K - 1, r, rem - sum);
            solve(l, l + K - 1, rem);
        }
    }

    typedef pair <int, int> pii;

    int main() {
        scanf("%d %d", &n, &K);
        for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[n+i] = a[i], s += a[i];
        for (int i = 1; i <= n; i ++) b[i%n] = a[i];
        s /= n;
        for (int i = 1; i <= 2 * n; i ++) a[i] = a[i] - s + a[i-1];
        for (int i = 2 * n; i; i --) {
            int &k = mp[a[i]];
            if (!k) k = ++tot;
            nxt[i][0] = lst[k][(i+1)%(K-1)];
            for (int j = 0; nxt[i][j]; j ++) nxt[i][j+1] = nxt[nxt[i][j]][j];
            lst[k][i%(K-1)] = i;
        }
        pii ans = pii(1e9, 0); int r = n % (K - 1);
        for (int i = 1; i <= n; i ++) {
            int k = 0, u = i;
            for (int j = 19; ~j; j --) {
                int v = nxt[u][j];
                if (v && v <= n + i) u = v, k ^= 1 << j;
            }
            int qr = k % (K - 1);
            if (qr >= r) k -= qr - r;
            else k -= qr - r + K - 1;
            ans = min(ans, pii((n - k) / (K - 1), i));
        }
        if (ans.first >= (n - 3 + K) / (K - 1)) printf("%d\n", (n - 3 + K) / (K - 1)), solve(1, n);
        else {
            printf("%d\n", ans.first);
            int u = ans.second, k = n - (K - 1) * ans.first;
            while (-- k) {
                if (u + 1 < nxt[u][0]) solve(u + 1, nxt[u][0]);
                u = nxt[u][0]; assert(u > 0);
            }
            if (u + 1 < n + ans.second) solve(u + 1, n + ans.second);
        }

        return 0;
    }

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos=0,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;j<logn;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

int ans=0,ts[100000],te[100000];
int tl[100000],tr[100000];
int tt[200000];
inline void addans(int s,int e,int l,int r)
{
	ts[ans]=s; te[ans]=e;
	tl[ans]=l; tr[ans++]=r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],x=sum[l]-num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,i,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,i,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,sta[top],L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	int T=sum[r]-sum[i-k+1]+num[i-k+1];
	if(i-k+1!=r)
		addans(i-k+1,r,T-ave*(r-i+k-1),ave);
	while(top>=0){
		int g=sta[top]-k+1;
		int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
		addans(g,sta[top],L,ave);
		sum[g]+=L-num[g];
		top--;
	}
	return;
}

inline void upd(int a,int b)
{
	tt[mod(a,n>>1)]=tt[mod(a,n>>1)+(n>>1)]=b;
	return;
}
void print(void)
{
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",mod(ts[i],n>>1),tl[i]);
		upd(ts[i],tl[i]);
		for(int j=ts[i]+1;j<te[i];j++){
			printf("%d ",ave);
			upd(j,ave);
		}
		printf("%d",tr[i]);
		upd(te[i],tr[i]);
		for(int j=te[i]+1;j<ts[i]+k;j++)
			printf(" %d",tt[mod(j,n>>1)]);
		putchar('\n');
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	print();
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=tt[i]=tt[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	if(ave==0){
		printf("0\n");
		return 0;
	}
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
7 5
0 2 0 1 2 0 16
*/

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())

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];
ll b[N];
pii go[N][20];
map<int,int> lst[N];
pii ans=mp(1e9,0);
vector<pair<int,vl>> Ans,tmp;

int cal(int len) {
	if(len<=1) return 0;
	return (len-1+k-2)/(k-1);
}

void apply(int s,vl v) {
	for(int i=s,j=0;i<s+k;i++,j++) b[i]=v[j];
}

void solve(int l,int r,ll 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;
		vl vec;
		for(int i=L;i<l;i++) vec.pb(b[i]);
		vec.pb(t);
		for(int i=l+1;i<=r;i++) vec.pb(0);
		for(int i=r+1;i<=R;i++) vec.pb(b[i]);
		Ans.pb(mp(L,vec));apply(L,vec);
	}
	else {
		ll sum=0;
		for(int i=l;i<l+k;i++) sum+=b[i];
		if(sum>=t) {
			vl vec;vec.pb(t);
			for(int i=l+1;i<l+k-1;i++) vec.pb(0);
			vec.pb(sum-t);
			Ans.pb(mp(l,vec));apply(l,vec);
			solve(l+k-1,r,0);
		}
		else {
			solve(l+k-1,r,t-sum+b[l+k-1]);
			vl vec;vec.pb(t);
			for(int i=l+1;i<=l+k-1;i++) vec.pb(0);
			Ans.pb(mp(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]=mp(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]=mp(x,cal(x-i));
		}
		else go[i][0]=mp(n+n,cal(n+n-i));
		for(int j=1;j<20;j++) go[i][j].fi=go[go[i][j-1].fi][j-1].fi,go[i][j].se=go[i][j-1].se+go[go[i][j-1].fi][j-1].se;
		if(i<n) {
			int x=i,w=0;
			for(int j=19;~j;j--)
				if(go[x][j].fi<i+n) w+=go[x][j].se,x=go[x][j].fi;
			w+=cal(n+i-x);
			ans=min(ans,mp(min(w,cal(n)),i));
		}
		lst[i%(k-1)][sum[i]]=i;
	}
	cout<<ans.fi<<'\n';
	for(int i=0;i<n+n;i++) b[i]=a[i];
	solve(ans.se,ans.se+n-1,0);tmp=Ans;Ans.clear();
	int x=ans.se;for(int i=0;i<n+n;i++) b[i]=a[i];
	while(go[x][0].fi<ans.se+n) solve(x,go[x][0].fi-1,0),x=go[x][0].fi;
	solve(x,ans.se+n-1,0);
	if(sz(tmp)<sz(Ans)) swap(tmp,Ans);
	for(auto x:Ans) {
		cout<<x.fi%n<<' ';
		for(auto y:x.se) cout<<y+avg<<' ';
		cout<<'\n';
	}
	return 0;
}

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=100000;
struct Op { int idx; vector<int> vals; };

int n,sz;
int a[MAXN];
vector<Op> ans;

int asum[MAXN+1];
int each;
int bal[MAXN];

pair<int,int> o[MAXN];

int x[2*MAXN],nx;
int y[2*MAXN];
int last[MAXN];
int nxt[2*MAXN];
vector<int> jump[2*MAXN];

int bestval,bestx,bestidx;

int cur[MAXN];

void calc() {
	int den=sz-1;
	REP(i,nx) x[nx+i]=x[i]+n;
	REP(i,2*nx) y[i]=x[i]%den;
	for(int i=2*nx-1;i>=0;--i) { nxt[i]=last[(y[i]+1)%den]; last[y[i]]=i; }
	for(int i=2*nx-1;i>=0;--i) { last[y[i]]=-1; }
	for(int i=2*nx-1;i>=0;--i) { jump[i].clear(); if(nxt[i]==-1) continue; jump[i].PB(nxt[i]); for(int k=1;;++k) { int mid=jump[i][k-1]; if(k-1>=SZ(jump[mid])) break; jump[i].PB(jump[mid][k-1]); } }

	//printf("%d\n",nx);
	//printf("x:"); REP(i,2*nx) printf(" %d",x[i]); puts("");
	//printf("y:"); REP(i,2*nx) printf(" %d",y[i]); puts("");
	//printf("nxt:"); REP(i,2*nx) printf(" %d",nxt[i]); puts("");
	//printf("jump:"); REP(i,2*nx) { printf(" ["); REPSZ(k,jump[i]) { if(k!=0) printf(","); printf("%d",jump[i][k]); } printf("]"); } puts("");

	REP(i,nx) {
		int j=i,steps=0; for(int k=SZ(jump[i])-1;k>=0;--k) if(k<SZ(jump[j])&&jump[j][k]<i+nx) j=jump[j][k],steps+=1<<k;
		int cur=(n-(steps+1)+den-1)/den;
		//printf("%d..%d -> %d\n",i,j,cur);
		if(cur<bestval) bestval=cur,bestx=x[i],bestidx=i;
	}
}

void fixrange(int l,int r,int extra) {
	//printf("fixrange(%d..%d,%d)\n",l,r,extra);
	int totcnt=r-l+1,fstcnt=min(totcnt,sz),fstoff=l;
	int fstsum=0; REP(i,fstcnt) fstsum+=cur[(fstoff+i)%n];
	//printf("fstcnt=%d fstsum=%d each=%d\n",fstcnt,fstsum,each);
	if(fstcnt==1) {
		assert(fstsum==each+extra);
	} else if(fstsum>=(fstcnt-1)*each+extra) {
		int rem=fstsum-(fstcnt-1)*each-extra; if(rem!=each) assert(fstcnt<totcnt);
		Op op; op.idx=fstoff%n; op.vals=vector<int>(sz,-1); REP(i,sz) op.vals[i]=i==0?each+extra:i<fstcnt-1?each:i==fstcnt-1?rem:cur[(fstoff+i)%n]; ans.PB(op);
		int before=0,after=0; REP(i,sz) { int idx=(fstoff+i)%n; before+=cur[idx],after+=op.vals[i]; cur[idx]=op.vals[i]; } assert(before==after);
		if(fstcnt<totcnt) fixrange(l+fstcnt-1,r,0);
	} else {
		assert(fstcnt<totcnt);
		fixrange(l+fstcnt-1,r,(fstcnt-1)*each+extra-fstsum+cur[(fstoff+fstcnt-1)%n]);
		fixrange(l,l+fstcnt-1,extra);
	}
}

void solve() {
	assert(sz>=2);
	asum[0]=0; REP(i,n) asum[i+1]=asum[i]+a[i];
	//printf("asum:"); REPE(i,n) printf(" %d",asum[i]); puts("");
	assert(asum[n]%n==0); each=asum[n]/n;
	REP(i,n) bal[i]=asum[i]-i*each;
	//printf("bal:"); REP(i,n) printf(" %d",bal[i]); puts("");

	REP(i,n) o[i]=MP(bal[i],i);
	sort(o,o+n);
	bestval=INT_MAX,bestx=-1,bestidx=-1;
	memset(last,-1,sizeof(last));
	for(int l=0,r=l;l<n;l=r) {
		while(r<n&&o[r].first==o[l].first) ++r;
		nx=0; FOR(i,l,r) x[nx++]=o[i].second;
		calc();
	}
	//printf("bestval=%d bestx=%d bestidx=%d\n",bestval,bestx,bestidx);
	nx=0; REP(i,n) if(bal[i]==bal[bestx]) x[nx++]=i;
	calc();

	ans.clear();
	REP(i,n) cur[i]=a[i];
	int at=bestidx;
	while(at<bestidx+nx) {
		if(nxt[at]==-1||nxt[at]>=bestidx+nx) {
			fixrange(x[at],x[bestidx+nx]-1,0);
			break;
		}
		fixrange(x[at],x[nxt[at]]-1,0);
		at=nxt[at];
	}
	//printf("nop=%d\n",SZ(ans));
	//printf("cur:"); REP(i,n) printf(" %d",cur[i]); puts("");
	assert(SZ(ans)==bestval);
	REP(i,n) assert(cur[i]==each);
}

void run() {
	scanf("%d%d",&n,&sz);
	REP(i,n) scanf("%d",&a[i]);
	solve();
	printf("%d\n",SZ(ans));
	REPSZ(i,ans) { printf("%d",ans[i].idx); REPSZ(j,ans[i].vals) printf(" %d",ans[i].vals[j]); puts(""); }
}

void stress() {
	int mxn=10,mxeach=10;
	REP(rep,1000000) {
		n=rand()%(mxn-2+1)+2; sz=rand()%(n-2+1)+2; int each=rand()%(mxeach+1);
		REP(i,n) a[i]=0; REP(i,n*each) { int idx=rand()%n; ++a[idx]; }
		//if(rep!=45853) continue;
		//printf("%d %d\n",n,sz);	REP(i,n) { if(i!=0) printf(" "); printf("%d",a[i]); } puts("");
		solve();
		printf(".");
	}
}

void stresstime() {
	REP(rep,3) {
		n=MAXN; sz=MAXN-1; REP(i,n) a[i]=rand()%1001; int sum=0; REP(i,n) sum+=a[i]; int need=(n-sum%n)%n; REP(i,need) { int idx=rand()%n; a[idx]++; }
		solve();
		printf(".");
	}
}

int main() {
	run();
	//stress();
	//stresstime();
	return 0;
}

    #include <algorithm>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <cmath>
    #include <cstring>
    #include <list>
    #include <cassert>
    #include <climits>
    #include <bitset>
    using namespace std;

    #define PB push_back
    #define MP make_pair
    #define SZ(v) ((int)(v).size())
    #define FOR(i,a,b) for(int i=(a);i<(b);++i)
    #define REP(i,n) FOR(i,0,n)
    #define FORE(i,a,b) for(int i=(a);i<=(b);++i)
    #define REPE(i,n) FORE(i,0,n)
    #define FORSZ(i,a,v) FOR(i,a,SZ(v))
    #define REPSZ(i,v) REP(i,SZ(v))
    typedef long long ll;
    ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

    const int MAXN=100000;
    struct Op { int idx; vector<int> vals; };

    int n,sz;
    int a[MAXN];
    vector<Op> ans;

    int asum[MAXN+1];
    int each;
    int bal[MAXN];

    pair<int,int> o[MAXN];

    int x[2*MAXN],nx;
    int y[2*MAXN];
    int last[MAXN];
    int nxt[2*MAXN];
    vector<int> jump[2*MAXN];

    int bestval,bestx,bestidx;

    int cur[MAXN];

    void calc() {
    	int den=sz-1;
    	REP(i,nx) x[nx+i]=x[i]+n;
    	REP(i,2*nx) y[i]=x[i]%den;
    	for(int i=2*nx-1;i>=0;--i) { nxt[i]=last[(y[i]+1)%den]; last[y[i]]=i; }
    	for(int i=2*nx-1;i>=0;--i) { last[y[i]]=-1; }
    	for(int i=2*nx-1;i>=0;--i) { jump[i].clear(); if(nxt[i]==-1) continue; jump[i].PB(nxt[i]); for(int k=1;;++k) { int mid=jump[i][k-1]; if(k-1>=SZ(jump[mid])) break; jump[i].PB(jump[mid][k-1]); } }

    	REP(i,nx) {
    		int j=i,steps=0; for(int k=SZ(jump[i])-1;k>=0;--k) if(k<SZ(jump[j])&&jump[j][k]<i+nx) j=jump[j][k],steps+=1<<k;
    		int cur=(n-(steps+1)+den-1)/den;

    		if(cur<bestval) bestval=cur,bestx=x[i],bestidx=i;
    	}
    }

    void fixrange(int l,int r,int extra) {

    	int totcnt=r-l+1,fstcnt=min(totcnt,sz),fstoff=l;
    	int fstsum=0; REP(i,fstcnt) fstsum+=cur[(fstoff+i)%n];

    	if(fstcnt==1) {
    		assert(fstsum==each+extra);
    	} else if(fstsum>=(fstcnt-1)*each+extra) {
    		int rem=fstsum-(fstcnt-1)*each-extra; if(rem!=each) assert(fstcnt<totcnt);
    		Op op; op.idx=fstoff%n; op.vals=vector<int>(sz,-1); REP(i,sz) op.vals[i]=i==0?each+extra:i<fstcnt-1?each:i==fstcnt-1?rem:cur[(fstoff+i)%n]; ans.PB(op);
    		int before=0,after=0; REP(i,sz) { int idx=(fstoff+i)%n; before+=cur[idx],after+=op.vals[i]; cur[idx]=op.vals[i]; } assert(before==after);
    		if(fstcnt<totcnt) fixrange(l+fstcnt-1,r,0);
    	} else {
    		assert(fstcnt<totcnt);
    		fixrange(l+fstcnt-1,r,(fstcnt-1)*each+extra-fstsum+cur[(fstoff+fstcnt-1)%n]);
    		fixrange(l,l+fstcnt-1,extra);
    	}
    }

    void solve() {
    	assert(sz>=2);
    	asum[0]=0; REP(i,n) asum[i+1]=asum[i]+a[i];
        assert(asum[n]%n==0); each=asum[n]/n;
    	REP(i,n) bal[i]=asum[i]-i*each;

    	REP(i,n) o[i]=MP(bal[i],i);
    	sort(o,o+n);
    	bestval=INT_MAX,bestx=-1,bestidx=-1;
    	memset(last,-1,sizeof(last));
    	for(int l=0,r=l;l<n;l=r) {
    		while(r<n&&o[r].first==o[l].first) ++r;
    		nx=0; FOR(i,l,r) x[nx++]=o[i].second;
    		calc();
    	}

    	nx=0; REP(i,n) if(bal[i]==bal[bestx]) x[nx++]=i;
    	calc();

    	ans.clear();
    	REP(i,n) cur[i]=a[i];
    	int at=bestidx;
    	while(at<bestidx+nx) {
    		if(nxt[at]==-1||nxt[at]>=bestidx+nx) {
    			fixrange(x[at],x[bestidx+nx]-1,0);
    			break;
    		}
    		fixrange(x[at],x[nxt[at]]-1,0);
    		at=nxt[at];
    	}

    	assert(SZ(ans)==bestval);
    	REP(i,n) assert(cur[i]==each);
    }

    void run() {
    	scanf("%d%d",&n,&sz);
    	REP(i,n) scanf("%d",&a[i]);
    	solve();
    	printf("%d\n",SZ(ans));
    	REPSZ(i,ans) { printf("%d",ans[i].idx); REPSZ(j,ans[i].vals) printf(" %d",ans[i].vals[j]); puts(""); }
    }

    void stress() {
    	int mxn=10,mxeach=10;
    	REP(rep,1000000) {
    		n=rand()%(mxn-2+1)+2; sz=rand()%(n-2+1)+2; int each=rand()%(mxeach+1);
    		REP(i,n) a[i]=0; REP(i,n*each) { int idx=rand()%n; ++a[idx];
    		}
    		solve();
    		printf(".");
    	}
    }

    void stresstime() {
    	REP(rep,3) {
    		n=MAXN; sz=MAXN-1; REP(i,n) a[i]=rand()%1001; int sum=0; REP(i,n) sum+=a[i]; int need=(n-sum%n)%n; REP(i,need) { int idx=rand()%n; a[idx]++; }
    		solve();
    		printf(".");
    	}
    }

    int main() {
    	run();
    	return 0;
    }

#include<bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 1.1e5;
int N, K;
int A[MAXN * 4];

int pref[MAXN * 4];

pair<ll, ll> dist[MAXN][20];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> N >> K;
	int tot = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		tot += A[i];
	}
	assert(tot % N == 0);
	int avg = tot / N;
	for (int i = 0; i < N; i++) {
		A[i] -= avg;
	}

	for (int i = 0; i < N; i++) {
		A[i+3*N] = A[i+2*N] = A[i+N] = A[i];
	}

	pref[0] = 0;
	for (int i = 0; i < 4 * N; i++) {
		pref[i+1] = pref[i] + A[i];
	}

	{
		vector<map<int, int>> prefMatches(N);

		for (int i = 2 * N; i >= 0; i--) {
			if (i < N) {
				if (prefMatches[(i+1)%(K-1)].count(pref[i]) && prefMatches[(i+1) % (K-1)][pref[i]] <= i+N) {
					int l = prefMatches[(i+1) % (K-1)][pref[i]] - i;
					dist[i][0] = {l, (l-1) / (K-1)};
				} else {
					dist[i][0] = {N, (N-1 + (K-2)) / (K-1)};
				}
			}
			prefMatches[i % (K-1)][pref[i]] = i;
		}
	}

	for (int z = 0; z < 19; z++) {
		for (int i = 0; i < N; i++) {
			int j = int((i + dist[i][z].first) % N);
			dist[i][z+1] = {
				dist[i][z].first + dist[j][z].first,
				dist[i][z].second + dist[j][z].second,
			};
		}
	}
	int bestStart = -1;
	int ans = N+1;
	for (int i = 0; i < N; i++) {
		int cur = i;
		int cost = 0;
		for (int z = 19; z >= 0; z--) {
			if (cur + dist[cur % N][z].first < i + N) {
				cost += int(dist[cur % N][z].second);
				cur += int(dist[cur % N][z].first);
			}
		}
		assert(cur < i + N);
		cost += (i + N - cur - 1 + (K-2)) / (K-1);
		if (cost < ans) {
			ans = cost;
			bestStart = i;
		}
	}

	cerr << bestStart << '\n';
	cout << ans << '\n';

	for (int st = bestStart; st < bestStart + N; ) {
		int en = st + int(dist[st%N][0].first);
		en = min(en, bestStart + N);

		assert(pref[en] == pref[st]);
		while ((en - st - 1) % (K-1) != 0) {
			assert(en >= bestStart + N);
			pref[en+1] = pref[en];
			en++;
		}
		assert(pref[en] == pref[st]);
		cerr << st << ' ' << en << '\n';

		unordered_set<int> moved;
		auto canMove = [&](int a) {
			if (moved.count(a)) return false;
			int b = a + K;
			assert(st <= a && b <= en);
			assert((a - st) % (K-1) == 0);
			return (pref[a] - pref[st] <= 0 || pref[a+1] - pref[st] <= 0) && (pref[b] - pref[st] >= 0 || pref[b-1] - pref[st] >= 0);
		};
		unordered_set<int> moves;
		for (int a = st; a+K <= en; a += K-1) {
			if (canMove(a)) {
				moves.insert(a);
			}
		}
		while (!moves.empty()) {
			auto it = moves.begin();
			int a = *it;
			moves.erase(it);
			moved.insert(a);
			int b = a+K;
			for (int i = 1; i <= K-1; i++) {
				pref[a+i] = pref[st];
			}

			cout << (a%N);
			for (int i = 0; i < K; i++) {
				if (a+i < bestStart + N) {
					A[(a+i)%N] = pref[a+i+1] - pref[a+i];
				}
				cout << " " << A[(a+i)%N]+avg;
			}
			cout << '\n';

			if (a > st) {
				if (canMove(a - (K-1))) {
					moves.insert(a - (K-1));
				} else {
					moves.erase(a - (K-1));
				}
			}
			if (b < en) {
				if (canMove(a + (K-1))) {
					moves.insert(a + (K-1));
				} else {
					moves.erase(a + (K-1));
				}
			}
		}

		st = en;
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pc putchar
#define gc getchar
#define pb push_back
#define ppb pop_back
#define x first
#define y second
#define bitcnt __builtin_popcount
#define bitctz __builtin_ctz
#define bitclz __builtin_clz
#define f0(i, a, b) for (int i = a; i < b; i ++)
#define f1(i, a, b) for (int i = a; i <= b; i ++)
#define f2(i, a, b) for (int i = a; i > b; i --)
#define f3(i, a, b) for (int i = a; i >= b; i --)
#pragma comment(linker, "/STACK:102400000,102400000")
template <class T> void chkmax(T &a, T b) {if (a < b) a = b;}
template <class T> void chkmin(T &a, T b) {if (a > b) a = b;}

typedef double ld;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef pair <int, ll> pil;
typedef pair <ll, int> pli;
typedef pair <ll, ll> pll;
typedef pair <double, int> pdi;
typedef vector <int> vi;

const int inf = 0x3f3f3f3f;
const ll linf = 2e18 + 100;
const ld dinf = 2e18 + 100;
const ld eps = 1e-7;
const ld pi = 3.14159265358979323846264338327950288L;

bool scanld(ld &x) {double xx; int f = scanf("%lf", &xx); x = xx; return f > 0;}
void printld(const ld &x) {printf("%.10lf", fabs(x) < eps ? 0 : (double)x);}

inline char nc() {
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
template <class T> void read(T &x){
	char c=nc(),b=1;
	for (;!((c>='0'&&c<='9')||c==EOF);c=nc()) if (c=='-') b=-b;
	for (x=0;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

inline void read(char *s){
	char c=nc(); int len=0;
	for (;c==' '||c=='\t'||c=='\n';c=nc());
	for (;!(c==' '||c=='\t'||c=='\n'||c==EOF);s[len++]=c,c=nc()); s[len]=0;
}

#define N 200005

int n, k, con;
int a[N], p[N];

map <pii, int> h;

pii nxt[N][18];

int st;

void update(int p, int v, vi &vec) {
    if (v < 0) vec.pb(a[p]);
    else vec.pb(v), a[p] = v;
    return;
}

void print(int p, vi &vec) {
    printf("%d", (p+st+1)%n);
    for (auto x : vec) printf(" %d", x);
    puts("");
    return;
}

void solve(int L, int R, int nd) {
    if (R == L) {
        assert(a[L] == nd+con);
		return;
    }
    assert(0<=L&&L<n);
    assert(0<=R&&R<n);
	vi vec;
    if (R-L+1 <= k) {
        int sum = accumulate(a+L, a+R+1, 0);
        assert(sum == nd + (R-L+1)*con);
        update(L, con+nd, vec);
        f1(i, L+1, R) update(i, con, vec);
        f0(i, 0, k-(R-L+1)) update((R+i+1)%n, -1, vec);
        print(L, vec);
        return;
    }
    int sum = accumulate(a+L, a+L+k, 0);
    if (sum >= nd + (k-1)*con) {
        f0(i, L, L+k) {
            if (i==L) update(i, con+nd, vec);
            else if (i < L+k-1) update(i, con, vec);
            else update(i, sum-nd-(k-1)*con, vec);
        }
        print(L, vec);
        solve(L+k-1, R, 0);
        return;
    }
    else {
        int md = nd+k*con-accumulate(a+L, a+L+k-1, 0)-con;
        solve(L+k-1, R, md);
        assert(a[L+k-1]==con+md);
        f0(i, L, L+k) {
            if (i==L) update(i, con+nd, vec);
            else update(i, con, vec);
        }
        print(L, vec);
        return;
    }
}

int get_cnt(int L, int R) {
    return (R-L+k-2)/(k-1);
}

int main() {
    int T = 0;
	while (1) {
		read(n), read(k);
		if (!n) break;
        int sum = 0; h.clear();
		f0(i, 0, n) read(a[i]), sum += a[i];
		con = sum / n;
		f0(i, 0, n) a[i] -= con, a[i+n] = a[i];
		int nn = n << 1;
		partial_sum(a, a+nn, p);
		f0(i, 0, 18) nxt[nn][0] = {nn, inf};
		f3(i, nn-1, 0) {
			int id = i % (k-1);
			int nid = (id < k-2 ? id+1 : 0);
			pii cur = pii(p[i], nid);
			if (!h.count(cur)) h[cur] = nn;
			int pt = h[cur];
			nxt[i][0] = {pt, get_cnt(i, pt-1)};
			h[pii(p[i], id)] = i;
		}

		f0(j, 0, 18) nxt[nn][j] = pii(nn, inf);

		f0(j, 1, 18) f0(i, 0, nn) {
			int pt = nxt[i][j-1].x, ct = nxt[i][j-1].y;
			nxt[i][j] = pii(nxt[pt][j-1].x, min(inf, ct+nxt[pt][j-1].y));
		}

		pii ans = pii(get_cnt(0, n-1), -1);
		f0(i, 0, n) {
			int res = 0, cur = i;
			bool flag = (i == 6);
			for (int j = 17; j >= 0; j --) if (nxt[cur][j].x < n+i) {
				res += nxt[cur][j].y;
				cur = nxt[cur][j].x;
			}
			if (nxt[cur][0].x == n+i) {
				res += nxt[cur][0].y;
				if (res < ans.x) {
					ans = pii(res, i);
				}
			}
		}
		f0(i, 0, nn) a[i] += con;
		printf("%d\n", ans.x);
		st = n-1;
		if (ans.y < 0) solve(0, n-1, 0);
		else {
			int pos = ans.y;
			st = pos + 1 > n ? 1 : pos + 1;
			rotate(a, a+st, a+n);
			st = pos;
			int sum = 0, L, R;
			for (L = 0, R = 0; L < n; R ++) {
				sum += a[R] - con;
				if (sum == 0 && (R-L)%(k-1)==0) {
					solve(L, R, 0);
					L = R + 1;
				}
			}
			assert(R==n);
		}
	}

	return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <unordered_set>
#include <algorithm>

using namespace std;
typedef long long ll;

int n, k;
vector<int> a, asum;

vector<vector<pair<ll,ll>>> dp(100010,vector<pair<ll,ll>>(25));

void init() {
    vector<map<int,int>> asumeq(n);
    for (int i=2*n; i>=0; --i) {
        if (i<n) {
            if (asumeq[(i+1)%(k-1)].count(asum[i]) && asumeq[(i+1)%(k-1)][asum[i]] <= n+i) {
                int x = asumeq[(i+1)%(k-1)][asum[i]] - i;
                dp[i][0] = make_pair(x, (x-1)/(k-1));
            }
            else dp[i][0] = make_pair(n, (n+k-3)/(k-1));
            asumeq[i%(k-1)][asum[i]] = i;
        }
    }
    for (int z = 0; z < 24; ++z) {
		for (int i = 0; i < n; ++i) {
			int j = int((i + dp[i][z].first) % n);
			dp[i][z+1] = make_pair(dp[i][z].first + dp[j][z].first, dp[i][z].second + dp[j][z].second);
		}
	}
}

unordered_set<int> moved;
unordered_set<int> moves;

bool canMove(int a, int s) {
	if (moved.count(a)) return false;
	int b = a + k;
    return (asum[a] - asum[s] <= 0 || asum[a+1] - asum[s] <= 0) && (asum[b] - asum[s] >= 0 || asum[b-1] - asum[s] >= 0);
};

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(4*n);
    asum.resize(4*n+1);
    int avg = 0;
    for (int i=0; i<n; ++i) {
        cin >> a[i];
        avg += a[i];
    }
    avg /= n;
    bool eq = true;
    for (int i=0; i<n; ++i) {
        a[i] -= avg;
        if (a[i]!=0) eq = false;
    }
    if (eq) {
        cout << 0 << endl;
        return 0;
    }
    for (int i=0; i<n; ++i) a[i+3*n] = a[i+2*n] = a[i+n] = a[i];
    asum[0] = 0;
    for (int i=0; i<4*n; ++i) asum[i+1] = asum[i]+a[i];

    init();
    int start = -1, ans = n+1;
    for (int i=0; i<n; ++i) {
        int curr = i, cost = 0;
        for (int j = 24; j >= 0; --j) {
			if (curr + dp[curr % n][j].first < i + n) {
				cost += int(dp[curr % n][j].second);
				curr += int(dp[curr % n][j].first);
			}
		}
		cost += (i+n - curr - 1 + (k-2)) / (k-1);
		if (cost < ans) {
			ans = cost;
			start = i;
		}
    }
    cout << ans << endl;

    for (int s=start; s<start+n; ) {
		int en = s + int(dp[s%n][0].first);
		en = min(en, start + n);
        while ((en-s-1) % (k-1) != 0) {
			asum[en+1] = asum[en];
			en++;
        }

        moved.clear();
        moves.clear();
		for (int x = s; x+k <= en; x += k-1) {
			if (canMove(x,s)) {
				moves.insert(x);
			}
		}
		while (!moves.empty()) {
			int x = *moves.begin();
			moves.erase(moves.begin());
			moved.insert(x);
			int y = x+k;
			for (int i = 1; i <= k-1; ++i) {
				asum[x+i] = asum[s];
			}

			cout << (x%n);
			for (int i = 0; i < k; ++i) {
				if (x+i < start + n) {
					a[(x+i)%n] = asum[x+i+1] - asum[x+i];
				}
				cout << " " << avg + a[(x+i)%n];
			}
			cout << endl;

			if (x > s) {
				if (canMove(x - (k-1),s)) {
					moves.insert(x - (k-1));
				} else {
					moves.erase(x - (k-1));
				}
			}
			if (y < en) {
				if (canMove(x + (k-1),s)) {
					moves.insert(x + (k-1));
				} else {
					moves.erase(x + (k-1));
				}
			}
		}

		s = en;
	}

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 200200

int n, K, a[N], b[N];

map <int, int> mp;

int tot, s;

map <int, int> lst[N];

int nxt[N][20];

void solve(int l, int r, int rem = 0) {
    if (l + K > r) {
        int sum = 0;
        for (int i = l; i <= r; i ++) sum += b[i%n] - s, b[i%n] = s;
        b[l%n] += rem, b[r%n] += sum - rem;
        printf("%d", (l - 1) % n);
        for (int i = l; i < l + K; i ++) printf(" %d", b[i%n]);
        puts(""); return;
    }
    int sum = 0;
    for (int i = l; i < l + K; i ++) sum += b[i%n] - s;
    if (sum >= rem) {
        solve(l, l + K - 1, rem);
        solve(l + K - 1, r, 0);
    }
    else {
        sum -= b[(l+K-1)%n] - s;
        solve(l + K - 1, r, rem - sum);
        solve(l, l + K - 1, rem);
    }
}

typedef pair <int, int> pii;

int main() {
    scanf("%d %d", &n, &K);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[n+i] = a[i], s += a[i];
    for (int i = 1; i <= n; i ++) b[i%n] = a[i];
    s /= n;
    for (int i = 1; i <= 2 * n; i ++) a[i] = a[i] - s + a[i-1];
    for (int i = 2 * n; i; i --) {
        int &k = mp[a[i]];
        if (!k) k = ++tot;
        nxt[i][0] = lst[k][(i+1)%(K-1)];
        for (int j = 0; nxt[i][j]; j ++) nxt[i][j+1] = nxt[nxt[i][j]][j];
        lst[k][i%(K-1)] = i;
    }
    pii ans = pii(1e9, 0); int r = n % (K - 1);
    for (int i = 1; i <= n; i ++) {
        int k = 0, u = i;
        for (int j = 19; ~j; j --) {
            int v = nxt[u][j];
            if (v && v <= n + i) u = v, k ^= 1 << j;
        }
        int qr = k % (K - 1);
        if (qr >= r) k -= qr - r;
        else k -= qr - r + K - 1;
        ans = min(ans, pii((n - k) / (K - 1), i));
    }
    if (ans.first >= (n - 3 + K) / (K - 1)) printf("%d\n", (n - 3 + K) / (K - 1)), solve(1, n);
    else {
        printf("%d\n", ans.first);
        int u = ans.second, k = n - (K - 1) * ans.first;
        while (-- k) {
            if (u + 1 < nxt[u][0]) solve(u + 1, nxt[u][0]);
            u = nxt[u][0]; assert(u > 0);
        }
        if (u + 1 < n + ans.second) solve(u + 1, n + ans.second);
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pc putchar
#define gc getchar
#define pb push_back
#define ppb pop_back
#define x first
#define y second
#define bitcnt __builtin_popcount
#define bitctz __builtin_ctz
#define bitclz __builtin_clz
#define f0(i, a, b) for (int i = a; i < b; i ++)
#define f1(i, a, b) for (int i = a; i <= b; i ++)
#define f2(i, a, b) for (int i = a; i > b; i --)
#define f3(i, a, b) for (int i = a; i >= b; i --)
#pragma comment(linker, "/STACK:102400000,102400000")
template <class T> void chkmax(T &a, T b) {if (a < b) a = b;}
template <class T> void chkmin(T &a, T b) {if (a > b) a = b;}

typedef double ld;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef pair <int, ll> pil;
typedef pair <ll, int> pli;
typedef pair <ll, ll> pll;
typedef pair <double, int> pdi;
typedef vector <int> vi;

const int inf = 0x3f3f3f3f;
const ll linf = 2e18 + 100;
const ld dinf = 2e18 + 100;
const ld eps = 1e-7;
const ld pi = 3.14159265358979323846264338327950288L;

bool scanld(ld &x) {double xx; int f = scanf("%lf", &xx); x = xx; return f > 0;}
void printld(const ld &x) {printf("%.10lf", fabs(x) < eps ? 0 : (double)x);}

inline char nc() {
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
template <class T> void read(T &x){
	char c=nc(),b=1;
	for (;!((c>='0'&&c<='9')||c==EOF);c=nc()) if (c=='-') b=-b;
	for (x=0;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

inline void read(char *s){
	char c=nc(); int len=0;
	for (;c==' '||c=='\t'||c=='\n';c=nc());
	for (;!(c==' '||c=='\t'||c=='\n'||c==EOF);s[len++]=c,c=nc()); s[len]=0;
}

#define N 200005

int n, k, con;
int a[N], p[N];

map <pii, int> h;

pii nxt[N][18];

int st;

void update(int p, int v, vi &vec) {
    if (v < 0) vec.pb(a[p]);
    else vec.pb(v), a[p] = v;
    return;
}

void print(int p, vi &vec) {
    printf("%d", (p+st+1)%n);
    for (auto x : vec) printf(" %d", x);
    puts("");
    return;
}

void solve(int L, int R, int nd) {
    if (R == L) {
        assert(a[L] == nd+con);
		return;
    }
    assert(0<=L&&L<n);
    assert(0<=R&&R<n);
	vi vec;
    if (R-L+1 <= k) {
        int sum = accumulate(a+L, a+R+1, 0);
        assert(sum == nd + (R-L+1)*con);
        update(L, con+nd, vec);
        f1(i, L+1, R) update(i, con, vec);
        f0(i, 0, k-(R-L+1)) update((R+i+1)%n, -1, vec);
        print(L, vec);
        return;
    }
    int sum = accumulate(a+L, a+L+k, 0);
    if (sum >= nd + (k-1)*con) {
        f0(i, L, L+k) {
            if (i==L) update(i, con+nd, vec);
            else if (i < L+k-1) update(i, con, vec);
            else update(i, sum-nd-(k-1)*con, vec);
        }
        print(L, vec);
        solve(L+k-1, R, 0);
        return;
    }
    else {
        int md = nd+k*con-accumulate(a+L, a+L+k-1, 0)-con;
        solve(L+k-1, R, md);
        assert(a[L+k-1]==con+md);
        f0(i, L, L+k) {
            if (i==L) update(i, con+nd, vec);
            else update(i, con, vec);
        }
        print(L, vec);
        return;
    }
}

int get_cnt(int L, int R) {
    return (R-L+k-2)/(k-1);
}

int main() {
    /// I don't know.
    int T = 0;
	while (1) {
		read(n), read(k);
		if (!n) break;
        int sum = 0; h.clear();
		f0(i, 0, n) read(a[i]), sum += a[i];
		con = sum / n;
		f0(i, 0, n) a[i] -= con, a[i+n] = a[i];
		int nn = n << 1;
		partial_sum(a, a+nn, p);
		f0(i, 0, 18) nxt[nn][0] = {nn, inf};
		f3(i, nn-1, 0) {
			int id = i % (k-1);
			int nid = (id < k-2 ? id+1 : 0);
			pii cur = pii(p[i], nid);
			if (!h.count(cur)) h[cur] = nn;
			int pt = h[cur];
			nxt[i][0] = {pt, get_cnt(i, pt-1)};
			h[pii(p[i], id)] = i;
		}

		f0(j, 0, 18) nxt[nn][j] = pii(nn, inf);

		f0(j, 1, 18) f0(i, 0, nn) {
			int pt = nxt[i][j-1].x, ct = nxt[i][j-1].y;
			nxt[i][j] = pii(nxt[pt][j-1].x, min(inf, ct+nxt[pt][j-1].y));
		}

		pii ans = pii(get_cnt(0, n-1), -1);
		f0(i, 0, n) {
			int res = 0, cur = i;
			bool flag = (i == 6);
			for (int j = 17; j >= 0; j --) if (nxt[cur][j].x < n+i) {
				res += nxt[cur][j].y;
				cur = nxt[cur][j].x;
			}
			if (nxt[cur][0].x == n+i) {
				res += nxt[cur][0].y;
				if (res < ans.x) {
					ans = pii(res, i);
				}
			}
		}
		f0(i, 0, nn) a[i] += con;
		printf("%d\n", ans.x);
		st = n-1;
		if (ans.y < 0) solve(0, n-1, 0);
		else {
			int pos = ans.y;
			st = pos + 1 > n ? 1 : pos + 1;
			rotate(a, a+st, a+n);
			st = pos;
			int sum = 0, L, R;
			for (L = 0, R = 0; L < n; R ++) {
				sum += a[R] - con;
				if (sum == 0 && (R-L)%(k-1)==0) {
					solve(L, R, 0);
					L = R + 1;
				}
			}
			assert(R==n);
		}
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=100005;
int n,k,a[N],b[N<<1],c[N<<1],dp[N],sum;
int ans;
map<int,int> M[N];
int chu(int x){
	return (x-1)%n;
}
void Solve(int l,int r){
	//cout<<l<<" "<<r<<endl;
	c[l-1]=0; For(i,l,r)c[i]=c[i-1]+b[i]-sum;
	int i=l,j=l+(k-1);
	while(j-(k-2)<=r){
		if(c[j]>=0){
			for(int o=j-(k-1);o>=i;o-=k-1){
				//cout<<o<<" fjzzq\n";
				wri(chu(o));
				int su=-c[o];
				wri(b[o]+=su);
				For(p,o+1,o+(k-2)){
					if(p<=n){su+=sum-b[p];wri(sum); b[p]=sum;} else wri(b[p-n]);
				}
				if(o+k-1>n)writeln(b[o+k-1-n]); else writeln(b[o+(k-1)]-=su);
			}
			i=j;
		}
		j+=k-1;
	}
}
void solve(int l,int r){
	//cout<<l<<" fjzzq "<<r<<endl;
	c[l-1]=0; For(i,l,r)c[i]=c[i-1]+b[i]-sum;
	int i=l,j=l+(k-1);
	while(j<=r){
		//if(r>n)cout<<c[j]<<" zyytxdy "<<j<<endl;
		if(c[j]>=0){
			for(int o=j-(k-1);o>=i;o-=k-1){
				//cout<<o<<" fjzzq\n";
				wri(chu(o));
				int su=-c[o];
				wri(b[o]+=su);
				For(p,o+1,o+(k-2)){
					su+=sum-b[p];
					wri(sum);
				}
				writeln(b[o+(k-1)]-=su);
			}
			i=j;
		}
		j+=k-1;
	}
}
int main(){
	//freopen("std.in","r",stdin);
	n=read(),k=read();
	For(i,1,n){
		b[i+n]=b[i]=a[i]=read(); sum+=a[i];
	}
	sum/=n;
	For(i,1,n)a[i]=a[i]-sum+a[i-1];
	//For(i,1,n)wri(a[i]); puts("fhsagf");
	//For(i,0,k-2)M[i][0]=1;
	For(i,1,n){
		auto it=M[(i-1)%(k-1)].find(a[i]);
		if(it!=M[(i-1)%(k-1)].end())dp[i]=it->second+1;
		else dp[i]=1;
		M[i%(k-1)][a[i]]=max(M[i%(k-1)][a[i]],dp[i]);
		ans=max(ans,dp[i]);
		//cout<<i<<" "<<t<<endl;
	}
	//cout<<ans<<endl;
	int jb=(n-ans+k-2)/(k-1);
	writeln(jb);
	if((n-1+k-2)/(k-1)==jb){Solve(1,n); return 0;}
	int an=n-1-jb*(k-1); //cout<<an<<endl;
	Rep(i,n,1)if(dp[i]==ans){
		int j=i;
		For(o,1,an){
			int O=j-1;
			//cout<<j<<endl;
			while((j-O-1)%(k-1)||a[j]!=a[O]||dp[O]+1!=dp[j])O--;
			solve(O+1,j); j=O;
		}
		solve(i+1,n+j);
		return 0;
	}
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<ll,ll> Pll;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> T gcd(T a, T b){return !b?a:gcd(b,a%b);}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 2e5+10;
int n,k,a[N],step,pos=-1,buc[N],nxt[N],dp[N];ll ave,qz[N];Pll b[N];vector<Vi>ans;
int calc(int n){return n<=k?1:(n+k-3)/(k-1);}
void solve(int l, int r){
	rep(i,l,r)dp[i]=1,nxt[i]=n+1;
	per(i,r-1,l){
		buc[b[i+1].se%(k-1)]=i+1;
		if(buc[(b[i].se+1)%(k-1)]<n)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	//	if(b[i+1].se%(k-1)==(b[i].se+1)%(k-1)&&b[i+1].se>b[i].se+1)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	}
	rep(i,l,r)buc[b[i].se%(k-1)]=n+1;//WA
	int p=r;rep(i,l,r)if(dp[i]>dp[p])p=i;if(dp[p]<n%(k-1))return;
	int tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
	if((n-tot)/(k-1)<step)step=(n-tot)/(k-1),pos=p;
}
void perform(int l, int r, int x, int y){
	Vi q={l%n};assert(r>l);int A=0,B=0;
	rep(i,0,k-1){
		int j=(l+i)%n;A+=a[j];
		a[j]=!i?x:(i>r-l?a[j]:(i==r-l?y:0)),q.pb(ave+a[j]);B+=a[j];
	}
//	assert(A==B);
	ans.pb(q);
}
void getans(int l, int r, int x=0, int y=0){
	if(l==r){assert(a[l%n]==(x|y));return;}
	if(r-l+1<=k){perform(l,r,x,y);return;}
	int sum=0;rep(i,l,l+k-1)sum+=a[i%n];
	if(sum>=x){
		perform(l,l+k-1,x,sum-x);getans(l+k-1,r,0,y);return;
	}
	sum=0;rep(i,l,l+k-2)sum+=a[i%n];
	getans(l+k-1,r,x-sum,y);perform(l,l+k-1,x,0);
}
int main() {
	read(n);read(k);
	rep(i,0,n-1)read(a[i]),ave+=a[i];ave/=n;
	rep(i,0,n-1)a[i]-=ave;
	step=calc(n);
	rep(i,0,n-1)qz[i]=(i-1>=0?qz[i-1]:0)+a[i];
	rep(i,0,n-1)b[i]=mp(qz[i],i);sort(b,b+n);
	rep(i,0,n)buc[i]=n+1;
	rep(l,0,n-1){
		int r=l;while(r+1<n&&b[r+1].fi==b[l].fi)r++;
		solve(l,r);l=r;
	}
	fprintf(stderr,"%d %d\n",step,pos);
	if(pos==-1)getans(0,n-1);//WA
	else{
		int p=pos;int tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
		int q=p;rep(t,1,tot-1){
			assert((b[nxt[q]].se-(b[q].se+1))%(k-1)==0);
			getans(b[q].se+1,b[nxt[q]].se),q=nxt[q];
		}
	//	rep(i,0,n-1)a[n+i]=a[i],qz[n+i]=qz[i];
		getans(b[q].se+1,b[p].se+n);
	}
	printf("%d\n",SZ(ans));
	for(auto b:ans){for(auto c:b)printf("%d ",c);puts("");}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define all(v) ((v).begin()),((v).end())

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;
	//printf("answer(s = %d, e = %d)\n", s, ee);
	for(e = ee; (e - s + 1) % (K - 1);) {
		A[++e + 1] = sum / N;
		P[e] = 0;
	}
	//printf("e = %d\n", e);
	vector<int> ava;
	for(int i = s; i <= e; i += K - 1) if(check(i)) {
		chk[i] = true;
		ava.push_back(i);
	}
	/*
	for(int i = s; i <= e + 1; i++) printf("%d ", A[i]);
	puts("");
	for(int i = s; i <= e; i++) printf(" %d", P[i]);
	puts("");
	*/

	while(!ava.empty()) {
		int t = ava.back();
		//printf("t = %d\n", t);
		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++) printf("%d ", P[i]);
	//puts("");

	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++) {
		//printf("ps[%d] = %d\n", i, ps[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--) {
			//printf("i = %d, ps[i] = %d, j = %d, v[i][j] = %d\n", i, ps[i], j, v[i][j]);
			int t = (v[i][j] + 1) % (K - 1);
			int l = upper_bound(all(u[t]), 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);
			//printf("l = %d, cnt[%d] = %d, w[%d] = %d\n", l, v[i][j], cnt[v[i][j]], v[i][j], w[v[i][j]]);
			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;
}

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

#define ASSERT(expr) \
{ \
    if (!(expr)) { \
        fprintf(stderr, "ERROR AT %s:%d", __FILE__, __LINE__); \
        exit(1); \
    } \
}

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);
    }
    // [pos1, pos2)
    int distance(int pos1, int pos2)
    {
        if (pos1 < pos2) return pos2-pos1;
        else return n-pos1+pos2;
    }
    // [pos1, pos2)
    bool suitable(int pos1, int pos2)
    {
        return (distance(pos1, pos2)-1) % (k-1) == 0;
    }
    // [pos1, pos2)
    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];
            }
            ASSERT(sum-extra == (R-L)*avg);
            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";
            // debug();
            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";
            // debug();
            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";
            // debug();
        }
    }
    void solve()
    {
        cin >> n >> k;
        for(int i = 0; i < n; i ++) {
            cin >> data[i];
            avg += data[i];
        }
        avg /= n;
        // cout << "avg = " << avg << endl;

        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];
            }
            ASSERT(sum % (R-L) == 0 && sum / (R-L) == avg);
        }
        for(int i = 0; i < (int)bestplan.size(); i ++) {
            // cout << bestplan[i].first << " " << bestplan[i].second << endl;
        // }
        // 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);
            }
        }
    }
}

int main()
{
    Solve::solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))

using uint=unsigned;
using ull=unsigned long long;

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

void print(ll x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<endl;
	#else
	cout<<"Yes"<<endl;
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<endl;
	#else
	cout<<"No"<<endl;
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
int mask(int i){
	return (int(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
    static random_device rd;
    static mt19937_64 gen(rd());
    #endif
    return uniform_int_distribution<ll>(l, r)(gen);
}

vvc<int> dub(vi a){
	int n=a.size();
	int h=topbit(n)+1;
	vvc<int> res(h);
	res[0]=a;
	rng(i,1,h){
		res[i].resize(n,-1);
		rep(j,n)
			if(res[i-1][j]!=-1)
				res[i][j]=res[i-1][res[i-1][j]];
	}
	return res;
}

pair<int,vi> sub(int len,vi x,int m){
	//cerr<<x<<endl;
	dmp(x);
	int n=x.size();
	rep(i,n)x.pb(x[i]+len);
	n*=2;
	vi a(n,-1);
	map<int,int> pos;
	per(i,n){
		if(pos.count((x[i]+1)%m))a[i]=pos[(x[i]+1)%m];
		pos[x[i]%m]=i;
	}
	dmp(a);
	auto b=dub(a);
	dmp(b);
	pi mx(0,-1);
	rep(i,n/2){
		int cur=i,c=0;
		per(j,b.size()){
			if(b[j][cur]!=-1&&b[j][cur]<=i+n/2){
				c+=1<<j;
				cur=b[j][cur];
			}
		}
		chmax(mx,pi(c,i));
	}
	dmp(mx);
	int z=mx.a;
	while(z&&z%m!=len%m)z--;
	if(z==0)
		return make_pair(-1,vi{});
	vi res;
	int cur=mx.b;
	rep(i,z){
		res.pb(x[cur]%len);
		cur=a[cur];
	}
	dmp(res);
	sort(all(res));
	return make_pair(res.size(),res);
}

void waf(vi cur,int s,int k,int n,int off,vi&head,vvc<int>&val){
	dmp(cur);
	vi ex(cur.size(),s);
	int mn=inf;
	for(int ii=0;ii<(int)cur.size()-1;ii+=k-1){
		int i=min(ii,(int)cur.size()-k);
		int has=0;
		rng(j,ii,i+k)
			has+=cur[j];
		int tar=0;
		rng(j,ii,i+k)
			tar+=ex[j];
		//cerr<<i<<" "<<ex<<" "<<cur<<endl;
		if(has>=tar){
			head.pb((i+off)%n);
			val.eb(k);
			rng(j,i,ii)
				val.back()[j-i]=cur[j];
			rng(j,ii,i+k-1)
				has-=(val.back()[j-i]=ex[j]);
			val.back()[k-1]=has;
			cur[i+k-1]=has;
			for(int z=ii-(k-1);z>=mn;z-=(k-1)){
				head.pb((z+off)%n);
				val.eb(k,s);
				val.back()[0]=ex[z];
			}
			rng(j,min(mn,i),i+k){
				ex[j]=s;
			}
			rng(j,min(mn,i),i+k-1){
				cur[j]=s;
			}
			mn=inf;
		}else{
			ex[i+k-1]=tar-(has-cur[i+k-1]);
			chmin(mn,i);
		}
	}
	dmp(ex);
	assert(mn==inf);
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	int n,k;cin>>n>>k;
	vi a=readvi(n);

	int s=accumulate(all(a),int(0));
	s/=n;

	pair<int,vi> ans(0,vi{0});

	map<int,vi> pos;
	{
		int cur=0;
		rep(i,n){
			//cerr<<cur-s*i<<endl;
			pos[cur-s*i].pb(i);
			cur+=a[i];
		}
	}
	for(auto kv:pos){
		chmax(ans,sub(n,kv.b,k-1));
	}

	vi head;
	vvc<int> val;
	vi idx=ans.b;
	idx.pb(idx[0]+n);
	dmp(idx);
	rep(i,(int)idx.size()-1){
		int x=idx[i],y=idx[i+1];
		vi cur;
		rng(j,x,y)cur.pb(a[j%n]);
		waf(cur,s,k,n,x,head,val);
	}

	print(head.size());
	rep(i,head.size()){
		print(head[i],2);
		print(val[i]);
		int cnt=0;
		rng(j,head[i],head[i]+k)
			cnt+=a[j%n];
		assert(cnt==accumulate(all(val[i]),int(0)));
		rep(j,k)
			a[(head[i]+j)%n]=val[i][j];
	}
	assert(a==vi(n,s));
}

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
//#define NDEBUG
#include <cassert>

const int MN = 1e5 + 100;

int l2(int x)
{
	return 31 - __builtin_clz(x);
}
int N, K, A, a[MN], p[MN*2], x[MN], X, l[MN*2], q[MN*2][20], F, C;
std::vector<int> v[MN];

int md(int x) {
	if(x<N) return x;
	if((x-=N)<N) return x;
	return x%N;
}
void solve(int p, int e = 0)
{
	int s = 0;
	if(p+K > F+N)
	{
		int k = F+N-p;
		for(int i = 0;i < k;++i)
			s += a[md(p+i)];
		assert(s==k*A+e);
		printf("%d ", p%N);
		printf("%d ", a[md(p)] = A+e);
		for(int i = 1;i < k;++i)
			printf("%d ", a[md(p+i)] = A);
		for(int i = k;i < K;++i)
			printf("%d%c", a[md(p+i)], " \n"[i+1==K]);
		return;
	}
	for(int i = 0;i < K;++i)
		s += a[md(p+i)];
	if((s -= A*(K-1) + e) < 0)
		solve(p+K-1, -(s-=a[md(p+K-1)])), s += a[md(p+K-1)];
	printf("%d ", p%N);
	printf("%d ", a[md(p)] = A+e);
	for(int i = 1;i < K-1;++i)
		printf("%d ", a[md(p+i)] = A);
	printf("%d\n", a[md(p+K-1)] = s);
}

int main(void)
{
	memset(l, -1, sizeof(l));
	memset(q, -1, sizeof(q));
	scanf("%d%d", &N, &K);
	C = N;
	for(int i = 0;i < N;++i) scanf("%d", a+i), A+=a[i];
	A/=N;
	for(int i = 0;i < N;++i) p[i+1] = p[i]+a[i]-A, x[i] = p[i+1];
	std::sort(x, x+N);
	X = std::unique(x, x+N)-x;
	for(int i = 0;i < N;++i)
		p[i] = std::lower_bound(x, x+X, p[i])-x, v[p[i]].push_back(i);//cut from i-1 -> i
	for(int y = 0, V;y < X;++y)
	{
		std::vector<int> &v = ::v[y];
		V = v.size();
		for(int i = 0;i < V;++i) v.push_back(v[i]+N);
		for(int i = v.size()-1;i >= 0;--i)
		{
			q[i][0] = l[(v[i]+1)%(K-1)];
			for(int j = 0;~q[i][j]&&~q[q[i][j]][j];++j)
				q[i][j+1] = q[q[i][j]][j];
			l[v[i]%(K-1)] = i;
		}
		for(int i = 0, n, c;i < V;++i)
		{
			n = i, c = 0;
			for(int j = l2(V), k;j >= 0;--j)
				if(~q[n][j] && (k=q[n][j])<i+V)
					c += (v[k]-v[n]-(1<<j))/(K-1), n = k;
			c += (v[i]+N-v[n]+K-3)/(K-1);
			if(c < C || !(C < c) && v[i] < F)
				C = c, F = v[i];
		}
		for(int i = 0;i < v.size();++i)
			for(int j = 0;~q[i][j];++j)
				q[i][j] = -1;
		for(int x : v)
			l[x%(K-1)] = -1;
	}

	printf("%d\n", C);
	for(int i = F;i < F+N;++i)
		if(a[md(i)] != A)
			solve(i);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read()
{
    ll x = 0; char ch = gc(); bool positive = 1;
    for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
    for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
    return positive ? x : -x;
}
inline void write(ll a)
{
    if(a<0)
	{
        a=-a;
		putchar('-');
    }
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd()
{
    return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=100005;
int n,k,a[N],b[N<<1],c[N<<1],dp[N],sum,ans;
map<int,int> M[N];
int chu(int x)
{
    return (x-1)%n;
}
void Solve(int l,int r)
{
    c[l-1]=0; For(i,l,r)c[i]=c[i-1]+b[i]-sum;
    int i=l,j=l+(k-1);
    while(j-(k-2)<=r)
	{
    	if(c[j]>=0)
		{
    		for(int o=j-(k-1);o>=i;o-=k-1)
			{
    			wri(chu(o));
    			int su=-c[o];
    			wri(b[o]+=su);
    			For(p,o+1,o+(k-2))
				{
    				if(p<=n)
					{
						su+=sum-b[p];
						wri(sum);
						b[p]=sum;
					}
					else wri(b[p-n]);
    			}
    			if(o+k-1>n)writeln(b[o+k-1-n]); else writeln(b[o+(k-1)]-=su);
    		}
    		i=j;
    	}
    	j+=k-1;
    }
}
void solve(int l,int r)
{
    c[l-1]=0; For(i,l,r)c[i]=c[i-1]+b[i]-sum;
    int i=l,j=l+(k-1);
    while(j<=r)
	{
    	if(c[j]>=0)
		{
    		for(int o=j-(k-1);o>=i;o-=k-1)
			{
    			wri(chu(o));
    			int su=-c[o];
    			wri(b[o]+=su);
    			For(p,o+1,o+(k-2))
				{
    				su+=sum-b[p];
    				wri(sum);
    			}
    			writeln(b[o+(k-1)]-=su);
    		}
    		i=j;
    	}
    	j+=k-1;
    }
}
int main()
{
    n=read(),k=read();
    For(i,1,n)
	{
    	b[i+n]=b[i]=a[i]=read();
		sum+=a[i];
    }
    sum/=n;
    For(i,1,n)a[i]=a[i]-sum+a[i-1];
    For(i,1,n)
	{
    	auto it=M[(i-1)%(k-1)].find(a[i]);
    	if(it!=M[(i-1)%(k-1)].end())dp[i]=it->second+1;
    	else dp[i]=1;
    	M[i%(k-1)][a[i]]=max(M[i%(k-1)][a[i]],dp[i]);
    	ans=max(ans,dp[i]);
    }
    int jb=(n-ans+k-2)/(k-1);
    writeln(jb);
    if((n-1+k-2)/(k-1)==jb){Solve(1,n); return 0;}
    int an=n-1-jb*(k-1);
    Rep(i,n,1)if(dp[i]==ans)
	{
    	int j=i;
    	For(o,1,an)
		{
    		int O=j-1;
    		while((j-O-1)%(k-1)||a[j]!=a[O]||dp[O]+1!=dp[j])O--;
    		solve(O+1,j); j=O;
    	}
    	solve(i+1,n+j);
    	return 0;
    }
}

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

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>;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

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.pb({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].fi].fi, go[j - 1][i].se + go[j - 1][go[j - 1][i].fi].se};

        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].fi <= to)
            {
                curans += go[j][cur].se;
                cur = go[j][cur].fi;
            }
            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.se, a + n);
    partial_sum(a, a + n, psum + 1);
    restore(0);

    assert((int)answer.size() == ans.fi);
    printf("%d\n", (int)answer.size());
    for (auto &t : answer)
    {
        printf("%d", (t.fi + ans.se) % n);
        for (auto tt : t.se) printf(" %lld", tt);
        printf("\n");
    }
    return 0;
}

#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = 200500, LOG = 20, inf = 1e9;

int n, k, a[N], up[LOG][N], cost[LOG][N];
int sum, A, pref[N];
pll ans;
map<int, int> lst[N];
int get(int i){
	i %= n;
	if(i <= ans.s)
		i += n;
	return a[i];
}
void gg(int l, int r, ll x, ll y){
	//cerr << l << " " << r << endl;
	a[l] = x, a[r] = y;
	for(int i = l + 1; i < r; i++)
		a[i] = 0;
	printf("%d ", l % n);
	for(int i = l; i <= r; i++)
		printf("%d ", a[i] + A);
	printf("\n");
}
void ff(int l, int r, ll need){
	if(l == r && a[l] == need)
		return;
	if(r - l + 1 <= k){
		a[l] = need;
		for(int i = l + 1; i <= r; i++)
			a[i] = 0;
		printf("%d ", l % n);
		for(int i = l; i <= l + k - 1; i++)
			printf("%d ", get(i) + A);
	   	printf("\n");
		return;
	}
    ll s = 0;
	for(int i = l; i <= l + k - 1; i++)
		s += a[i];
	if(s < need){
		ff(l + k - 1, r, need - s + a[l + k - 1]);
		gg(l, l + k - 1, need, 0);
	}else{
		gg(l, l + k - 1, need, s - need);
		ff(l + k - 1, r, 0);
	}
}
int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		sum += a[i];
	}
	A = sum / n;
	for(int i = 0; i < n; i++)
		a[i + n] = (a[i] -= A);
	for(int i = 1; i < n + n; i++)
		pref[i] = pref[i - 1] + a[i];
	for(int i = 0; i < LOG; i++)
		for(int j = 0; j < N; j++)
			up[i][j] = n + n;
	for(int i = n + n - 1; i >= 0; i--){
		int rem = (i + 1) % (k - 1);
		if(lst[rem].count(pref[i]))
			up[0][i] = lst[rem][pref[i]], cost[0][i] = (lst[rem][pref[i]] - i + k - 3) / (k - 1);
		lst[i % (k - 1)][pref[i]] = i;
	}
	for(int j = 1; j < LOG; j++)
		for(int i = 0; i < n + n; i++)
            up[j][i] = up[j - 1][ up[j - 1][i] ], cost[j][i] = cost[j - 1][i] + cost[j - 1][ up[j - 1][i] ];
	ans = {inf, inf};
	for(int i = 0; i < n; i++){
		int v = i;
		int res = 0;
		for(int j = LOG - 1; j >= 0; j--)
			if(up[j][v] <= n + i)
				res += cost[j][v], v = up[j][v];
		if(v <= n + i)
			res += (n + i - v + k - 2) / (k - 1);
		ans = min(ans, {res, i});
	}
	printf("%d\n", ans.f);
	int v = ans.s;
	while(v < ans.s + n){
		if(up[0][v] < ans.s + n)
			ff(v + 1, up[0][v], 0);
		else
			ff(v + 1, ans.s + n, 0);
		v = up[0][v];
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 100010

int n, k, a[N<<1], pre[N];
int tar;

map<int, int> last[N];
int prv[N];
int dp[N];

typedef pair<int, int> pii;
vector<pii> op;

vector<pair<int, vector<int> > > answer;

int val(int x) {
	return x?pre[x]-pre[x-1]:pre[0];
}

void oper(int st, int l) {
	if (l == 1) return;
	int fi = pre[st+k-1];
	if (st) fi -= pre[st-1];
	int total = pre[st+l-1];
	if (st) total -= pre[st-1];
	if (fi >= total) {
		vector<int> cur(k);
		cur[0] = tar + total;
		for (int i = 1; i < k - 1; i ++) cur[i] = tar;
		cur[k-1] = tar + fi - total;
		for (int i = 0; i < k; i ++) {
			pre[st+i] = (st+i)?pre[st+i-1]:0;
			pre[st+i] += cur[i] - tar;
		}
		answer.emplace_back(st, cur);
		if (l == k) return;
		int nl = max(k, l - k + 1);
		oper(st + l - nl, nl);
	}
	else {
		int nl = max(k, l - k + 1);
		if (l - k + 1 >= k) oper(st + l - nl, nl);
		else {
			vector<int> cur(k);
			int nt = st + l - k;
			for (int i = 0; i < k; i ++) {
				cur[i] = tar + val(nt+i);
				if (nt + i >= st + k) cur[i] = tar;
			}
			cur[0] += total - fi;
			for (int i = 0; i < k; i ++) {
				pre[nt+i] = (nt+i)?pre[nt+i-1]:0;
				pre[nt+i] += cur[i] - tar;
			}
			answer.emplace_back(nt, cur);
		}
		vector<int> cur(k);
		cur[0] = tar + total;
		for (int i = 1; i < k; i ++) cur[i] = tar;
		for (int i = 0; i < k; i ++) {
			pre[st+i] = (st+i)?pre[st+i-1]:0;
			pre[st+i] += cur[i] - tar;
		}
		answer.emplace_back(st, cur);
	}
}

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i ++) scanf("%d", a + i), a[n+i] = a[i];
	tar = accumulate(a, a + n, 0);
	assert(tar % n == 0);
	tar /= n;
	for (int i = 0; i < n; i ++) a[i] -= tar;
	partial_sum(a, a + n, pre);
	last[0][pre[0]] = 0;
	for (int i = 0; i < n; i ++) prv[i] = -1;
	for (int i = 1; i < n; i++) {
		int j = (i - 1) % (k - 1);
		if (last[j].count(pre[i])) {
			prv[i] = last[j][pre[i]];
		}
		last[i%(k-1)][pre[i]] = i;
	}
	for (int i = 0; i < n; i ++) {
		if (prv[i] >= 0) dp[i] = dp[prv[i]] + 1;
	}
	int r = (n - 1) % (k - 1);
	int mx = 0, id = -1;
	for (int i = 0; i < n; i ++) {
		if (dp[i] >= r) {
			int cur = (k - 1) * ((dp[i] - r) / (k - 1)) + r;
			if (cur > mx) {
				mx = cur;
				id = i;
			}
		}
	}
	int st = 0;
	if (id >= 0) {
		int ed = id;
		for (int i = 0; i < mx; i ++) {
			op.emplace_back(prv[id] + 1, id - prv[id]);
			id = prv[id];
		}
		op.emplace_back(ed + 1, id + n - ed);
		st = (id + 1) % n;
	}
	else op.emplace_back(0, n);
	for (auto &p:op) p.first -= st;
	rotate(a, a + st, a + n);
	partial_sum(a, a + n, pre);
	for (auto p:op) oper(p.first, p.second);
	printf("%d\n", answer.size());
	for (auto &p:answer) {
		printf("%d", (p.first + st) % n);
		for (auto q:p.second) printf(" %d", q);
		puts("");
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

// missed charactors / ?

template <typename T>
boolean vmin(T& a, T b) {
	return (a > b) ? (a = b, true) : (false);
}
template <typename T>
boolean vmax(T& a, T b) {
	return (a < b) ? (a = b, true) : (false);
}

template <typename T>
T smax(T x) {
	return x;
}
template <typename T, typename ...K>
T smax(T a, const K &...args) {
	return max(a, smax(args...));
}

template <typename T>
T smin(T x) {
	return x;
}
template <typename T, typename ...K>
T smin(T a, const K &...args) {
	return min(a, smin(args...));
}

// debugging lib

#define VN(x) #x
#define Vmsg(x) VN(x) << " = " << (x)
#define printv(x) cerr << VN(x) << " = " << (x);
#define debug(...) fprintf(stderr, __VA_ARGS__);

template <typename A, typename B>
ostream& operator << (ostream& os, const pair<A, B>& z) {
	os << "(" << z.first << ", " << z.second << ')';
	return os;
}
template <typename T>
ostream& operator << (ostream& os, const vector<T>& a) {
	boolean isfirst = true;
	os << "{";
	for (auto z : a) {
		if (!isfirst) {
			os << ", ";
		}
		os << z;
		isfirst = false;
	}
	os << '}';
	return os;
}

const int bzmax = 20;

int n, k;

int main() {
	scanf("%d%d", &n, &k);
	vector<int> a (n << 1);
	int sum = 0, avg = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", a.data() + i);
		sum += a[i];
	}
	avg = sum / n;
	for (int i = n; i < (n << 1); i++) {
		a[i] = a[i - n];
	}
	int n2 = n << 1;
	vector<int> psum (n2);
	map<pair<int, int>, vector<int>> G;
	for (int i = 0, _sum = 0; i < n2; i++) {
		psum[i] = _sum;
		_sum += a[i] - avg;
	}
//	cerr << Vmsg(psum) << '\n';
	for (int i = 0; i < n2; i++) {
		G[make_pair(i % (k - 1), psum[i])].push_back(i);
	}
	vector<vector<int>> bz (bzmax, vector<int>(n2 + 1));
	for (int i = 0; i < n2; i++) {
		auto id = make_pair((i + 1) % (k - 1), psum[i]);
		if (!G.count(id)) {
			bz[0][i] = n2;
		} else {
			auto& v = G[id];
			auto it = upper_bound(v.begin(), v.end(), i);
			if (it == v.end()) {
				bz[0][i] = n2;
			} else {
				bz[0][i] = *it;
			}
		}
	}
	bz[0][n2] = n2;
	for (int i = 1; i < bzmax; i++) {
		for (int j = 0; j <= n2; j++) {
			bz[i][j] = bz[i - 1][bz[i - 1][j]];
		}
	}
//	cerr << Vmsg(bz[0])<< '\n';
	int ans = n2, id = -1;
	for (int i = 0; i < n; i++) {
		int tmp = 0, p = i;
		for (int j = bzmax - 1; ~j; j--) {
			if (bz[j][p] < i + n) {
				p = bz[j][p];
				tmp |= 1 << j;
			}
		}
		tmp = (p - i - tmp) / (k - 1) + (i + n - p + k - 3) / (k - 1);
		if (tmp < ans) {
			ans = tmp;
			id = i;
		}
	}
	printf("%d\n", ans);

	vector<int> bel (n2);

	auto rearrange = [&] (int p, int tol, int tor) {
//		cerr << Vmsg(a) << '\n';
		int sum = 0;
		int r = p + k - 1, bl = r;
		while (bl >= p && bel[bl] == bel[r])
			bl--;
		for (int i = bl + 1; i <= r; i++)
			sum += a[i];
		assert(sum == avg * (r - bl) + tol + tor);
		for (int i = bl + 1; i <= r; i++)
			a[i] = avg;
		if (bl >= p) {
			a[bl] += tol;
		} else {
			assert(!tol);
		}
		a[r] += tor;
		bel[r]++;
		printf("%d", p % n);
		for (int i = 0; i < k; i++)
			printf(" %d", a[i + p]);
		putchar('\n');
	};

	auto construct = [&] (int l, int r) {
		if (l == r)
			return;
		int c = (r - l + k - 2) / (k - 1);
		for (int i = l, t = 0; i <= r; i++, t++) {
			if (t < k) {
				bel[i] = 0;
			} else {
				bel[i] = (t - k) / (k - 1) + 1;
			}
		}
		vector<int> bsum (c, 0);
		vector<int> deg (c, 0);
		vector<int> Gl (c, 0), Gr (c, 0);
		for (int i = l; i <= r; i++) {
			bsum[bel[i]] += a[i] - avg;
		}
		int sum = 0;
		for (int i = 0; i < c; i++) {
			if (sum < 0) {
				Gl[i] = -sum;
				deg[i - 1]++;
			}
			if (sum > 0) {
				Gr[i - 1] = sum;
				deg[i]++;
			}
			sum += bsum[i];
		}
//		cerr << Vmsg(bsum) << '\n';
//		cerr << Vmsg(Gl) << '\n';
//		cerr << Vmsg(Gr) << '\n';
		queue<int> Q;
		for (int i = 0; i < c; i++) {
			if (!deg[i]) {
				Q.push(i);
			}
		}
		while (!Q.empty()) {
			int p = Q.front();
			Q.pop();
			int rr = min(r, l + (k - 1) * (p + 1));
			int ll = rr - (k - 1);
			rearrange(ll, Gl[p], Gr[p]);
			if (Gl[p] && !--deg[p - 1])
				Q.push(p - 1);
			if (Gr[p] && !--deg[p + 1])
				Q.push(p + 1);
		}
	};

	int p = id, R = id + n;
	while (bz[0][p] < R) {
		construct(p, bz[0][p] - 1);
		p = bz[0][p];
	}
	construct(p, R - 1);
	return 0;
}

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int cnt = 0, i, already = 0;
      for (i = tans.second; cnt < n; ) {
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        cnt += nxt[i][0].second.second;
        i = nxt[i][0].first;
      }
      if (tans.first == 2408) {
    	  printf("%d\n", ans.size());
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos=0,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;j<logn;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

int ans=0,ts[100000],te[100000];
int tl[100000],tr[100000];
int tt[200000];
inline void addans(int s,int e,int l,int r)
{
	ts[ans]=s; te[ans]=e;
	tl[ans]=l; tr[ans++]=r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],x=sum[l]-num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,i,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,i,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,sta[top],L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	int T=sum[r]-sum[i-k+1]+num[i-k+1];
	if(i-k+1!=r)
		addans(i-k+1,r,T-ave*(r-i+k-1),ave);
	while(top>=0){
		int g=sta[top]-k+1;
		int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
		addans(g,sta[top],L,ave);
		sum[g]+=L-num[g];
		top--;
	}
	return;
}

void print(void)
{
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		tt[ts[i]]=tl[i];
		for(int j=ts[i]+1;j<te[i];j++){
			printf("%d ",ave);
			tt[j]=ave;
		}
		printf("%d",tr[i]);
		tt[te[i]]=tr[i];
		for(int j=te[i];j<ts[i]+k-1;j++)
			printf(" %d",tt[j>=n?j-(n>>1):j]);
		putchar('\n');
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	print();
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=tt[i]=tt[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
7 5
0 0 0 0 0 0 21
*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 200000, W = sqrt(N);
int x[N], y[N], nxt[N], n, k, ano, need;
vector<int> mp, in[N], an, can;

vector<int> g[N];
int inc[N];
bool vis[N];

void upan(int w) { if (can.size() < an.size())an = can, ano = w; }

void upb(int w, int st){
	can.clear();
	int j = st;
	st += n >> 1;
	while (j < st){
		can.push_back(j);
		j += k;
		if (x[j] == w)j = nxt[j];
	}
	upan(w);
}

void ups(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st;
	st = j + n / 2;
	while (j < st){
		can.push_back(j);
		j += k;
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
	}
	upan(w);
}

void gob(int w){
	f(i, 0, k)upb(w, i);
}

void gos(int w){
	f(i, 0, in[w].size() >> 1)ups(w, i);
}

void go(int w){
	if ((int)in[w].size() >> 1 > w)gob(w);
	else gos(w);
}

void fn(){
	n >>= 1;
	++k;
	for (int u: an)for (int z: g[u])++inc[z];
	stack<int> q;
	for (int u: an)if (inc[u] == 0)q.push(u);
	while (!q.empty()){
		int v = q.top();
		q.pop();
		for (int u: g[v])if (--inc[u] == 0)q.push(u);
		if (v >= n)v -= n;
		printf("%d", v);

		if (!vis[v]){
			int t = v ? v - 1 : n - 1;
			y[v] = need - x[t];
		}

		int t = v + k - 1;
		if (t >= n)t -= n;
		if (!vis[t])y[t] = need + x[t];

		f(i, v, v + k){
			int j = i >= n ? i - n : i;
			if (vis[j] || (i != v && i + 1 != v + k))y[j] = need;
			vis[j] = true;
		}

		f(i, 0, k)printf(" %d", y[i + v >= n ? i + v - n : i + v]);
		printf("\n");
	}
}

int main(){
	scanf("%d%d", &n, &k);
	--k;
	f(i, 0, n)scanf("%d", y + i);
	f(i, 0, n)need += y[i];
	need /= n;
	int car = 0;
	f(i, 0, n){
		x[i] = y[i] + car - need;
		car = x[i];
	}
	mp.resize(n);
	f(i, 0, n)mp[i] = x[i];
	sort(mp.begin(), mp.end());
	mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
	if (mp.size() == 1){
		printf("0\n");
		return 0;
	}
	f(i, 0, n)x[i] = lower_bound(mp.begin(), mp.end(), x[i]) - mp.begin();
	f(i, 0, n)x[i + n] = x[i];
	n <<= 1;
	f(i, 0, n)in[x[i]].push_back(i);
	nxt[n - 1] = n;
	for (int i = n - 2; i >= 0; --i)if (x[i] == x[i + 1])nxt[i] = nxt[i + 1]; else nxt[i] = i + 1;

	an.resize(n);
	f(i, 0, mp.size())go(i);

	ano = mp[ano];
	f(i, 0, n)x[i] = mp[x[i]] - ano;
	f(i, 1, an.size())if (an[i] == an[i - 1] + k){
		if (x[an[i] - 1] > 0 && x[an[i]] > 0)g[an[i - 1]].push_back(an[i]);
		else if (x[an[i]] < 0 && x[an[i] - 1] < 0)g[an[i]].push_back(an[i - 1]);
	}
	int t = an[0] + n / 2;
	if (t == an.back() + k){
		if (x[t - 1] > 0 && x[t] > 0)g[an.back()].push_back(an[0]);
		else if (x[t] < 0 && x[t - 1] < 0)g[an[0]].push_back(an.back());
	}

	printf("%d\n", (int)an.size());
	fn();
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<ll,ll> Pll;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> T gcd(T a, T b){return !b?a:gcd(b,a%b);}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 2e5+10;
int n,k,a[N],step,pos=-1,buc[N],nxt[N],dp[N];ll ave,qz[N];Pll b[N];vector<Vi>ans;
int calc(int n){return n<=k?1:(n+k-3)/(k-1);}
void solve(int l, int r){
	rep(i,l,r)dp[i]=1,nxt[i]=n+1;
	per(i,r-1,l){
		buc[b[i+1].se%(k-1)]=i+1;
		if(buc[(b[i].se+1)%(k-1)]<n)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	//	if(b[i+1].se%(k-1)==(b[i].se+1)%(k-1)&&b[i+1].se>b[i].se+1)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	}
	int p=r;rep(i,l,r)if(dp[i]>dp[p])p=i;if(dp[p]<n%(k-1))return;
	int tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
	if((n-tot)/(k-1)<step)step=(n-tot)/(k-1),pos=p;
	rep(i,l,r)buc[b[i].se%(k-1)]=n+1;
}
void perform(int l, int r, int x, int y){
	Vi q={l%n};assert(r>l);int A=0,B=0;
	rep(i,0,k-1){
		int j=(l+i)%n;A+=a[j];
		a[j]=!i?x:(i>r-l?a[j]:(i==r-l?y:0)),q.pb(ave+a[j]);B+=a[j];
	}
//	assert(A==B);
	ans.pb(q);
}
void getans(int l, int r, int x=0, int y=0){
	if(l==r)return;
	if(r-l+1<=k){perform(l,r,x,y);return;}
	int sum=0;rep(i,l,l+k-1)sum+=a[i%n];
	if(sum>=x){
		perform(l,l+k-1,x,sum-x);getans(l+k-1,r,0,y);return;
	}
	sum=0;rep(i,l,l+k-2)sum+=a[i%n];
	getans(l+k-1,r,x-sum,y);perform(l,l+k-1,x,0);
}
int main() {
	read(n);read(k);
	rep(i,0,n-1)read(a[i]),ave+=a[i];ave/=n;
	rep(i,0,n-1)a[i]-=ave;
	step=calc(n);
	rep(i,0,n-1)qz[i]=(i-1>=0?qz[i-1]:0)+a[i];
	rep(i,0,n-1)b[i]=mp(qz[i],i);sort(b,b+n);
	rep(i,0,n)buc[i]=n+1;
	rep(l,0,n-1){
		int r=l;while(r+1<n&&b[r+1].fi==b[l].fi)r++;
		solve(l,r);l=r;
	}
	fprintf(stderr,"%d %d\n",step,pos);
	if(pos==-1)getans(0,n-1);//WA
	else{
		int p=pos;int tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
		int q=p;rep(t,1,tot-1)getans(b[q].se+1,b[nxt[q]].se),q=nxt[q];
		rep(i,0,n-1)a[n+i]=a[i],qz[n+i]=qz[i];
		getans(b[q].se+1,b[p].se+n);
	}
	printf("%d\n",SZ(ans));
	for(auto b:ans){for(auto c:b)printf("%d ",c);puts("");}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos=0,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;j<logn;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{if(n>10000)printf("%d %d\n",l,r);
	int d=num[l],x=sum[l]-num[l];
	for(int i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
*/

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn * 2], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute2(int l, int r);
void distribute1(int l, int r){
    //cout << "d1 " << l << " " << r << endl;
    int len = r <= l ? r + n - l: r - l;
    if(len <= k){
        int ok = 1;
        for(int i = l;;){
            ok &= a[i] == avg;
            i = (i + 1) % n;
            if(i == r) break;
        }
        if(ok) return;
        cout << l;
        for(int i = l;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == r) break;
        }
        for(int i = r; i != (l + k) % n; i = (i + 1) % n)
            cout << " " << a[i];
        cout << "\n";
        return;
    }
    int sum = 0;
    for(int i = l;;){
        sum += a[i];
        i = (i + 1) % n;
        if(i == (l + k) % n) break;
    }
    if(sum >= (k - 1) * avg){
        cout << l;
        for(int i = l;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == (l + k - 1) % n) break;
        }
        cout << " " << (a[(l + k - 1) % n] = sum - (k - 1) * avg) << "\n";
        distribute1((l + k - 1) % n, r);
        return;
    }
    distribute2((l + k - 1) % n, r);
    distribute1(l, (l + k) % n);
}

void distribute2(int l, int r){
    //cout << "d2 " << l << " " << r << endl;
    int len = r <= l ? r + n - l: r - l;
    if(len <= k){
        distribute1(l, r);
        return;
    }
    int sum = 0;
    for(int i = (r + n - k) % n;;){
        sum += a[i];
        i = (i + 1) % n;
        if(i == r) break;
    }
    if(sum >= (k - 1) * avg){
        cout << (r + n - k) % n;
        cout << " " << (a[(r + n - k) % n] = sum - (k - 1) * avg);
        for(int i = (r + n - k + 1) % n;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << "\n";
        distribute2(l, (r + n - k) % n);
        return;
    }
    distribute1(l, (r + n - k + 1) % n);
    distribute2((r + n - k) % n, r);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i + n] = a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        vector<int> pos(k - 1, c * 2);
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; i; i -= 1){
            next[i][0] = pos[(v[i] + 1) % (k - 1)];
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1) if(v[i] % (k - 1) == 0){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        //cout << "cut" << " " << cut[i] << endl;
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        interval.push_back({l, r});
        if(r <= l) r += n;
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute1(p.first, p.second);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 200000, W = sqrt(N);
int x[N], y[N], nxt[N], n, k, ano, need;
vector<int> mp, in[N], an, can;
pair<int, int> pp(-1, 0);

vector<int> g[N];
int inc[N];
bool vis[N];

void upan(int w) { if (can.size() < an.size())an = can, ano = w, pp.first = -1; }

void upb(int w, int st){
	can.clear();
	int j = st;
	st += n >> 1;
	if (x[j] == w)j = nxt[j];
	while (j < st){
		can.push_back(j);
		j += k;
		if (x[j] == w)j = nxt[j];
	}
	upan(w);
}

void ups(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st, pan = 0, ed = j + n / 2;
	while (j < ed){
		int t = (in[w][i] - j + k - 1) / k;
		pan += t;
		j += t * k;
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
	}
	if (pan < (int)an.size()){
		pp = make_pair(w, st);
		an.resize(pan);
	}
}

void upsa(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st;
	st = j + n / 2;
	while (j < st){
		can.push_back(j);
		j += k;
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
	}
	an.resize(can.size() + 1);
	upan(w);
}

void gob(int w){
	f(i, 0, k)upb(w, i);
}

void gos(int w){
	f(i, 0, in[w].size() >> 1)ups(w, i);
}

void go(int w){
	if ((int)in[w].size() >> 1 > W)gob(w);
	else gos(w);
}

void fn(){
	n >>= 1;
	++k;
	for (int u: an)for (int z: g[u])++inc[z];
	stack<int> q;
	for (int u: an)if (inc[u] == 0)q.push(u);
	while (!q.empty()){
		int v = q.top();
		q.pop();
		for (int u: g[v])if (--inc[u] == 0)q.push(u);
		if (v >= n)v -= n;
		printf("%d", v);

		if (!vis[v]){
			int t = v ? v - 1 : n - 1;
			y[v] = need - x[t];
		}

		int t = v + k - 1;
		if (t >= n)t -= n;
		if (!vis[t])y[t] = need + x[t];

		f(i, v, v + k){
			int j = i >= n ? i - n : i;
			if (vis[j] || (i != v && i + 1 != v + k))y[j] = need;
			vis[j] = true;
		}

		f(i, 0, k)printf(" %d", y[i + v >= n ? i + v - n : i + v]);
		printf("\n");
	}
}

int main(){
	scanf("%d%d", &n, &k);
	--k;
	f(i, 0, n)scanf("%d", y + i);
	f(i, 0, n)need += y[i];
	need /= n;
	int car = 0;
	f(i, 0, n){
		x[i] = y[i] + car - need;
		car = x[i];
	}
	mp.resize(n);
	f(i, 0, n)mp[i] = x[i];
	sort(mp.begin(), mp.end());
	mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
	if (mp.size() == 1){
		printf("0\n");
		return 0;
	}
	f(i, 0, n)x[i] = lower_bound(mp.begin(), mp.end(), x[i]) - mp.begin();
	f(i, 0, n)x[i + n] = x[i];
	n <<= 1;
	f(i, 0, n)in[x[i]].push_back(i);
	nxt[n - 1] = n;
	for (int i = n - 2; i >= 0; --i)if (x[i] == x[i + 1])nxt[i] = nxt[i + 1]; else nxt[i] = i + 1;

	an.resize(n);
	f(i, 0, mp.size())go(i);
	if (pp.first != -1)upsa(pp.first, pp.second);

	ano = mp[ano];
	f(i, 0, n)x[i] = mp[x[i]] - ano;
	f(i, 1, an.size())if (an[i] == an[i - 1] + k){
		if (x[an[i] - 1] > 0 && x[an[i]] > 0)g[an[i - 1]].push_back(an[i]);
		else if (x[an[i]] < 0 && x[an[i] - 1] < 0)g[an[i]].push_back(an[i - 1]);
	}
	int t = an[0] + n / 2;
	if (t == an.back() + k){
		if (x[t - 1] > 0 && x[t] > 0)g[an.back()].push_back(an[0]);
		else if (x[t] < 0 && x[t - 1] < 0)g[an[0]].push_back(an.back());
	}

	printf("%d\n", (int)an.size());
	fn();
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,M=20; vector<int>v[N]; bool fc[N];
int n,k,lf,rf,len,ans,pos,opt,s[N],q[N],w[N],c[N],nxt[N],f[N][M],g[N][M]; map<int,int>ft[N];
struct node {int l,r,x,t;}e[N],h[N];
inline bool operator < (node a,node b){
	if(a.x==b.x) return a.t<b.t; return a.x<b.x;
}
set<node>rt; set<int>gt;
inline int read(){
	int num=0; char g=getchar(); while(g<48||57<g) g=getchar();
	while(47<g&&g<58) num=(num<<1)+(num<<3)+g-48,g=getchar(); return num;
}
inline void write(long long u){
	if(u<0) putchar('-'),u=-u; if(u>9) write(u/10); putchar(u%10+'0');
}
inline int lowbit(int u){
	return (u&(-u));
}
inline void addit(int u,int x){
	while(u<=n) s[u]+=x,u+=lowbit(u);
}
inline int getit(int u){
	int ans=0; while(u>0) ans+=s[u],u-=lowbit(u); return ans;
}
inline int getans(int l,int r){
	while(l>=n&&r>=n) l-=n,r-=n;
	if(r<l) return 0; if(r<n) return getit(r+1)-getit(l); return getit(n)-getit(l)+getit(r-n+1);
}
inline void change(int u,int x){
	u=u%n; addit(u+1,x-w[u]),w[u]=x;
}
inline int getpos(int u){
	return w[u%n];
}
inline void dele(int u){
	gt.insert(u);
	int a=getans(lf,h[u].l-1),b=getans(h[u].r+1,rf);
	if(*gt.begin()!=u) a=getans(h[*--gt.find(u)].r,h[u].l-1);
	if(*gt.rbegin()!=u) b=getans(h[u].r+1,h[*++gt.find(u)].l);
	node g=h[u]; g.x=max(a,b); rt.erase(g);
	gt.erase(u);
}
inline void push(int u){
	gt.insert(u);
	int a=getans(lf,h[u].l-1),b=getans(h[u].r+1,rf);
	if(*gt.begin()!=u) a=getans(h[*--gt.find(u)].r,h[u].l-1);
	if(*gt.rbegin()!=u) b=getans(h[u].r+1,h[*++gt.find(u)].l);
	node g=h[u]; g.x=max(a,b); rt.insert(g);
	gt.erase(u);
}
inline void delit(int l,int r){
	if(l==r) return; lf=l,rf=r;
	int u=l,len=0; u=min(u+k-1,r),len++,h[len].l=l,h[len].r=u;
	while(u<r) len++,h[len].l=u,u=min(u+k-1,r),h[len].r=u;
	for(int i=1;i<=len;i++){
		h[i].t=i;
		node v; v.l=h[i].l,v.r=h[i].r,v.t=i,v.x=max(getans(l,v.l-1),getans(v.r+1,r)); rt.insert(v);
	}
	while(gt.size()) gt.erase(*gt.begin());
	while(rt.size()){
		node g=*rt.begin(); fc[g.t]=1;
		if(g.x>opt){
			puts("鸡你太美");
		}
		if(g.t-1>=1&&!fc[g.t-1]) dele(g.t-1);
		if(g.t+1<=len&&!fc[g.t+1]) dele(g.t+1);
		for(int i=g.l+1;i<g.r;i++) change(i,0); gt.insert(g.t);
		int a=getans(l,g.l-1),b=getans(g.r+1,r); change(g.l,-a),change(g.r,-b);
		if(g.t-1>=1&&!fc[g.t-1]) push(g.t-1);
		if(g.t+1<=len&&!fc[g.t+1]) push(g.t+1);
		q[0]++,q[q[0]]=g.l%n; v[q[0]].resize(k+1);
		for(int x=1;x<=k;x++) v[q[0]][x]=getpos(x+g.l-1); rt.erase(g);
	}
}
int main(){
	n=read(),k=read(); int sum=0; ans=19260817,pos=0;
	for(int i=0;i<n;i++) w[i]=w[i+n]=read(),sum+=w[i]; sum=sum/n;
	for(int i=0;i<(n<<1);i++) w[i]-=sum; c[0]=w[0]; opt=sum;
	for(int i=1;i<(n<<1);i++) c[i]=c[i-1]+w[i];
	for(int i=1;i<=n;i++) addit(i,w[i-1]);
	for(int x=0;x<=18;x++) f[(n<<1)][x]=(n<<1);
	for(int i=(n<<1)-1;i>=0;i--){
		nxt[i]=ft[i%(k-1)][c[i]]; if(!nxt[i]) nxt[i]=(n<<1);
		f[i][0]=nxt[i],g[i][0]=(nxt[i]-i-1)/(k-1);
		for(int x=1;x<=18;x++) f[i][x]=f[f[i][x-1]][x-1],g[i][x]=g[i][x-1]+g[f[i][x-1]][x-1];
		if(i<n){
			int to=i+n-1,u=i,sum=0;
			for(int x=18;x>=0;x--) while(f[u][x]<=to) sum+=g[u][x],u=f[u][x];
			sum=sum+(to-u)/(k-1)+((to-u)%(k-1)>0);
			if(sum<=ans) ans=sum,pos=i;
		}
		if(i>0) ft[(i-1)%(k-1)][c[i]]=i;
	}
	int to=pos+n-1;
	while(nxt[pos]<=to) len++,e[len].l=pos+1,e[len].r=nxt[pos],pos=nxt[pos];
	len++,e[len].l=pos+1,e[len].r=to+1;
	for(int i=1;i<=len;i++){
		delit(e[i].l,e[i].r);
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		write(q[i]),putchar(' ');
		for(int x=1;x<=k;x++) write(v[i][x]+sum),putchar(' '); putchar('\n');
	}
	return 0;
}
//6 2
//0 1 0 1 4 0

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int already = 0;
      for (int i = tans.second, cnt = 0; cnt < n; ) {
    	int brans = ans.size();
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        if (already || (tans.first == 2408 && ans.size() > 2408)) {
        	already = 1;
      	  printf("419: %I64d %d\n", nxt[i][0].second.first, (int)ans.size(), brans);
        }
        i = nxt[i][0].first;
        cnt += nxt[i][0].second.second;
      }

      if (ans.size() != tans.first) {
    	  printf("426: %d\n", ans.size());
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  if (!(a >= 0)) {
    			  printf("431: %d\n", a);
    		  }
    	  }
      }
      for (int i = 0; i < n; i++) {
    	  if (orig[i] != avg) {
    		  printf("437: %d %d\n", i, orig[i]);
    	  }
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int cnt = 0, i, already = 0;
      for (i = tans.second; cnt < n; ) {
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        cnt += nxt[i][0].second.second;
        i = nxt[i][0].first;
      }
      if (tans.first == 2408) {
    	  for (int i = 5; i > 0; i--) {
    		  printf("%d\n", ans[ans.size() - i][0]);
    	  }
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int already = 0;
      for (int i = tans.second, cnt = 0; cnt < n; ) {
    	int brans = ans.size();
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        if (already || (tans.first == 2408 && ans.size() - brans != nxt[i][0].second.first)) {
        	already = 1;
      	  printf("419: %I64d %d\n", nxt[i][0].second.first, (int)ans.size(), brans);
        }
        i = nxt[i][0].first;
        cnt += nxt[i][0].second.second;
      }

      if (ans.size() != tans.first) {
    	  printf("426: %d\n", ans);
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  if (!(a >= 0)) {
    			  printf("431: %d\n", a);
    		  }
    	  }
      }
      for (int i = 0; i < n; i++) {
    	  if (orig[i] != avg) {
    		  printf("437: %d %d\n", i, orig[i]);
    	  }
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos=0,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;j<logn;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],x=sum[l]-num[l];
	for(int i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
*/

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		LL hh = -s[n - k];
		if(hh < 0) ans[tp].push_back(V), a[n - k + 1] = s[n - k + 1] = 0;
		else ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include <cstdio>
#include <algorithm>

int n,k,a[100001],ave,ans,mx,Last[100001],len[100001],nxt[100001],val[100001];
struct point{
	int sum,ord;
}b[100001];
bool cmp(point a,point b){return a.sum<b.sum||(a.sum==b.sum&&a.ord<b.ord);}
void solve(int l,int r,long long res=0){
	int len=(l<=r)?r-l+1:(r-l+1+n);
//	printf("solve %d %d %d\n",l,r,len);
	if(len==1)return;
	if(len<=k){
		printf("%d ",l);
		for(int i=0;i<len;i++){
			printf("%d ",a[(i+l)%n]=1ll*res*(i==0)+ave);
		}
		for(int i=len;i<k;i++)
			printf("%d ",a[(i+l)%n]);
		putchar('\n');
		return;
	}
	long long sum=0;
	for(int i=0;i<k;i++)
		sum+=a[(i+l)%n];
//	printf("%d\n",sum);
	if(sum>=res+1ll*(k-1)*ave){
		printf("%d ",l);
		for(int i=0;i<k-1;i++)
			printf("%d ",a[(i+l)%n]=1ll*res*(i==0)+ave);
		printf("%d ",a[(k-1+l)%n]=sum-res-1ll*(k-1)*ave);
		putchar('\n');
		solve((l+k-1)%n,r,0);
		return;
	}
	else{
		solve((l+k-1)%n,r,len*ave+res-(len-k+1)*ave-sum+a[(l+k)%n]);
		printf("%d ",l);
		for(int i=0;i<k;i++)
			printf("%d ",a[(i+l)%n]=1ll*res*(i==0)+ave);
		putchar('\n');
		return;
	}
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)scanf("%d",a+i),ave+=a[i];
	ave/=n;
	for(int i=0;i<n;i++){
		b[i].sum=a[i]-ave;
		if(i)b[i].sum+=b[i-1].sum;
		b[i].ord=i;
	}
	mx=(n-1+k-1-1)/(k-1);
	std::sort(b,b+n,cmp);
	for(int i=0;i<=n;i++)val[i]=0x7f7f7f7f;
	for(int i=n%(k-1);i<=n;i+=k-1)
		if(i)val[i]=(n-i)/(k-1);
	for(int i=1;i<=n;i++)val[i]=std::min(val[i],val[i-1]);
	for(int i=0,last=0;i<n;i++)
		if(i==n-1||b[i+1].sum!=b[i].sum){
			for(int j=i;j>=last;j--){
				int u=(b[j].ord+1)%(k-1);
				nxt[b[j].ord]=Last[u];
				if(Last[u])len[b[j].ord]=len[Last[u]];
				++len[b[j].ord];
//				printf("%d %d %d %d\n",b[j].ord,nxt[b[j].ord],len[b[j].ord],val[len[b[j].ord]]);
				if(mx>val[len[b[j].ord]]){
					mx=val[len[b[j].ord]];
					ans=b[j].ord;
				}
				Last[b[j].ord%(k-1)]=b[j].ord;
			}
			for(int j=i;j>=last;j--)Last[b[j].ord%(k-1)]=0;
			last=i+1;
		}
	printf("%d\n",mx);
	if(mx<(n-1+k-1-1)/(k-1)){
		int p=0;
		for(int i=n%(k-1);i<=n;i+=k-1)
			if(val[i]==mx)p=i+1;
		for(int i=ans;p;i=nxt[i],--p){
			solve((i==n-1)?0:(i+1),nxt[i]?nxt[i]:ans);
		}
	}
	else solve(0,n-1);
}

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn * 2], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute2(int l, int r);
void distribute1(int l, int r){
    //cout << "d1 " << l << " " << r << endl;
    int len = r <= l ? r + n - l: r - l;
    if(len <= k){
        int ok = 1;
        for(int i = l;;){
            ok &= a[i] == avg;
            i = (i + 1) % n;
            if(i == r) break;
        }
        if(ok) return;
        int sum = 0;
        for(int i = l;;){
            sum += a[i];
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << l;
        for(int i = l;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == (r + n - 1) % n) break;
        }
        cout << " " << (a[(r + n - 1) % n] = sum - (len - 1) * avg);
        for(int i = r; i != (l + k) % n; i = (i + 1) % n)
            cout << " " << a[i];
        cout << "\n";
        return;
    }
    int sum = 0;
    for(int i = l;;){
        sum += a[i];
        i = (i + 1) % n;
        if(i == (l + k) % n) break;
    }
    if(sum >= (k - 1) * avg){
        cout << l;
        for(int i = l;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == (l + k - 1) % n) break;
        }
        cout << " " << (a[(l + k - 1) % n] = sum - (k - 1) * avg) << "\n";
        distribute1((l + k - 1) % n, r);
        return;
    }
    distribute2((l + k - 1) % n, r);
    distribute1(l, (l + k) % n);
}

void distribute2(int l, int r){
    //cout << "d2 " << l << " " << r << endl;
    int len = r <= l ? r + n - l: r - l;
    if(len <= k){
        int ok = 1;
        for(int i = l;;){
            ok &= a[i] == avg;
            i = (i + 1) % n;
            if(i == r) break;
        }
        if(ok) return;
        int sum = 0;
        for(int i = l;;){
            sum += a[i];
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << l;
        cout << " " << (a[l] = sum - (len - 1) * avg);
        for(int i = (l + 1) % n;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == r) break;
        }
        for(int i = r; i != (l + k) % n; i = (i + 1) % n)
            cout << " " << a[i];
        cout << "\n";
        return;
    }
    int sum = 0;
    for(int i = (r + n - k) % n;;){
        sum += a[i];
        i = (i + 1) % n;
        if(i == r) break;
    }
    if(sum >= (k - 1) * avg){
        cout << (r + n - k) % n;
        cout << " " << (a[(r + n - k) % n] = sum - (k - 1) * avg);
        for(int i = (r + n - k + 1) % n;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << "\n";
        distribute2(l, (r + n - k + 1) % n);
        return;
    }
    distribute1(l, (r + n - k + 1) % n);
    distribute2((r + n - k) % n, r);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i + n] = a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        vector<int> pos(k - 1, c * 2);
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; i; i -= 1){
            next[i][0] = pos[(v[i] + 1) % (k - 1)];
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1) if(v[i] % (k - 1) == 0){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        //cout << "cut" << " " << cut[i] << endl;
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        interval.push_back({l, r});
        if(r <= l) r += n;
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute1(p.first, p.second);
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(i-k+1==l)
				addans(i-k+1,t-d+ave+num[i-k+1],d);
			else addans(i-k+1,ave,d);
			while(top>=0){
				int g=sta[top--]-k+1;
				addans(g,sum[i]-sum[g]-ave*(i-g),ave);
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&mod(i-Lst+1,k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
3 2
40 63 5
*/

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		LL hh = -s[x - 1]; a[x] = 0;
		for(reg i = x; i >= n - k + 1; i--) {
			if(a[i] + hh + V < 0) a[i] = -V, hh += V + a[i];
			else {a[i] += hh; break;}
		} for(reg i = n - k + 1; i <= x; i++) ans[tp].push_back(V + a[i]), s[i] = s[i - 1] + a[i];
		for(reg i = x + 1; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos=0,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;j<logn;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

int ans=0,ts[100000],te[100000];
int tl[100000],tr[100000];
int tt[200000];
inline void addans(int s,int e,int l,int r)
{
	ts[ans]=s; te[ans]=e;
	tl[ans]=l; tr[ans++]=r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],x=sum[l]-num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,i,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,i,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,sta[top],L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	int T=sum[r]-sum[i-k+1]+num[i-k+1];
	if(i-k+1!=r)
		addans(i-k+1,r,T-ave*(r-i+k-1),ave);
	while(top>=0){
		int g=sta[top]-k+1;
		int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
		addans(g,sta[top],L,ave);
		sum[g]+=L-num[g];
		top--;
	}
	return;
}

void print(void)
{
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		tt[ts[i]]=tl[i];
		for(int j=ts[i]+1;j<te[i];j++){
			printf("%d ",ave);
			tt[j]=ave;
		}
		printf("%d",tr[i]);
		tt[te[i]]=tr[i];
		for(int j=te[i];j<ts[i]+k-1;j++)
			printf(" %d",tt[j>=n?j-(n>>1):j]);
		putchar('\n');
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	print();
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=tt[i]=tt[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
7 5
0 0 0 0 0 0 21
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],x=sum[l]-num[l];
	for(int i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{if(n==17329)printf("%d %d\n",l,r);
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
*/

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>

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][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);
    }
    // [pos1, pos2)
    int distance(int pos1, int pos2)
    {
        if (pos1 < pos2) return pos2-pos1;
        else return n-pos1+pos2;
    }
    // [pos1, pos2)
    bool suitable(int pos1, int pos2)
    {
        return (distance(pos1, pos2)-1) % (k-1) == 0;
    }
    // [pos1, pos2)
    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) {
            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";
            // debug();
            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";
            // debug();
            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;
            }
            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i%n];
            }
            cout << "\n";
            // debug();
        }
    }
    void solve()
    {
        cin >> n >> k;
        for(int i = 0; i < n; i ++) {
            cin >> data[i];
            avg += data[i];
        }
        avg /= n;
        // cout << "avg = " << avg << endl;

        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];
            }
            assert(sum % (R-L) == 0 && sum / (R-L) == avg);
        }
        for(int i = 0; i < (int)bestplan.size(); i ++) {
            // cout << bestplan[i].first << " " << bestplan[i].second << endl;
        // }
        // 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);
            }
        }
    }
}

int main()
{
    Solve::solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,M=20; vector<int>v[N]; bool fc[N];
int n,k,lf,rf,len,ans,pos,opt,les,s[N],q[N],w[N],c[N],nxt[N],f[N][M],g[N][M]; map<int,int>ft[N];
struct node {int l,r,x,t;}e[N],h[N];
inline bool operator < (node a,node b){
	if(a.x==b.x) return a.t<b.t; return a.x<b.x;
}
set<node>rt; set<int>gt;
inline int read(){
	int num=0; char g=getchar(); while(g<48||57<g) g=getchar();
	while(47<g&&g<58) num=(num<<1)+(num<<3)+g-48,g=getchar(); return num;
}
inline void write(long long u){
	if(u<0) putchar('-'),u=-u; if(u>9) write(u/10); putchar(u%10+'0');
}
inline int lowbit(int u){
	return (u&(-u));
}
inline void addit(int u,int x){
	while(u<=n) s[u]+=x,u+=lowbit(u);
}
inline int getit(int u){
	int ans=0; while(u>0) ans+=s[u],u-=lowbit(u); return ans;
}
inline int getans(int l,int r){
	while(l>=n&&r>=n) l-=n,r-=n;
	if(r<l) return 0; if(r<n) return getit(r+1)-getit(l); return getit(n)-getit(l)+getit(r-n+1);
}
inline void change(int u,int x){
	u=u%n; addit(u+1,x-w[u]),w[u]=x;
}
inline int getpos(int u){
	return w[u%n];
}
inline void dele(int u){
	gt.insert(u);
	node g=h[u]; g.x=getans(h[u].l,h[u].r); rt.erase(g);
	gt.erase(u);
}
inline void solve(int l,int r,int t){
	if(t>les) return;
	int a=getans(lf,h[t].l-1),b=getans(h[t].r+1,rf);
	if(opt-a<0||opt-b<0){
		solve(h[t+1].l,r,t+1);
		q[++q[0]]=h[t].l%n; v[q[0]].resize(k+1);
		for(int x=h[t].l;x<=h[t].r;x++) change(x,0);
		change(h[t].l,-a),change(h[t].r,-b);
		for(int x=h[t].l;x<h[t].l+k;x++) v[q[0]][x-h[t].l+1]=getpos(x);
	}
	else{
		for(int x=h[t].l;x<=h[t].r;x++) change(x,0);
		change(h[t].l,-a),change(h[t].r,-b);
		q[++q[0]]=h[t].l%n; v[q[0]].resize(k+1);
		for(int x=h[t].l;x<h[t].l+k;x++) v[q[0]][x-h[t].l+1]=getpos(x);
		solve(h[t+1].l,r,t+1);
	}
}
inline void delit(int l,int r){
	if(l==r) return; lf=l,rf=r;
	int u=l,len=0; u=min(u+k-1,r),len++,h[len].l=l,h[len].r=u;
	while(u<r) len++,h[len].l=u,u=min(u+k-1,r),h[len].r=u; les=len;
	for(int i=1;i<=len;i++){
		h[i].t=i;
		node v; v.l=h[i].l,v.r=h[i].r,v.t=i,v.x=getans(h[i].l,h[i].r); rt.insert(v);
	}
	solve(lf,rf,1);
}
int main(){
	n=read(),k=read(); int sum=0; ans=19260817,pos=0;
	for(int i=0;i<n;i++) w[i]=w[i+n]=read(),sum+=w[i]; sum=sum/n;
	for(int i=0;i<(n<<1);i++) w[i]-=sum; c[0]=w[0]; opt=sum;
	for(int i=1;i<(n<<1);i++) c[i]=c[i-1]+w[i];
	for(int i=1;i<=n;i++) addit(i,w[i-1]);
	for(int x=0;x<=18;x++) f[(n<<1)][x]=(n<<1);
	for(int i=(n<<1)-1;i>=0;i--){
		nxt[i]=ft[i%(k-1)][c[i]]; if(!nxt[i]) nxt[i]=(n<<1);
		f[i][0]=nxt[i],g[i][0]=(nxt[i]-i-1)/(k-1);
		for(int x=1;x<=18;x++) f[i][x]=f[f[i][x-1]][x-1],g[i][x]=g[i][x-1]+g[f[i][x-1]][x-1];
		if(i<n){
			int to=i+n-1,u=i,sum=0;
			for(int x=18;x>=0;x--) while(f[u][x]<=to) sum+=g[u][x],u=f[u][x];
			sum=sum+(to-u)/(k-1)+((to-u)%(k-1)>0);
			if(sum<=ans) ans=sum,pos=i;
		}
		if(i>0) ft[(i-1)%(k-1)][c[i]]=i;
	}
	int to=pos+n-1;
	while(nxt[pos]<=to) len++,e[len].l=pos+1,e[len].r=nxt[pos],pos=nxt[pos];
	len++,e[len].l=pos+1,e[len].r=to+1;
	for(int i=1;i<=len;i++){
		delit(e[i].l,e[i].r);
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		write(q[i]),putchar(' ');
		for(int x=1;x<=k;x++) write(v[i][x]+sum),putchar(' '); putchar('\n');
	}
	return 0;
}
//6 2
//0 1 0 1 4 0

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

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][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);
    }
    // [pos1, pos2)
    int distance(int pos1, int pos2)
    {
        if (pos1 < pos2) return pos2-pos1;
        else return n-pos1+pos2;
    }
    // [pos1, pos2)
    bool suitable(int pos1, int pos2)
    {
        return (distance(pos1, pos2)-1) % (k-1) == 0;
    }
    // [pos1, pos2)
    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 output(int L, int R, int extra)
    {
        if (R-L == 1) return;
        if (R-L <= k) {
            for(int i = L; i < R; i ++) {
                data[i] = avg;
            }
            data[L] += extra;

            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i];
            }
            cout << "\n";
            return;
        }

        int sum = 0;
        for(int i = 0; i < k; i ++) {
            sum += data[i+L];
        }
        if (sum >= extra + avg*(k-1)) {
            for(int i = L; i < L+k-1; i ++) {
                data[i] = avg;
            }
            data[L] += extra;
            data[R] = sum-avg*(k-1)-extra;

            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i];
            }
            cout << "\n";
            output(L+k-1, R, 0);
        } else {
            output(L+k-1, R, avg*(k-1)-(sum-data[L+k-1])+extra);
            for(int i = L; i < L+k; i ++) {
                data[i] = avg;
            }
            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i];
            }
            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 ++) {
            // cout << bestplan[i].first << " " << bestplan[i].second << endl;
        // }
        // 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);
            }
        }
    }
}

int main()
{
    Solve::solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())

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];
ll b[N];
pii go[N][20];
map<int,int> lst[N];
pii ans=mp(1e9,0);
vector<pair<int,vl>> Ans,tmp;

int cal(int len) {
	if(len<=1) return 0;
	return (len-1+k-2)/(k-1);
}

void apply(int s,vl v) {
	for(int i=s,j=0;i<s+k;i++,j++) b[i]=v[j];
}

void solve(int l,int r,ll t) {
	//cerr<<l<<' '<<r<<' '<<t<<endl;
	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;
		vl vec;
		for(int i=L;i<l;i++) vec.pb(b[i]);
		vec.pb(t);
		for(int i=l+1;i<=r;i++) vec.pb(0);
		for(int i=r+1;i<=R;i++) vec.pb(b[i]);
		Ans.pb(mp(L,vec));apply(L,vec);
	}
	else {
		ll sum=0;
		for(int i=l;i<l+k;i++) sum+=b[i];
		//cerr<<l<<' '<<r<<' '<<sum<<endl;
		if(sum>=t) {
			vl vec;vec.pb(t);
			for(int i=l+1;i<l+k-1;i++) vec.pb(0);
			vec.pb(sum-t);
			Ans.pb(mp(l,vec));apply(l,vec);
			solve(l+k-1,r,0);
		}
		else {
			//cerr<<l<<' '<<r<<endl;
			solve(l+k-1,r,t-sum+b[l+k-1]);
			vl vec;vec.pb(t);
			for(int i=l+1;i<=l+k-1;i++) vec.pb(0);
			Ans.pb(mp(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]=mp(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]=mp(x,cal(x-i));
		}
		else go[i][0]=mp(n+n,cal(n+n-i));
		//cerr<<i<<' '<<go[i][0].fi<<endl;
		for(int j=1;j<20;j++) go[i][j].fi=go[go[i][j-1].fi][j-1].fi,go[i][j].se=go[i][j-1].se+go[go[i][j-1].fi][j-1].se;
		if(i<n) {
			int x=i,w=0;
			for(int j=1;j<20;j++)
				if(go[x][j].fi<i+n) w+=go[x][j].se,x=go[x][j].fi;
			w+=cal(n+i-x);
			//cerr<<i<<' '<<w<<endl;
			ans=min(ans,mp(min(w,cal(n)),i));
		}
		lst[i%(k-1)][sum[i]]=i;
	}
	cout<<ans.fi<<'\n';//cerr<<ans.se<<endl;
	for(int i=0;i<n+n;i++) b[i]=a[i];//,cerr<<b[i]<<' ';
	solve(ans.se,ans.se+n-1,0);tmp=Ans;Ans.clear();//exit(0);
	//for(auto x:Ans) cerr<<x.fi<<endl;
	//cerr<<sz(Ans)<<endl;
	//exit(0);
	int x=ans.se;for(int i=0;i<n+n;i++) b[i]=a[i];
	while(go[x][0].fi<ans.se+n) solve(x,go[x][0].fi-1,0),x=go[x][0].fi;
	solve(x,ans.se+n-1,0);//cerr<<x<<' '<<ans.se+n-1<<endl;
	if(sz(tmp)<sz(Ans)) swap(tmp,Ans);
	for(auto x:Ans) {
		cout<<x.fi%n<<' ';
		for(auto y:x.se) cout<<y+avg<<' ';
		cout<<'\n';
	}
	return 0;
}

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>

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][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);
    }
    // [pos1, pos2)
    int distance(int pos1, int pos2)
    {
        if (pos1 < pos2) return pos2-pos1;
        else return n-pos1+pos2;
    }
    // [pos1, pos2)
    bool suitable(int pos1, int pos2)
    {
        return (distance(pos1, pos2)-1) % (k-1) == 0;
    }
    // [pos1, pos2)
    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];
            }
            assert(sum-extra == (R-L)*avg);
            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";
            // debug();
            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";
            // debug();
            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;
            }
            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i%n];
            }
            cout << "\n";
            // debug();
        }
    }
    void solve()
    {
        cin >> n >> k;
        for(int i = 0; i < n; i ++) {
            cin >> data[i];
            avg += data[i];
        }
        avg /= n;
        // cout << "avg = " << avg << endl;

        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];
            }
            assert(sum % (R-L) == 0 && sum / (R-L) == avg);
        }
        for(int i = 0; i < (int)bestplan.size(); i ++) {
            // cout << bestplan[i].first << " " << bestplan[i].second << endl;
        // }
        // 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);
            }
        }
    }
}

int main()
{
    Solve::solve();
    return 0;
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<int> ans[N];
int n, k, V, a[N * 2], s[N * 2];

I void gao(cr x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		reg hh = s[n] - s[n - k];
		ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		reg up = x + k - 1, hh = s[up] - s[x - 1];
		ans[tp].push_back(hh - s[up] + V), a[x] = hh - s[up]; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(cr i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	reg tt = -1;
	for(reg i = 1; i <= tp; i++) {
		for(reg j = 1; j <= k; j++) {
			if(ans[i][j] < 0) {
				tt = (ans[i][0] + pos) % n;
			}
		}
	} if(tt != -1) pr2(tt);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = 200500, LOG = 20, inf = 1e9;

int n, k, a[N], up[LOG][N], cost[LOG][N];
int sum, A, pref[N];
pll ans;
map<int, int> lst[N];
int get(int i){
	i %= n;
	if(i <= ans.s)
		i += n;
	return a[i];
}
void gg(int l, int r, ll x, ll y){
	//cerr << l << " " << r << endl;
	a[l] = x, a[r] = y;
	for(int i = l + 1; i < r; i++)
		a[i] = 0;
	printf("%d ", l % n + 1);
	for(int i = l; i <= r; i++)
		printf("%d ", a[i] + A);
	printf("\n");
}
void ff(int l, int r, ll need){
	if(l == r && a[l] == need)
		return;
	if(r - l + 1 <= k){
		a[l] = need;
		for(int i = l + 1; i <= r; i++)
			a[i] = 0;
		printf("%d ", l % n + 1);
		for(int i = l; i <= l + k - 1; i++)
			printf("%d ", get(i) + A);
	   	printf("\n");
		return;
	}
    ll s = 0;
	for(int i = l; i <= l + k - 1; i++)
		s += a[i];
	if(s < need){
		ff(l + k - 1, r, need - s + a[l + k - 1]);
		gg(l, l + k - 1, need, 0);
	}else{
		gg(l, l + k - 1, need, s - need);
		ff(l + k - 1, r, 0);
	}
}
int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		sum += a[i];
	}
	A = sum / n;
	for(int i = 0; i < n; i++)
		a[i + n] = (a[i] -= A);
	for(int i = 1; i < n + n; i++)
		pref[i] = pref[i - 1] + a[i];
	for(int i = 0; i < LOG; i++)
		for(int j = 0; j < N; j++)
			up[i][j] = n + n, cost[i][j] = inf;
	for(int i = n + n - 1; i >= 0; i--){
		int rem = (i + 1) % (k - 1);
		if(lst[rem].count(pref[i]))
			up[0][i] = lst[rem][pref[i]], cost[0][i] = (lst[rem][pref[i]] - i + k - 3) / (k - 1);
		lst[i % (k - 1)][pref[i]] = i;
	}
	for(int j = 1; j < LOG; j++)
		for(int i = 0; i < n + n; i++)
            up[j][i] = up[j - 1][ up[j - 1][i] ], cost[j][i] = cost[j - 1][i] + cost[j - 1][ up[j - 1][i] ];
	ans = {inf, inf};
	for(int i = 0; i < n; i++){
		int v = i;
		int res = 0;
		for(int j = LOG - 1; j >= 0; j--)
			if(up[j][v] <= n + i)
				res += cost[j][v], v = up[j][v];
		if(v <= n + i)
			res += (n + i - v + k - 2) / (k - 1);
		ans = min(ans, {res, i});
	}
	printf("%d\n", ans.f);
	int v = ans.s;
	while(v < ans.s + n){
		if(up[0][v] < ans.s + n)
			ff(v + 1, up[0][v], 0);
		else
			ff(v + 1, ans.s + n, 0);
		v = up[0][v];
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(i-k+1==l)
				addans(i-k+1,t-d+ave+num[i-k+1],d);
			else addans(i-k+1,ave,d);
			while(top>=0){
				int g=sta[top]-k+1;
				addans(g,sum[i]-sum[g]-ave*(i-g),ave);
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l-1;
	for(int i=l;i<r;i++){
		if(sum[i]-sum[Lst]==ave*(i-Lst)&&mod(i-Lst,k-1)==1){
			slv(Lst+1,i);
			Lst=i;
		}}
	slv(Lst+1,r);
	if((r-Lst)%(k-1)!=1)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
6 3
1 2 3 1 2 3
8 3
4 4 3 2 3 4 3 1
6 3
3 4 5 3 3 6
*/

//#define USEPB_DS
#define USETR1
#define CPPELEVEN
#define GPP

/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")

#include <math.h>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

#ifndef CPPELEVEN
#ifdef USETR1
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#endif
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef USEPB_DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
// binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
// splay_tree_tag, ov_tree_tag
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    tree_type;
#endif

#define mpr make_pair
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef map<string, int> mpsi;
typedef map<double, int> mpdi;
typedef map<int, int> mpii;

const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                        {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

template <class T>
inline T abs1(T a) {
  return a < 0 ? -a : a;
}

#ifndef CPPELEVEN
template <class T>
inline T max1(T a, T b) {
  return b < a ? a : b;
}
template <class T>
inline T max1(T a, T b, T c) {
  return max1(max1(a, b), c);
}
template <class T>
inline T max1(T a, T b, T c, T d) {
  return max1(max1(a, b, c), d);
}
template <class T>
inline T max1(T a, T b, T c, T d, T e) {
  return max1(max1(a, b, c, d), e);
}
template <class T>
inline T min1(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline T min1(T a, T b, T c) {
  return min1(min1(a, b), c);
}
template <class T>
inline T min1(T a, T b, T c, T d) {
  return min1(min1(a, b, c), d);
}
template <class T>
inline T min1(T a, T b, T c, T d, T e) {
  return min1(min1(a, b, c, d), e);
}
#else
template <typename t, typename t1>
t min1(t a, t1 b) {
  return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
  return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
  return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
  return max1(a, max1(arr...));
}
#endif

inline int jud(double a, double b) {
  if (abs(a) < eps && abs(b) < eps)
    return 0;
  else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
    return 0;
  if (a < b) return -1;
  return 1;
}
template <typename t>
inline int jud(t a, t b) {
  if (a < b) return -1;
  if (a == b) return 0;
  return 1;
}

template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
  if (na == 0) return 0;
  int be = 0, en = na - 1;
  if (*a <= *(a + na - 1)) {
    if (f_lb == 0)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != 1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != -1)
          en = mid;
        else
          be = mid + 1;
      }
    if (f_small && jud(*(a + be), val) == 1) be--;
    if (!f_small && jud(*(a + be), val) == -1) be++;
  } else {
    if (f_lb)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != -1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != 1)
          en = mid;
        else
          be = mid + 1;
      }
    if (!f_small && jud(*(a + be), val) == -1) be--;
    if (f_small && jud(*(a + be), val) == 1) be++;
  }
  return be;
}

template <class T>
inline T lowb(T num) {
  return num & (-num);
}
#ifdef GPP
inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(ull nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(ll nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(ui a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(ull a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
inline int bitmaxl(ll a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
#else
#endif

long long pow(long long n, long long m, long long mod = 0) {
  if (m < 0) return 0;
  long long ans = 1;
  long long k = n;
  while (m) {
    if (m & 1) {
      ans *= k;
      if (mod) ans %= mod;
    }
    k *= k;
    if (mod) k %= mod;
    m >>= 1;
  }
  return ans;
}

#define MOD 1000000007
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
  if (mod == -1) mod = MOD;
  a += b;
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
  cerr << endl;
}
template <class t>
void output2(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}

//...................................................................

const int maxn = 200100;
int orig[maxn], sum[maxn], diff[maxn], avg;
int st[maxn], lst, rst;
pair<int, pii> nxt[maxn][20];
int n, k;
unordered_map<int, int> ump;

int GetSum(int a, int b) {
  if (a > b) {
    return sum[n - 1] - sum[a - 1] + sum[b];
  } else {
    int ans = sum[b];
    if (a) ans -= sum[a - 1];
    return ans;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
#ifdef DEBUG  //..................................................
  freopen("input.txt", "r", stdin);
#endif  //........................................................
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", orig + i);
    sum[i] = orig[i];
    if (i) sum[i] += sum[i - 1];
  }
  avg = sum[n - 1] / n;
  for (int i = 0; i < n; i++) {
    diff[i] = sum[i] - (i + 1) * avg;
  }

  for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
  for (int i = 0; i < k - 1; i++) {
    ump.clear();
    for (int j = i; j < n * 2; j += k - 1) {
      int last_no = (j - 1 + n) % n, cur_no = j % n;
      ump[diff[last_no]] = last_no;
      if (ump.find(diff[cur_no]) != ump.end()) {
        int to = ump[diff[cur_no]];
        if (to >= cur_no) {
          nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
        } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
          nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
        }
      }
    }
  }

  for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
    nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
    nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
  }

  for (int i = 0; i < 17; i++) {
    for (int j = 0; j < n; j++) {
      int to = nxt[j][i].first;
      pii rl = nxt[j][i].second;
      nxt[j][i + 1] = mpr(nxt[to][i].first,
                          mpr(nxt[to][i].second.first + rl.first,
                              nxt[to][i].second.second + rl.second));
    }
  }

  pii tans = mpr(n, -1);
  for (int i = 0; i < n; i++) {
    pii ans = mpr(0, 0);
    int last = i;
    for (int j = 17; j >= 0; j--) {
      if (nxt[last][j].second.second + ans.second < n) {
        ans.first += nxt[last][j].second.first;
        ans.second += nxt[last][j].second.second;
        last = nxt[last][j].first;
      }
    }
    int rans = ans.first;
    if (nxt[last][0].second.second != 1) {
      rans += max(1, (n - ans.second - 1 - 1) / (k - 1) + 1);
    }
    if (rans < tans.first) {
      tans = mpr(rans, i);
    }
  }

  printf("%d\n", tans.first);
  if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
    for (int i = 0; i < n; i++) {
      if (GetSum(i, (i + k - 1) % n) > avg * k) {
        tans.second = (i + k - 1) % n;
        break;
      }
    }
  }

  for (int i = tans.second, cnt = 0; cnt < n; ) {
    lst = 0, rst = 0;
    int delta = 0;
    int cur = i;
    int rcnt = 0;
    auto push = [&]() {
      st[rst++] = orig[cur];
      delta += avg - orig[cur];
      cur = (cur - 1 + n) % n;
      rcnt++;
    };
    push();
    for (; rcnt < nxt[i][0].second.second; ) {
      do {
        for (int t = 0; t < k - 1; t++) {
          push();
        }
      } while (delta > 0 && rcnt < nxt[i][0].second.second);
      if (rst > 1) {
        for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
          int sum1 = 0, sum2 = 0;
          for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
          printf("%d", p);
          printf(" %d", !have * -delta + avg);
          orig[p] = !have * -delta + avg;
          sum2 += !have * -delta + avg;
          for (int t = 1; t < k - 1; t++) {
            printf(" %d", avg);
            orig[(t + p) % n] = avg;
            sum2 += avg;
          }
          printf(" %d\n", sum1 - sum2);
          orig[(k - 1 + p) % n] = sum1 - sum2;
          rdelta = avg - sum1 + sum2;
          have = 1;
        }
        rst = lst = 0;
        st[rst++] = -delta + avg;
      }
    }
    i = nxt[i][0].first;
    cnt += nxt[i][0].second.second;
  }

  return 0;
}

#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = 200500, LOG = 20, inf = 1e9;

int n, k, a[N], up[LOG][N], cost[LOG][N];
int sum, A, pref[N];
pll ans;
map<int, int> lst[N];
int get(int i){
	i %= n;
	if(i <= ans.s)
		i += n;
	return a[i];
}
void gg(int l, int r, ll x, ll y){
	//cerr << l << " " << r << endl;
	a[l] = x, a[r] = y;
	for(int i = l + 1; i < r; i++)
		a[i] = 0;
	printf("%d ", l % n + 1);
	for(int i = l; i <= r; i++)
		printf("%d ", a[i] + A);
	printf("\n");
}
void ff(int l, int r, ll need){
	if(l == r && a[l] == need)
		return;
	if(r - l + 1 <= k){
		a[l] = need;
		for(int i = l + 1; i <= r; i++)
			a[i] = 0;
		printf("%d ", l % n);
		for(int i = l; i <= l + k - 1; i++)
			printf("%d ", get(i) + A);
	   	printf("\n");
		return;
	}
    ll s = 0;
	for(int i = l; i <= l + k - 1; i++)
		s += a[i];
	if(s < need){
		ff(l + k - 1, r, need - s + a[l + k - 1]);
		gg(l, l + k - 1, need, 0);
	}else{
		gg(l, l + k - 1, need, s - need);
		ff(l + k - 1, r, 0);
	}
}
int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		sum += a[i];
	}
	A = sum / n;
	for(int i = 0; i < n; i++)
		a[i + n] = (a[i] -= A);
	for(int i = 1; i < n + n; i++)
		pref[i] = pref[i - 1] + a[i];
	for(int i = 0; i < LOG; i++)
		for(int j = 0; j < N; j++)
			up[i][j] = n + n;
	for(int i = n + n - 1; i >= 0; i--){
		int rem = (i + 1) % (k - 1);
		if(lst[rem].count(pref[i]))
			up[0][i] = lst[rem][pref[i]], cost[0][i] = (lst[rem][pref[i]] - i + k - 3) / (k - 1);
		lst[i % (k - 1)][pref[i]] = i;
	}
	for(int j = 1; j < LOG; j++)
		for(int i = 0; i < n + n; i++)
            up[j][i] = up[j - 1][ up[j - 1][i] ], cost[j][i] = cost[j - 1][i] + cost[j - 1][ up[j - 1][i] ];
	ans = {inf, inf};
	for(int i = 0; i < n; i++){
		int v = i;
		int res = 0;
		for(int j = LOG - 1; j >= 0; j--)
			if(up[j][v] <= n + i)
				res += cost[j][v], v = up[j][v];
		if(v <= n + i)
			res += (n + i - v + k - 2) / (k - 1);
		ans = min(ans, {res, i});
	}
	printf("%d\n", ans.f);
	int v = ans.s;
	while(v < ans.s + n){
		if(up[0][v] < ans.s + n)
			ff(v + 1, up[0][v], 0);
		else
			ff(v + 1, ans.s + n, 0);
		v = up[0][v];
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{printf("%d %d\n",l,r);
	int d=num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,ave,d);
			while(top>=0){
				int g=sta[top]-k+1;
				int L=sum[l]-num[l]-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++){
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
14 3
0 2 0 1 2 0 2 0 38 0 1 0 3 21
*/

#include <iostream>
#include <vector>
#include <map>
#include <unordered_set>
#include <algorithm>

using namespace std;
typedef long long ll;

int n, k;
vector<int> a, asum;

vector<vector<pair<ll,ll>>> dp(100010,vector<pair<ll,ll>>(25));

void init() {
    vector<map<int,int>> asumeq(n);
    for (int i=2*n; i>=0; --i) {
        if (i<n) {
            if (asumeq[(i+1)%(k-1)].count(asum[i]) && asumeq[(i+1)%(k-1)][asum[i]] <= n+i) {
                int x = asumeq[(i+1)%(k-1)][asum[i]] - i;
                dp[i][0] = make_pair(x, (x-1)/(k-1));
            }
            else dp[i][0] = make_pair(n, (n+k-3)/(k-1));
            asumeq[i%(k-1)][asum[i]] = i;
        }
    }
}

unordered_set<int> moved;
unordered_set<int> moves;

bool canMove(int a, int s) {
	if (moved.count(a)) return false;
	int b = a + k;
    return (asum[a] - asum[s] <= 0 || asum[a+1] - asum[s] <= 0) && (asum[b] - asum[s] >= 0 || asum[b-1] - asum[s] >= 0);
};

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(4*n); asum.resize(4*n+1);
    int avg = 0;
    for (int i=0; i<n; ++i) {
        cin >> a[i];
        avg += a[i];
    }
    avg /= n;
    bool eq = true;
    for (int i=0; i<n; ++i) {
        a[i] -= avg;
        if (a[i]!=0) eq = false;
    }
    if (eq) {
        cout << 0 << endl;
        return 0;
    }
    for (int i=0; i<n; ++i) a[i+3*n] = a[i+2*n] = a[i+n] = a[i];
    asum[0] = 0;
    for (int i=0; i<4*n; ++i) asum[i+1] = asum[i]+a[i];

    init();
    int start = -1, ans = n+1;
    for (int i=0; i<n; ++i) {
        int curr = i, cost = 0;
        for (int j = 24; j >= 0; --j) {
			if (curr + dp[curr % n][j].first < i + n) {
				cost += int(dp[curr % n][j].second);
				curr += int(dp[curr % n][j].first);
			}
		}
		cost += (i+n - curr - 1 + (k-2)) / (k-1);
		if (cost < ans) {
			ans = cost;
			start = i;
		}
    }
    cout << ans << endl;

    for (int s=start; s<start+n; ) {
		int en = s + int(dp[s%n][0].first);
		en = min(en, start + n);
        while ((en-s-1) % (k-1) != 0) {
			asum[en+1] = asum[en];
			en++;
        }

        moved.clear();
        moves.clear();
		for (int x = s; x+k <= en; x += k-1) {
			if (canMove(x,s)) {
				moves.insert(x);
			}
		}
		while (!moves.empty()) {
			int x = *moves.begin();
			moves.erase(moves.begin());
			moved.insert(x);
			int y = x+k;
			for (int i = 1; i <= k-1; ++i) {
				asum[x+i] = asum[s];
			}

			cout << (x%n);
			for (int i = 0; i < k; ++i) {
				if (x+i < start + n) {
					a[(x+i)%n] = asum[x+i+1] - asum[x+i];
				}
				cout << " " << avg + a[(x+i)%n];
			}
			cout << endl;

			if (x > s) {
				if (canMove(x - (k-1),s)) {
					moves.insert(x - (k-1));
				} else {
					moves.erase(x - (k-1));
				}
			}
			if (y < en) {
				if (canMove(x + (k-1),s)) {
					moves.insert(x + (k-1));
				} else {
					moves.erase(x + (k-1));
				}
			}
		}

		s = en;
	}

    return 0;
}

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

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][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);
    }
    // [pos1, pos2)
    int distance(int pos1, int pos2)
    {
        if (pos1 < pos2) return pos2-pos1;
        else return n-pos1+pos2;
    }
    // [pos1, pos2)
    bool suitable(int pos1, int pos2)
    {
        return (distance(pos1, pos2)-1) % (k-1) == 0;
    }
    // [pos1, pos2)
    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) {
            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";
            // debug();
            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";
            // debug();
            output(L+k-1, R, 0);
        } else {
            output(L+k-1, R, avg*(k-1)-(sum-data[(L+k-1)%n])+extra);
            for(int i = L; i < L+k; i ++) {
                data[i%n] = avg;
            }
            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i%n];
            }
            cout << "\n";
            // debug();
        }
    }
    void solve()
    {
        cin >> n >> k;
        for(int i = 0; i < n; i ++) {
            cin >> data[i];
            avg += data[i];
        }
        avg /= n;
        // cout << "avg = " << avg << endl;

        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 ++) {
            // cout << bestplan[i].first << " " << bestplan[i].second << endl;
        // }
        // 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);
            }
        }
    }
}

int main()
{
    Solve::solve();
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=L-num[i-k+1];
			}
			else addans(i-k+1,ave,d);
			while(top>=0){
				int g=sta[top]-k+1;
				int L=sum[l]-num[l]-sum[g]+num[g]-ave*(k-2-sta[top]-l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&mod(i-Lst+1,k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
9 3
0 2 0 1 2 0 2 0 38
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos=0,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],x=sum[l]-num[l];
	for(int i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,ave,d);
			num[i]=d;
			while(top>=0){
				int g=sta[top]-k+1;
				int L=x-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++)
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int mod(int x,int p){return x>=p?x-p:x;}

int n,m=0,k,logn=0;
int ave=0,pos,cnt=0,o=0;
int num[200000],sum[200000];
int pre[200000],lst[200000];
int val[200000],id[200000];
int st[200000],ed[200000];
int t1[200000],t2[200000];
int sta[200000],top=-1;
int f[200000];
bool vis[200000],bepre[200000];
int ans=0,ts[100000];
int tl[100000],tr[100000];

struct Tree{
	int cnt;
	int fir[200000];
	int to[400000];
	int nxt[400000];
	int dep[200000];
	int fa[19][200000];
	inline void add(int a,int b)
	{
		to[++cnt]=b;
		nxt[cnt]=fir[a];
		fir[a]=cnt;
		return;
	}
	void dfs(int i)
	{
		for(int j=fir[i];j!=-1;j=nxt[j]){
			if(to[j]==fa[0][i])
				continue;
			fa[0][to[j]]=i;
			dep[to[j]]=dep[i]+1;
			dfs(to[j]);
		}
		for(int j=1;(1<<j)<n;j++)
			if(fa[j-1][i]==-1)
				fa[j][i]=-1;
			else fa[j][i]=fa[j-1][fa[j-1][i]];
		return;
	}
	inline int getans(int x)
	{
		int res=0,y=x;
		for(int i=logn;i>=0;i--){
			if(fa[i][y]!=-1&&id[fa[i][y]]+(n>>1)>=id[x])
				y=fa[i][y];
			}
		return dep[x]-dep[y]+1;
	}
}tree;

void push1(int x)
{
	if(x==-1) return;
	push1(pre[x]);
	t1[cnt]=sum[x]; t2[cnt++]=x;
	return;
}
void f1(void)
{
	for(int i=0;i<ave;i++)
		lst[i]=-1;
	for(int i=0;i<n;i++){
		pre[i]=lst[sum[i]%ave];
		lst[sum[i]%ave]=i;
	}
	for(int i=0;i<ave;i++){
		st[i]=cnt;
		push1(lst[i]);
		ed[i]=cnt-1;
	}
	return;
}

void push2(int x)
{
	if(x==-1) return;
	push2(pre[x]);
	val[cnt]=t1[x]; id[cnt++]=t2[x];
	return;
}
void f2(void)
{
	for(int i=0;i<n;i++)
		lst[i]=-1;
	for(int i=0;i<ave;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=t1[j]/ave-t2[j]+(n>>1);
			pre[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0){
			if(lst[sta[top]]!=-1){
				f[m++]=lst[sta[top]];
				lst[sta[top]]=-1;
			}
			top--;
		}
	}
	cnt=0;
	for(int i=0;i<m;i++){
		st[i]=cnt;
		push2(f[i]);
		ed[i]=cnt-1;
	}
	return;
}

void f3(void)
{
	for(int i=0;i<n;i++)
		tree.fa[0][i]=tree.fa[1][i]=tree.fir[i]=-1;
	for(int i=0;i<m;i++){
		for(int j=st[i];j<=ed[i];j++){
			int t=id[j]%(k-1);
			pre[j]=lst[mod(t-1+k-1,k-1)];
			if(pre[j]!=-1){
				tree.add(pre[j],j);
				tree.add(j,pre[j]);
				bepre[pre[j]]=1;
			}
			t1[j]=lst[t];
			lst[t]=j;
			sta[++top]=t;
		}
		while(top>=0)
			lst[sta[top--]]=-1;
	}
	for(int i=0;i<n;i++)
		if(tree.fa[0][i]==-1){
			tree.dep[i]=0;
			tree.dfs(i);
		}
	for(int i=0;i<n;i++){
		int t=tree.getans(i);
		if(o<t&&bepre[i]){
			o=t; pos=mod(id[i]+1,n>>1);
		}
	}
	return;
}

inline void addans(int s,int l,int r)
{
	ts[ans]=s; tl[ans]=l; tr[ans++]=r;
	sum[s]=(s==0?0:sum[s-1])+l;
	for(int i=1;i<k-1;i++)
		sum[i+s]=sum[i-1+s]+ave;
	sum[s+k-1]=sum[s+k-2]+r;
	return;
}
void slv(int l,int r)
{
	int d=num[l],i;
	for(i=l+k-1;i<=r;i+=k-1){
		int t=sum[i]-sum[i-k+1]-(k-1)*ave;
		d+=t;
		if(d>=0){
			if(top>=0){
				int L=t-d+ave+num[i-k+1];
				addans(i-k+1,L,d);
				sum[i-k+1]+=t-d+ave;
			}
			else addans(i-k+1,ave,d);
			while(top>=0){
				int g=sta[top]-k+1;
				int L=sum[l]-num[l]-sum[g]+num[g]-ave*(k-2-sta[top]+l);
				addans(g,L,ave);
				sum[g]+=L-num[g];
				top--;
			}
		}
		else sta[++top]=i;
	}
	return;
}
void solve(int l,int r)
{
	int Lst=l,p=(k==2?0:1);
	for(int i=l;i<r;i++){
		if(sum[i]-sum[Lst]+num[Lst]==ave*(i-Lst+1)&&(i-Lst+1)%(k-1)==p){
			slv(Lst,i);
			Lst=i+1;
		}}
	slv(Lst,r);
	if((r-Lst+1)%(k-1)!=p)
		addans(r-k+1,ave,ave);
	printf("%d\n",ans);
	for(int i=0;i<ans;i++){
		printf("%d %d ",ts[i]%(n>>1),tl[i]);
		for(int j=2;j<k;j++)
			printf("%d ",ave);
		printf("%d\n",tr[i]);
	}
	return;
}

int main(void)
{
	scanf("%d%d",&n,&k);
	while((1<<logn)<n)
		logn++;
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
		ave+=num[i];
		num[n+i]=num[i];
	}
	ave/=n; n<<=1; sum[0]=num[0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+num[i];
	f1(); f2(); f3();
	solve(pos,pos+(n>>1)-1);
	return 0;
}
/*
14 3
0 2 0 1 2 0 2 0 38 0 1 0 3 21
*/

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		LL hh = -s[x - 1]; a[x] = 0;
		for(reg i = x; i >= n - k + 1; i--) {
			if(hh + V < 0) a[i] += -V, hh += V;
			else {a[i] += hh; break;}
		} for(reg i = n - k + 1; i <= x; i++) ans[tp].push_back(V + a[i]);
		ans[tp].push_back(hh + V), a[x] = hh, s[x] = 0;
		for(reg i = x + 1; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<ll,ll> Pll;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> T gcd(T a, T b){return !b?a:gcd(b,a%b);}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 2e5+10;
int n,k,a[N],step,pos,buc[N],nxt[N],dp[N];ll ave,qz[N];Pll b[N];vector<Vi>ans;
int calc(int n){return n<=k?1:(n+k-3)/(k-1);}
void solve(int l, int r){
	rep(i,l,r)dp[i]=1,nxt[i]=n+1;
	per(i,r-1,l){
		if(buc[(b[i].se+1)%(k-1)]<n)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
		buc[b[i+1].se%(k-1)]=i+1;
		if(b[i+1].se%(k-1)==(b[i].se+1)%(k-1)&&b[i+1].se>b[i].se+1)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	}
	int p=r;rep(i,l,r)if(dp[i]>dp[p])p=i;if(dp[p]<n%(k-1))return;
	int tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
	if((n-tot)/(k-1)<step)step=(n-tot)/(k-1),pos=p;
	rep(i,l,r)buc[b[i].se%(k-1)]=n+1;
}
void perform(int l, int r, int x, int y){
	Vi q={l%n};assert(r>l);
	rep(i,0,k-1){
		int j=(l+i)%n;
		a[j]=!i?x:(i>r-l?a[j]:(i==r-l?y:0)),q.pb(ave+a[j]);
	}
	ans.pb(q);
}
void getans(int l, int r, int x=0, int y=0){
	if(r-l+1<=k){perform(l,r,x,y);return;}
	if(qz[l+k-1]-qz[l-1]>=x){
		perform(l,l+k-1,x,qz[l+k-1]-qz[l-1]-x);getans(l+k-1,r,0,y);return;
	}
	getans(l+k-1,r,qz[r]-qz[l+k-2]-y,y);perform(l,l+k-1,x,0);
}
int main() {
	read(n);read(k);
	rep(i,0,n-1)read(a[i]),ave+=a[i];ave/=n;
	rep(i,0,n-1)a[i]-=ave;
	step=calc(n);
	rep(i,0,n-1)qz[i]=(i-1>=0?qz[i-1]:0)+a[i];
	rep(i,0,n-1)b[i]=mp(qz[i],i);sort(b,b+n);
	rep(i,0,n)buc[i]=n+1;
	rep(l,0,n-1){
		int r=l;while(r+1<n&&b[r+1].fi==b[l].fi)r++;
		solve(l,r);l=r;
	}
//	printf("%d %d\n",step,pos);
	if(!pos)getans(0,n-1);
	else{
		int p=pos,tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
		int q=p;rep(t,1,tot-1)getans(b[q].se+1,b[nxt[q]].se),q=nxt[q];
		rep(i,0,n-1)a[n+i]=a[i],qz[n+i]=qz[i];
		getans(b[q].se+1,b[p].se+n);
	}
	printf("%d\n",SZ(ans));
	for(auto b:ans){for(auto c:b)printf("%d ",c);puts("");}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 100005;

int n, k;
int A = 0;
int a[2 * MX];
int need[2 * MX];

void Print(int L) {
	printf("%d ", (L - 1) % n);
	for (int i = L; i < L + k; i++) printf("%d ", a[i]);
	printf("\n");
}
void Set(int i, int x) {
	if (i <= n) {
		a[i] = a[i + n] = x;
	}
	else {
		a[i] = a[i - n] = x;
	}
}
void SetNeed(int i, int x) {
	if (i <= n) {
		need[i] = need[i + n] = x;
	}
	else {
		need[i] = need[i - n] = x;
	}
}
void Do(int L, int R)
{
	if (L > n) L -= n, R -= n;

	//printf("[%d %d]\n", L, R);

	if (R - L + 1 <= k) {
		bool ok = true;
		for (int i = L; i <= R; i++) {
			if (a[i] != need[i]) ok = false;
			Set(i, need[i]);
		}
		if (ok) return;

		Print(L);
		return;
	}

	int s = 0;
	for (int i = L; i < L + k; i++) s += a[i];
	int x = 0;
	for (int i = L; i < L + (k - 1); i++) x += need[i];

	if (s >= x) {
		for (int i = L; i < L + (k - 1); i++) {
			Set(i, need[i]);
		}
		Set(L + k - 1, s - x);
		Print(L);
		Do(L + k - 1, R);
	}
	else {
		SetNeed(L + k - 1, need[L + k - 1] + x - s);
		Do(L + k - 1, R);
		for (int i = L; i < L + (k - 1); i++) {
			Set(i, A);
		}
		Print(L);
	}
}

int p[MX];

map<int, vector<int>> M;

int up[2 * MX][20];
int last[2 * MX];

int main()
{
	memset(up, -1, sizeof up);
	memset(last, -1, sizeof last);

	ignore = scanf("%d %d", &n, &k);

	for (int i = 1; i <= n; i++) {
		ignore = scanf("%d", a + i);
		A += a[i];
	}
	A /= n;
	for (int i = 1; i <= n; i++) {
		p[i] = p[i - 1] + a[i] - A;
		a[i + n] = a[i];
		p[i + n] = p[i];
		M[p[i]].push_back(i);
	}

	int ans = n;
	int from = 1;

	for (auto& P : M) {
		vector<int>& v = P.second;
		//printf("v = { "); for (int i : v) printf("%d ", i); printf("}\n");

		int m = (int)v.size();
		for (int i = 0; i < m; i++) v.push_back(v[i] + n);

		for (int i = (int)v.size() - 1; i >= 0; i--) {
			int t = v[i];
			up[t][0] = last[(t + 1) % (k - 1)];
			for (int j = 0; j + 1 < 20 && up[t][j] != -1; j++) {
				up[t][j + 1] = up[up[t][j]][j];
			}
			last[t % (k - 1)] = t;
		}

		for (int i = 0; i < m; i++) {
			int cnt = 0;
			int t = v[i];
			for (int j = 19; j >= 0; j--) {
				if (up[t][j] == -1) continue;
				if (up[t][j] >= v[i] + n) continue;
				cnt += (up[t][j] - t - (1 << j)) / (k - 1);
				t = up[t][j];
			}
			//printf("v[i] = %d, t = %d\n", v[i], t);
			cnt += (v[i] + n - t + (k - 2)) / (k - 1);
			if (cnt <= ans) {
				ans = cnt;
				from = v[i];
			}
		}

		for (int i = 0; i < (int)v.size(); i++) {
			for (int j = 0; j < 20; j++) up[v[i]][j] = -1;
			last[v[i] % (k - 1)] = -1;
		}
	}

	//printf("from=%d, ans=%d\n", from, ans);
	printf("%d\n", ans);
	//for (int i = 1; i <= 2 * n; i++) printf("%d ", p[i]);
	//printf("\n");

	for (int i = 1; i <= 2 * n; i++) need[i] = A;
	for (int i = from + 1, pr = from; i <= from + n; i++) {
		if (p[i] == p[pr] && (i == from + n || (i - pr) % (k - 1) == 1)) {
			//printf("[%d %d]\n", pr + 1, i);
			Do(pr + 1, i);
			pr = i;
		}
	}
}

//#define USEPB_DS
#define USETR1
#define CPPELEVEN
#define GPP

/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")

#include <math.h>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

#ifndef CPPELEVEN
#ifdef USETR1
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#endif
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef USEPB_DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
// binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
// splay_tree_tag, ov_tree_tag
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    tree_type;
#endif

#define mpr make_pair
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef map<string, int> mpsi;
typedef map<double, int> mpdi;
typedef map<int, int> mpii;

const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                        {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

template <class T>
inline T abs1(T a) {
  return a < 0 ? -a : a;
}

#ifndef CPPELEVEN
template <class T>
inline T max1(T a, T b) {
  return b < a ? a : b;
}
template <class T>
inline T max1(T a, T b, T c) {
  return max1(max1(a, b), c);
}
template <class T>
inline T max1(T a, T b, T c, T d) {
  return max1(max1(a, b, c), d);
}
template <class T>
inline T max1(T a, T b, T c, T d, T e) {
  return max1(max1(a, b, c, d), e);
}
template <class T>
inline T min1(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline T min1(T a, T b, T c) {
  return min1(min1(a, b), c);
}
template <class T>
inline T min1(T a, T b, T c, T d) {
  return min1(min1(a, b, c), d);
}
template <class T>
inline T min1(T a, T b, T c, T d, T e) {
  return min1(min1(a, b, c, d), e);
}
#else
template <typename t, typename t1>
t min1(t a, t1 b) {
  return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
  return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
  return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
  return max1(a, max1(arr...));
}
#endif

inline int jud(double a, double b) {
  if (abs(a) < eps && abs(b) < eps)
    return 0;
  else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
    return 0;
  if (a < b) return -1;
  return 1;
}
template <typename t>
inline int jud(t a, t b) {
  if (a < b) return -1;
  if (a == b) return 0;
  return 1;
}

template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
  if (na == 0) return 0;
  int be = 0, en = na - 1;
  if (*a <= *(a + na - 1)) {
    if (f_lb == 0)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != 1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != -1)
          en = mid;
        else
          be = mid + 1;
      }
    if (f_small && jud(*(a + be), val) == 1) be--;
    if (!f_small && jud(*(a + be), val) == -1) be++;
  } else {
    if (f_lb)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != -1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != 1)
          en = mid;
        else
          be = mid + 1;
      }
    if (!f_small && jud(*(a + be), val) == -1) be--;
    if (f_small && jud(*(a + be), val) == 1) be++;
  }
  return be;
}

template <class T>
inline T lowb(T num) {
  return num & (-num);
}
#ifdef GPP
inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(ull nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(ll nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(ui a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(ull a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
inline int bitmaxl(ll a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
#else
#endif

long long pow(long long n, long long m, long long mod = 0) {
  if (m < 0) return 0;
  long long ans = 1;
  long long k = n;
  while (m) {
    if (m & 1) {
      ans *= k;
      if (mod) ans %= mod;
    }
    k *= k;
    if (mod) k %= mod;
    m >>= 1;
  }
  return ans;
}

#define MOD 1000000007
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
  if (mod == -1) mod = MOD;
  a += b;
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
  cerr << endl;
}
template <class t>
void output2(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}

//...................................................................

const int maxn = 200100;
int orig[maxn], sum[maxn], diff[maxn], avg;
int st[maxn], lst, rst;
pair<int, pii> nxt[maxn][20];
int n, k;
unordered_map<int, int> ump;

int GetSum(int a, int b) {
  if (a > b) {
    return sum[n - 1] - sum[a - 1] + sum[b];
  } else {
    int ans = sum[b];
    if (a) ans -= sum[a - 1];
    return ans;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
#ifdef DEBUG  //..................................................
  freopen("input.txt", "r", stdin);
#endif  //........................................................
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", orig + i);
    sum[i] = orig[i];
    if (i) sum[i] += sum[i - 1];
  }
  avg = sum[n - 1] / n;
  for (int i = 0; i < n; i++) {
    diff[i] = sum[i] - (i + 1) * avg;
  }

  for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
  for (int i = 0; i < k - 1; i++) {
    ump.clear();
    for (int j = i; j < n * 2; j += k - 1) {
      int last_no = (j - 1 + n) % n, cur_no = j % n;
      ump[diff[last_no]] = last_no;
      if (ump.find(diff[cur_no]) != ump.end()) {
        int to = ump[diff[cur_no]];
        if (to >= cur_no) {
          nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
        } else if ((cur_no - to) % (k - 1) == 1) {
          nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
        }
      }
    }
  }

  for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
    nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
    nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
  }

  for (int i = 0; i < 17; i++) {
    for (int j = 0; j < n; j++) {
      int to = nxt[j][i].first;
      pii rl = nxt[j][i].second;
      nxt[j][i + 1] = mpr(nxt[to][i].first,
                          mpr(nxt[to][i].second.first + rl.first,
                              nxt[to][i].second.second + rl.second));
    }
  }

  pii tans = mpr(n, -1);
  for (int i = 0; i < n; i++) {
    pii ans = mpr(0, 0);
    int last = i;
    for (int j = 17; j >= 0; j--) {
      if (nxt[last][j].second.second + ans.second < n) {
        ans.first += nxt[last][j].second.first;
        ans.second += nxt[last][j].second.second;
        last = nxt[last][j].first;
      }
    }
    int rans = ans.first;
    if (nxt[last][0].second.second != 1) {
      rans += max(1, (n - ans.second - 1 - 1) / (k - 1) + 1);
    }
    if (rans < tans.first) {
      tans = mpr(rans, i);
    }
  }

  printf("%d\n", tans.first);
  if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
    for (int i = 0; i < n; i++) {
      if (GetSum(i, (i + k - 1) % n) > avg * k) {
        tans.second = (i + k - 1) % n;
        break;
      }
    }
  }

  for (int i = tans.second, cnt = 0; cnt < n; ) {
    lst = 0, rst = 0;
    int delta = 0;
    int cur = i;
    int rcnt = 0;
    auto push = [&]() {
      st[rst++] = orig[cur];
      delta += avg - orig[cur];
      cur = (cur - 1 + n) % n;
      rcnt++;
    };
    push();
    for (; rcnt < nxt[i][0].second.second; ) {
      do {
        for (int t = 0; t < k - 1; t++) {
          push();
        }
      } while (delta > 0 && rcnt < nxt[i][0].second.second);
      if (rst > 1) {
        for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
          int sum1 = 0, sum2 = 0;
          for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
          printf("%d", p);
          printf(" %d", !have * -delta + avg);
          orig[p] = !have * -delta + avg;
          sum2 += !have * -delta + avg;
          for (int t = 1; t < k - 1; t++) {
            printf(" %d", avg);
            orig[(t + p) % n] = avg;
            sum2 += avg;
          }
          printf(" %d\n", sum1 - sum2);
          orig[(k - 1 + p) % n] = sum1 - sum2;
          rdelta = avg - sum1 + sum2;
          have = 1;
        }
        rst = lst = 0;
        st[rst++] = -delta + avg;
      }
    }
    i = nxt[i][0].first;
    cnt += nxt[i][0].second.second;
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 200000, W = sqrt(N);
int x[N], y[N], nxt[N], n, k, ano, need;
vector<int> mp, in[N], an, can;

vector<int> g[N];
int inc[N];
bool vis[N];

void upan(int w) { if (can.size() < an.size())an = can, ano = w; }

void upb(int w, int st){
	can.clear();
	int j = st;
	st += n >> 1;
	while (j < st){
		if (x[j] == w)j = nxt[j];
		can.push_back(j);
		j += k;
	}
	upan(w);
}

void ups(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st;
	st = j + n / 2;
	while (j < st){
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
		can.push_back(j);
		j += k;
	}
	upan(w);
}

void gob(int w){
	f(i, 0, k)upb(w, i);
}

void gos(int w){
	f(i, 0, in[w].size() >> 1)ups(w, i);
}

void go(int w){
	if ((int)in[w].size() >> 1 > w)gob(w);
	else gos(w);
}

void fn(){
	n >>= 1;
	++k;
	for (int u: an)for (int z: g[u])++inc[z];
	stack<int> q;
	for (int u: an)if (inc[u] == 0)q.push(u);
	while (!q.empty()){
		int v = q.top();
		q.pop();
		for (int u: g[v])if (--inc[u] == 0)q.push(u);
		if (v >= n)v -= n;
		printf("%d", v);

		if (!vis[v]){
			int t = v ? v - 1 : n - 1;
			y[v] = need - x[t];
		}

		int t = v + k - 1;
		if (t >= n)t -= n;
		if (!vis[t])y[t] = need + x[t];

		f(i, v, v + k){
			int j = i >= n ? i - n : i;
			if (vis[j] || (i != v && i + 1 != v + k))y[j] = need;
			vis[j] = true;
		}

		f(i, 0, k)printf(" %d", y[i + v >= n ? i + v - n : i + v]);
		printf("\n");
	}
}

int main(){
	scanf("%d%d", &n, &k);
	--k;
	f(i, 0, n)scanf("%d", y + i);
	f(i, 0, n)need += y[i];
	need /= n;
	int car = 0;
	f(i, 0, n){
		x[i] = y[i] + car - need;
		car = x[i];
	}
	mp.resize(n);
	f(i, 0, n)mp[i] = x[i];
	sort(mp.begin(), mp.end());
	mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
	if (mp.size() == 1){
		printf("0\n");
		return 0;
	}
	f(i, 0, n)x[i] = lower_bound(mp.begin(), mp.end(), x[i]) - mp.begin();
	f(i, 0, n)x[i + n] = x[i];
	n <<= 1;
	f(i, 0, n)in[x[i]].push_back(i);
	nxt[n - 1] = n;
	for (int i = n - 2; i >= 0; --i)if (x[i] == x[i + 1])nxt[i] = nxt[i + 1]; else nxt[i] = i + 1;

	an.resize(n);
	f(i, 0, mp.size())go(i);

	ano = mp[ano];
	f(i, 0, n)x[i] = mp[x[i]] - ano;
	f(i, 1, an.size())if (an[i] == an[i - 1] + k){
		if (x[an[i] - 1] > 0 && x[an[i]] > 0)g[an[i - 1]].push_back(an[i]);
		else if (x[an[i]] < 0 && x[an[i] - 1] < 0)g[an[i]].push_back(an[i - 1]);
	}
	int t = an[0] + n / 2;
	if (t == an.back() + k){
		if (x[t - 1] > 0 && x[t] > 0)g[an.back()].push_back(an[0]);
		else if (x[t] < 0 && x[t - 1] < 0)g[an[0]].push_back(an.back());
	}

	printf("%d\n", (int)an.size());
	fn();
}

#include <bits/stdc++.h>
using namespace std;

#define N 200200

int n, K, a[N], b[N];

map <int, int> mp;

int tot, s;

map <int, int> lst[N];

int nxt[N][20];

void solve(int l, int r, int rem = 0) {
    if (l + K > r) {
        int sum = 0;
        for (int i = l; i <= r; i ++) sum += b[i] - s, b[i] = s;
        b[l] += rem, b[r] += sum - rem;
        printf("%d", (l - 1) % n);
        for (int i = l; i < l + K; i ++) printf(" %d", b[i]);
        puts(""); return;
    }
    int sum = 0;
    for (int i = l; i < l + K; i ++) sum += b[i] - s;
    if (sum >= rem) {
        solve(l, l + K - 1, rem);
        solve(l + K - 1, r, 0);
    }
    else {
        sum -= b[l+K-1] - s;
        solve(l + K - 1, r, rem - sum);
        solve(l, l + K - 1, rem);
    }
}

typedef pair <int, int> pii;

int main() {
    scanf("%d %d", &n, &K);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[n+i] = a[i], s += a[i];
    for (int i = 1; i <= 2 * n; i ++) b[i] = a[i];
    s /= n;
    for (int i = 1; i <= 2 * n; i ++) a[i] = a[i] - s + a[i-1];
    for (int i = 2 * n; i; i --) {
        int &k = mp[a[i]];
        if (!k) k = ++tot;
        nxt[i][0] = lst[k][(i+1)%(K-1)];
        for (int j = 0; nxt[i][j]; j ++) nxt[i][j+1] = nxt[nxt[i][j]][j];
        lst[k][i%(K-1)] = i;
    }
    pii ans = pii(1e9, 0); int r = n % (K - 1);
    for (int i = 1; i <= n; i ++) {
        int k = 0, u = i;
        for (int j = 19; ~j; j --) {
            int v = nxt[u][j];
            if (v && v <= n + i) u = v, k ^= 1 << j;
        }
        int qr = k % (K - 1);
        if (qr >= r) k -= qr - r;
        else k -= qr - r + K - 1;
        ans = min(ans, pii((n - k) / (K - 1), i));
    }
    if (ans.first >= (n - 3 + K) / (K - 1)) printf("%d\n", (n - 3 + K) / (K - 1)), solve(1, n);
    else {
        printf("%d\n", ans.first);
        int u = ans.second, k = n - (K - 1) * ans.first;
        while (-- k) {
            solve(u + 1, nxt[u][0]);
            u = nxt[u][0]; assert(u > 0);
        }
        if (u + 1 < n + ans.second) solve(u + 1, n + ans.second);
    }

    return 0;
}

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      for (int i = tans.second, cnt = 0; cnt < n; ) {
    	int brans = ans.size();
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        if (tans.first == 2408) {
        	rcnt = i - nxt[i][0].first;
        	if (rcnt < 0) rcnt += n;
        	if (rcnt != nxt[i][0].second.second) {
        		printf("420: %d %d %d\n", i, nxt[i][0].second.second, rcnt);
        	}
        }
        i = nxt[i][0].first;
        cnt += nxt[i][0].second.second;
      }

      if (ans.size() != tans.first) {
    	  printf("426: %d\n", ans.size());
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  if (!(a >= 0)) {
    			  printf("431: %d\n", a);
    		  }
    	  }
      }
      for (int i = 0; i < n; i++) {
    	  if (orig[i] != avg) {
    		  printf("437: %d %d\n", i, orig[i]);
    	  }
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		LL hh = -s[n - k];
		if(hh < 0) ans[tp].push_back(V), a[n - k + 1] = s[n - k + 1] = 0;
		else ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute(int l, int len, int need){
    //cout << "dis " << l << " " << len << " " << need << endl;
    //for(int i = 0; i < len; i += 1) cout << a[(l + i) % n] << " ";
    //cout << endl;
    if(len <= 1) return;
    if(a[l] == avg and need == 0){
        distribute((l + 1) % n, len - 1, 0);
        return;
    }
    int sum = 0, m = min(len, k);
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    if(sum >= avg * (m - 1) + need){
        sum -= a[l] = avg + need;
        for(int i = 1; i < m - 1; i += 1) sum -= a[(l + i) % n] = avg;
        a[(l + m - 1) % n] = sum;
        cout << l;
        for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
        cout << "\n";
        distribute((l + m - 1) % n, len - m + 1, 0);
        return;
    }
    distribute((l + m - 1) % n, len - m + 1, avg * (m - 1) + need - sum + a[(l + m - 1)]);
    sum = 0;
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    for(int i = 1; i < m; i += 1) sum -= a[(l + i) % n] = avg;
    a[l] = sum;
    cout << l;
    for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
    cout << "\n";
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        if(c < k - 1) continue;
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        vector<int> pos(k - 1, c * 2);
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; i; i -= 1){
            next[i][0] = pos[(v[i] + 1) % (k - 1)];
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1) if(v[i] % (k - 1) == 0){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        //cout << "cut" << " " << cut[i] << endl;
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        if(r <= l) r += n;
        interval.push_back({l, r - l});
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute(p.first, p.second, 0);
    for(int i = 0; i < n; i += 1) if(a[i] != avg) cout << i << " " << a[i] << endl;
    return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

const int MN = 1e5 + 100;

int l2(int x)
{
	return 31 - __builtin_clz(x);
}
int N, K, A, a[MN], b[MN], p[MN*2], x[MN], X, l[MN*2], q[MN*2][20], F, C;
std::vector<int> v[MN];

int md(int x) {
	if(x<N) return x;
	if((x-=N)<N) return x;
	return x%N;
}
void solve(int p, int e = 0)
{
	int s = 0, u = 0;
	for(int i = 0;i < K-1;++i)
		if(!b[md(p+i)])
			s += a[md(p+i)], b[md(p+i)] = p, ++u;
	s += a[md(p+K-1)];
	if((s -= A*u + e) < 0)
		solve(p+K-1, -(s-=a[md(p+K-1)])), s += a[md(p+K-1)];
	printf("%d ", p%N);
	printf("%d ", a[md(p)] = A+e);
	for(int i = 1;i < K-1;++i)
		printf("%d ", b[md(p+i)]==p?a[md(p+i)] = A:a[md(p+i)]);
	printf("%d\n", a[md(p+K-1)] = s);
}

int main(void)
{
	memset(l, -1, sizeof(l));
	memset(q, -1, sizeof(q));
	scanf("%d%d", &N, &K);
	C = N;
	for(int i = 0;i < N;++i) scanf("%d", a+i), A+=a[i];
	A/=N;
	for(int i = 0;i < N;++i) p[i+1] = p[i]+a[i]-A, x[i] = p[i+1];
	std::sort(x, x+N);
	X = std::unique(x, x+N)-x;
	for(int i = 0;i < N;++i)
		p[i] = std::lower_bound(x, x+X, p[i])-x, v[p[i]].push_back(i);//cut from i-1 -> i
	for(int y = 0, V;y < X;++y)
	{
		std::vector<int> &v = ::v[y];
		V = v.size();
		for(int i = 0;i < V;++i) v.push_back(v[i]+N);
		for(int i = v.size()-1;i >= 0;--i)
		{
			q[i][0] = l[(v[i]+1)%(K-1)];
			for(int j = 0;~q[i][j]&&~q[q[i][j]][j];++j)
				q[i][j+1] = q[q[i][j]][j];
			l[v[i]%(K-1)] = i;
		}
		for(int i = 0, n, c;i < V;++i)
		{
			n = i, c = 0;
			for(int j = l2(V), k;j >= 0;--j)
				if(~q[n][j] && (k=q[n][j])<i+V)
					c += (v[k]-v[n]-(1<<j))/(K-1), n = k;
			c += (v[i]+N-v[n]+K-3)/(K-1);
			if(c < C || !(C < c) && v[i] < F)
				C = c, F = v[i];
		}
		for(int i = 0;i < v.size();++i)
			for(int j = 0;~q[i][j];++j)
				q[i][j] = -1;
		for(int x : v)
			l[x%(K-1)] = -1;
	}

	F = 4;
	printf("%d\n", C);
	for(int i = F;i < F+N;++i)
		if(a[md(i)] != A)
			solve(i);
	return 0;
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<ll,ll> Pll;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> T gcd(T a, T b){return !b?a:gcd(b,a%b);}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 2e5+10;
int n,k,a[N],step,pos=-1,buc[N],nxt[N],dp[N];ll ave,qz[N];Pll b[N];vector<Vi>ans;
int calc(int n){return n<=k?1:(n+k-3)/(k-1);}
void solve(int l, int r){
	rep(i,l,r)dp[i]=1,nxt[i]=n+1;
	per(i,r-1,l){
		buc[b[i+1].se%(k-1)]=i+1;
		if(buc[(b[i].se+1)%(k-1)]<n)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	//	if(b[i+1].se%(k-1)==(b[i].se+1)%(k-1)&&b[i+1].se>b[i].se+1)nxt[i]=buc[(b[i].se+1)%(k-1)],dp[i]=dp[nxt[i]]+1;
	}
	int p=r;rep(i,l,r)if(dp[i]>dp[p])p=i;if(dp[p]<n%(k-1))return;
	int tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
	if((n-tot)/(k-1)<step)step=(n-tot)/(k-1),pos=p;
	rep(i,l,r)buc[b[i].se%(k-1)]=n+1;
}
void perform(int l, int r, int x, int y){
	Vi q={l%n};assert(r>l);
	rep(i,0,k-1){
		int j=(l+i)%n;
		a[j]=!i?x:(i>r-l?a[j]:(i==r-l?y:0)),q.pb(ave+a[j]);
	}
	ans.pb(q);
}
void getans(int l, int r, int x=0, int y=0){
	if(l==r)return;
	if(r-l+1<=k){perform(l,r,x,y);return;}
	if(qz[l+k-1]-qz[l-1]>=x){
		perform(l,l+k-1,x,qz[l+k-1]-qz[l-1]-x);getans(l+k-1,r,0,y);return;
	}
	getans(l+k-1,r,qz[r]-qz[l+k-2]-y,y);perform(l,l+k-1,x,0);
}
int main() {
	read(n);read(k);
	rep(i,0,n-1)read(a[i]),ave+=a[i];ave/=n;
	rep(i,0,n-1)a[i]-=ave;
	step=calc(n);
	rep(i,0,n-1)qz[i]=(i-1>=0?qz[i-1]:0)+a[i];
	rep(i,0,n-1)b[i]=mp(qz[i],i);sort(b,b+n);
	rep(i,0,n)buc[i]=n+1;
	rep(l,0,n-1){
		int r=l;while(r+1<n&&b[r+1].fi==b[l].fi)r++;
		solve(l,r);l=r;
	}
//	printf("%d %d\n",step,pos);
	if(pos==-1)getans(0,n-1);//WA
	else{
		int p=pos,tot=(dp[p]-n%(k-1))/(k-1)*(k-1)+n%(k-1);
		int q=p;rep(t,1,tot-1)getans(b[q].se+1,b[nxt[q]].se),q=nxt[q];
		rep(i,0,n-1)a[n+i]=a[i],qz[n+i]=qz[i];
		getans(b[q].se+1,b[p].se+n);
	}
	printf("%d\n",SZ(ans));
	for(auto b:ans){for(auto c:b)printf("%d ",c);puts("");}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 200000, W = sqrt(N);
int x[N], y[N], nxt[N], n, k, ano, need;
vector<int> mp, in[N], an, can;

vector<int> g[N];
int inc[N];
bool vis[N];

void upan(int w) { if (can.size() < an.size())an = can, ano = w; }

void upb(int w, int st){
	can.clear();
	int j = st;
	st += n >> 1;
	while (j < st){
		can.push_back(j);
		j += k;
		if (x[j] == w)j = nxt[j];
	}
	upan(w);
}

void ups(int w, int st){
	can.clear();
	int j = in[w][st] + 1, i = st;
	st = j + n / 2;
	while (j < st){
		can.push_back(j);
		j += k;
		while (i < (int)in[w].size() && j >= in[w][i]){
			if (j == in[w][i])++j;
			++i;
		}
	}
	upan(w);
}

void gob(int w){
	f(i, 0, k)upb(w, i);
}

void gos(int w){
	f(i, 0, in[w].size() >> 1)ups(w, i);
}

void go(int w){
	if ((int)in[w].size() >> 1 > w)gob(w);
	else gos(w);
}

void fn(){
	n >>= 1;
	++k;
	for (int u: an)for (int z: g[u])++inc[z];
	stack<int> q;
	for (int u: an)if (inc[u] == 0)q.push(u);
	while (!q.empty()){
		int v = q.top();
		q.pop();
		for (int u: g[v])if (--inc[u] == 0)q.push(u);
		if (v >= n)v -= n;
		printf("%d", v);

		if (!vis[v]){
			int t = v ? v - 1 : n - 1;
			y[v] = need - x[t];
		}

		int t = v + k - 1;
		if (t >= n)t -= n;
		if (!vis[t])y[t] = need + x[t];

		f(i, v, v + k){
			int j = i >= n ? i - n : i;
			if (vis[j] || (i != v && i + 1 != v + k))y[j] = need;
			vis[j] = true;
		}

		f(i, 0, k)printf(" %d", y[i + v >= n ? i + v - n : i + v]);
		printf("\n");
	}
}

int main(){
	scanf("%d%d", &n, &k);
	--k;
	f(i, 0, n)scanf("%d", y + i);
	f(i, 0, n)need += y[i];
	need /= n;
	int car = 0;
	f(i, 0, n){
		x[i] = y[i] + car - need;
		car = x[i];
	}
	mp.resize(n);
	f(i, 0, n)mp[i] = x[i];
	sort(mp.begin(), mp.end());
	mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
	if (mp.size() == 1){
		printf("0\n");
		return 0;
	}
	f(i, 0, n)x[i] = lower_bound(mp.begin(), mp.end(), x[i]) - mp.begin();
	f(i, 0, n)x[i + n] = x[i];
	n <<= 1;
	f(i, 0, n)in[x[i]].push_back(i);
	nxt[n - 1] = n;
	for (int i = n - 2; i >= 0; --i)if (x[i] == x[i + 1])nxt[i] = nxt[i + 1]; else nxt[i] = i + 1;

	an.resize(n);
	f(i, 0, mp.size())go(i);

	ano = mp[ano];
	f(i, 0, n)x[i] = mp[x[i]] - ano;
	f(i, 1, an.size())if (an[i] == an[i - 1] + k){
		if (x[an[i] - 1] > 0 && x[an[i]] > 0)g[an[i - 1]].push_back(an[i]);
		else if (x[an[i]] < 0 && x[an[i] - 1] < 0)g[an[i]].push_back(an[i - 1]);
	}
	int t = an[0] + n / 2;
	if (t == an.back() + k){
		if (x[t - 1] > 0 && x[t] > 0)g[an.back()].push_back(an[0]);
		else if (x[t] < 0 && x[t - 1] < 0)g[an[0]].push_back(an.back());
	}

	printf("%d\n", (int)an.size());
	fn();
}

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

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][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);
    }
    // [pos1, pos2)
    int distance(int pos1, int pos2)
    {
        if (pos1 < pos2) return pos2-pos1;
        else return n-pos1+pos2;
    }
    // [pos1, pos2)
    bool suitable(int pos1, int pos2)
    {
        return (distance(pos1, pos2)-1) % (k-1) == 0;
    }
    // [pos1, pos2)
    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) {
            for(int i = L; i < R; i ++) {
                data[i] = avg;
            }
            data[L] += extra;

            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i];
            }
            cout << "\n";
            // debug();
            return;
        }

        int sum = 0;
        for(int i = 0; i < k; i ++) {
            sum += data[i+L];
        }
        if (sum >= extra + avg*(k-1)) {
            for(int i = L; i < L+k-1; i ++) {
                data[i] = avg;
            }
            data[L] += extra;
            data[L+k-1] = sum-avg*(k-1)-extra;

            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i];
            }
            cout << "\n";
            // debug();
            output(L+k-1, R, 0);
        } else {
            output(L+k-1, R, avg*(k-1)-(sum-data[L+k-1])+extra);
            for(int i = L; i < L+k; i ++) {
                data[i] = avg;
            }
            cout << (L%n);
            for(int i = L; i < L+k; i ++) {
                cout << " " << data[i];
            }
            cout << "\n";
            // debug();
        }
    }
    void solve()
    {
        cin >> n >> k;
        for(int i = 0; i < n; i ++) {
            cin >> data[i];
            avg += data[i];
        }
        avg /= n;
        // cout << "avg = " << avg << endl;

        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 ++) {
            // cout << bestplan[i].first << " " << bestplan[i].second << endl;
        // }
        // 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);
            }
        }
    }
}

int main()
{
    Solve::solve();
    return 0;
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<int> ans[N];
int n, k, V, a[N * 2], s[N * 2];

I void gao(cr x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		reg hh = -s[n - k];
		if(hh < 0) {
			puts("FUCK");
		}
		ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		reg up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(cr i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1);
	pr2(tp);
	reg t1 = -1, t2 = -1;
	for(reg i = 1; i <= tp; i++) {
		for(reg j = 1; j <= k; j++) {
			if(ans[i][j] < 0) {
				t1 = ans[i][0], t2 = tp;
			}
		}
	} if(t1 != -1) printf("%d %d\n", t1, tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	}
	return 0;
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		for(reg i = x; i >= n - k + 1; i--) {
			if(V - s[i - 1] < 0) s[i] = a[i] = 0;
			else {a[i] = -s[i - 1], s[i] = 0; break;}
		} for(reg i = n - k + 1; i <= x; i++) ans[tp].push_back(V + a[i]);
		for(reg i = x + 1; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int already = 0;
      for (int i = tans.second, cnt = 0; cnt < n; ) {
    	int brans = ans.size();
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        if (already || (tans.first == 2408 && ans.size() > 2408)) {
        	already = 1;
      	  printf("419: %I64d %d\n", nxt[i][0].second.first, (int)ans.size(), brans);
        }
        i = nxt[i][0].first;
        cnt += nxt[i][0].second.second;
      }

      if (ans.size() != tans.first) {
    	  printf("426: %d\n", ans);
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  if (!(a >= 0)) {
    			  printf("431: %d\n", a);
    		  }
    	  }
      }
      for (int i = 0; i < n; i++) {
    	  if (orig[i] != avg) {
    		  printf("437: %d %d\n", i, orig[i]);
    	  }
      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<LL> ans[N];
int n, k, V; LL a[N * 2], s[N * 2];

I void gao(reg x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		LL hh = -s[x - 1]; a[x] = 0;
		for(reg i = x; i >= n - k + 1; i--) {
			if(a[i] + hh + V < 0) a[i] = -V, hh += V + a[i];
			else {a[i] += hh; break;}
		} for(reg i = n - k + 1; i <= x; i++) ans[tp].push_back(V + a[i]), s[i] = s[i - 1] + a[i];
		for(reg i = x + 1; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		LL up = x + k - 1, hh = -s[x - 1];
		ans[tp].push_back(hh + V), a[x] = hh; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(reg i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,M=20; vector<int>v[N]; bool fc[N];
int n,k,lf,rf,len,ans,pos,opt,les,s[N],q[N],w[N],c[N],nxt[N],f[N][M],g[N][M]; map<int,int>ft[N];
struct node {int l,r,x,t;}e[N],h[N];
inline bool operator < (node a,node b){
	if(a.x==b.x) return a.t<b.t; return a.x<b.x;
}
set<node>rt; set<int>gt;
inline int read(){
	int num=0; char g=getchar(); while(g<48||57<g) g=getchar();
	while(47<g&&g<58) num=(num<<1)+(num<<3)+g-48,g=getchar(); return num;
}
inline void write(long long u){
	if(u<0) putchar('-'),u=-u; if(u>9) write(u/10); putchar(u%10+'0');
}
inline int lowbit(int u){
	return (u&(-u));
}
inline void addit(int u,int x){
	while(u<=n) s[u]+=x,u+=lowbit(u);
}
inline int getit(int u){
	int ans=0; while(u>0) ans+=s[u],u-=lowbit(u); return ans;
}
inline int getans(int l,int r){
	while(l>=n&&r>=n) l-=n,r-=n;
	if(r<l) return 0; if(r<n) return getit(r+1)-getit(l); return getit(n)-getit(l)+getit(r-n+1);
}
inline void change(int u,int x){
	u=u%n; addit(u+1,x-w[u]),w[u]=x;
}
inline int getpos(int u){
	return w[u%n];
}
inline void dele(int u){
	gt.insert(u);
	node g=h[u]; g.x=getans(h[u].l,h[u].r); rt.erase(g);
	gt.erase(u);
}
inline void solve(int l,int r,int t){
	if(t>les) return;
	int a=getans(lf,h[t].l-1),b=getans(h[t].r+1,rf);
	if(opt-a<0||opt-b<0){
		solve(h[t+1].l,r,t+1);
		q[++q[0]]=h[t].l%n; v[q[0]].resize(k+1);
		for(int x=h[t].l;x<h[t].l+k;x++) v[q[0]][x-h[t].l+1]=getpos(x);
	}
	else{
		for(int x=h[t].l;x<=h[t].r;x++) change(x,0);
		change(h[t].l,-a),change(h[t].r,-b);
		q[++q[0]]=h[t].l%n; v[q[0]].resize(k+1);
		for(int x=h[t].l;x<h[t].l+k;x++) v[q[0]][x-h[t].l+1]=getpos(x);
		solve(h[t+1].l,r,t+1);
	}
}
inline void delit(int l,int r){
	if(l==r) return; lf=l,rf=r;
	int u=l,len=0; u=min(u+k-1,r),len++,h[len].l=l,h[len].r=u;
	while(u<r) len++,h[len].l=u,u=min(u+k-1,r),h[len].r=u; les=len;
	for(int i=1;i<=len;i++){
		h[i].t=i;
		node v; v.l=h[i].l,v.r=h[i].r,v.t=i,v.x=getans(h[i].l,h[i].r); rt.insert(v);
	}
	solve(lf,rf,1);
}
int main(){
	n=read(),k=read(); int sum=0; ans=19260817,pos=0;
	for(int i=0;i<n;i++) w[i]=w[i+n]=read(),sum+=w[i]; sum=sum/n;
	for(int i=0;i<(n<<1);i++) w[i]-=sum; c[0]=w[0]; opt=sum;
	for(int i=1;i<(n<<1);i++) c[i]=c[i-1]+w[i];
	for(int i=1;i<=n;i++) addit(i,w[i-1]);
	for(int x=0;x<=18;x++) f[(n<<1)][x]=(n<<1);
	for(int i=(n<<1)-1;i>=0;i--){
		nxt[i]=ft[i%(k-1)][c[i]]; if(!nxt[i]) nxt[i]=(n<<1);
		f[i][0]=nxt[i],g[i][0]=(nxt[i]-i-1)/(k-1);
		for(int x=1;x<=18;x++) f[i][x]=f[f[i][x-1]][x-1],g[i][x]=g[i][x-1]+g[f[i][x-1]][x-1];
		if(i<n){
			int to=i+n-1,u=i,sum=0;
			for(int x=18;x>=0;x--) while(f[u][x]<=to) sum+=g[u][x],u=f[u][x];
			sum=sum+(to-u)/(k-1)+((to-u)%(k-1)>0);
			if(sum<=ans) ans=sum,pos=i;
		}
		if(i>0) ft[(i-1)%(k-1)][c[i]]=i;
	}
	int to=pos+n-1;
	while(nxt[pos]<=to) len++,e[len].l=pos+1,e[len].r=nxt[pos],pos=nxt[pos];
	len++,e[len].l=pos+1,e[len].r=to+1;
	for(int i=1;i<=len;i++){
		delit(e[i].l,e[i].r);
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		write(q[i]),putchar(' ');
		for(int x=1;x<=k;x++) write(v[i][x]+sum),putchar(' '); putchar('\n');
	}
	return 0;
}
//6 2
//0 1 0 1 4 0

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=301000;
int n,k,a[N],s[N],avg,ps[N],ss[N],val[N],off[N];
map<int,VI> hs;
PII bf[N];
int q[N],vis[N],nval[N];
VI ret;

void print(int x,VI y) {
	assert(SZ(y)==k);
	int s1=0,s2=0;
	printf("%d",x); for (auto p:y) printf(" %d",p);
	puts("");
	rep(i,0,k) s1+=a[(i+x)%n];
	rep(i,0,k) s2+=y[i];
	assert(s1==s2);
	rep(i,0,k) assert(y[i]>=0);
	rep(i,0,k) a[(i+x)%n]=y[i];
}

void gao(int l,int r) {
//	printf("gao %d %d\n",l,r);
	int len=(r-l+k-2)/(k-1);
	int cutoff=r-l;
	//printf()
	rep(i,0,len*(k-1)+1) {
		if (l+i>r) val[i]=0;
		else val[i]=a[(l+i)%n]-avg;
		off[i]=(a[(l+i)%n]-avg)-val[i];
	}
	int m=len*(k-1)+1;
	ps[0]=val[0]; ss[m-1]=val[m-1];
	rep(i,1,m) {
		ps[i]=ps[i-1]+val[i];
	}
	per(i,0,m-1) {
		ss[i]=ss[i+1]+val[i];
	}
	int t=0;
	auto add=[&](int bl) {
		if (vis[bl]) return;
		if (bf[bl].fi>=-avg&&bf[bl].se>=-avg) {
			q[t++]=bl;
			vis[bl]=1;
		}
	};
	rep(bl,0,len) {
		int pl=bl*(k-1);
		int pr=(bl+1)*(k-1);
		bf[bl]=mp(ss[pl],ps[pr]);
	//	printf("ooo %d %d\n",bf[bl].fi,bf[bl].se);
		vis[bl]=0;
		add(bl);
	}
	rep(i,0,t) {
		int bl=q[i];
		int pl=bl*(k-1);
		int pr=(bl+1)*(k-1);
		nval[pl]=bf[bl].fi; nval[pr]=bf[bl].se;
		rep(k,pl+1,pr) nval[k]=0;
		//printf("gg %d %d\n",pl,pr);
		VI xx;
		rep(k,pl,pr+1) {
			if (k>cutoff) xx.pb(a[(l+k)%n]);
			else {
				xx.pb(nval[k]+avg);
			}
			val[k]=nval[k];
		}
		print((pl+l)%n,xx);
		if (bl>0) {
			bf[bl-1].se=0;
			add(bl-1);
		}
		if (bl+1<len) {
			bf[bl+1].fi=0;
			add(bl+1);
		}
	}
}
int main() {
	scanf("%d%d",&n,&k);
	rep(i,0,n) {
		scanf("%d",a+i);
		avg+=a[i];
	}
	assert(avg%n==0);
	avg/=n;
	rep(i,0,n) {
		s[i+1]=s[i]+a[i]-avg;
		hs[s[i]].pb(i);
	}
	int mr=n;
	for (auto p:hs) {
		VI z=p.se;
		z.pb(z[0]+n);
		int ss=0;
		rep(i,0,SZ(z)-1) {
			ss+=(z[i+1]-z[i]-1+k-2)/(k-1);
		}
		if (ss<mr) {
			mr=ss;
			ret=z;
		}
	}
	printf("%d\n",mr);
	rep(i,0,SZ(ret)-1) {
		gao(ret[i],ret[i+1]-1);
	}
	rep(i,0,n) assert(a[i]==avg);
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7,M=20; vector<int>v[N]; bool fc[N];
int n,k,lf,rf,len,ans,pos,s[N],q[N],w[N],c[N],nxt[N],f[N][M],g[N][M]; map<int,int>ft[N];
struct node {int l,r,x,t;}e[N],h[N];
inline bool operator < (node a,node b){
	if(a.x==b.x) return a.t<b.t; return a.x<b.x;
}
set<node>rt; set<int>gt;
inline int read(){
	int num=0; char g=getchar(); while(g<48||57<g) g=getchar();
	while(47<g&&g<58) num=(num<<1)+(num<<3)+g-48,g=getchar(); return num;
}
inline void write(long long u){
	if(u<0) putchar('-'),u=-u; if(u>9) write(u/10); putchar(u%10+'0');
}
inline int lowbit(int u){
	return (u&(-u));
}
inline void addit(int u,int x){
	while(u<=n) s[u]+=x,u+=lowbit(u);
}
inline int getit(int u){
	int ans=0; while(u>0) ans+=s[u],u-=lowbit(u); return ans;
}
inline int getans(int l,int r){
	while(l>=n&&r>=n) l-=n,r-=n;
	if(r<l) return 0; if(r<n) return getit(r+1)-getit(l); return getit(n)-getit(l)+getit(r-n+1);
}
inline void change(int u,int x){
	u=u%n; addit(u+1,x-w[u]),w[u]=x;
}
inline int getpos(int u){
	return w[u%n];
}
inline void dele(int u){
	gt.insert(u);
	int a=getans(lf,h[u].l-1),b=getans(h[u].r+1,rf);
	if(*gt.begin()!=u) a=getans(h[*--gt.find(u)].r,h[u].l-1);
	if(*gt.rbegin()!=u) b=getans(h[u].r+1,h[*++gt.find(u)].l);
	node g=h[u]; g.x=max(a,b); rt.erase(g);
	gt.erase(u);
}
inline void push(int u){
	gt.insert(u);
	int a=getans(lf,h[u].l-1),b=getans(h[u].r+1,rf);
	if(*gt.begin()!=u) a=getans(h[*--gt.find(u)].r,h[u].l-1);
	if(*gt.rbegin()!=u) b=getans(h[u].r+1,h[*++gt.find(u)].l);
	node g=h[u]; g.x=max(a,b); rt.insert(g);
	gt.erase(u);
}
inline void delit(int l,int r){
	if(l==r) return; lf=l,rf=r;
	int u=l,len=0; u=min(u+k-1,r),len++,h[len].l=l,h[len].r=u;
	while(u<r) len++,h[len].l=u,u=min(u+k-1,r),h[len].r=u;
	for(int i=1;i<=len;i++){
		h[i].t=i;
		node v; v.l=h[i].l,v.r=h[i].r,v.t=i,v.x=max(getans(l,v.l-1),getans(v.r+1,r)); rt.insert(v);
	}
	while(gt.size()) gt.erase(*gt.begin());
	while(rt.size()){
		node g=*rt.begin(); fc[g.t]=1;
		if(g.t-1>=1&&!fc[g.t-1]) dele(g.t-1);
		if(g.t+1<=len&&!fc[g.t+1]) dele(g.t+1);
		for(int i=g.l+1;i<g.r;i++) change(i,0); gt.insert(g.t);
		int a=getans(l,g.l-1),b=getans(g.r+1,r); change(g.l,-a),change(g.r,-b);
		if(g.t-1>=1&&!fc[g.t-1]) push(g.t-1);
		if(g.t+1<=len&&!fc[g.t+1]) push(g.t+1);
		q[0]++,q[q[0]]=g.l%n; v[q[0]].resize(k+1);
		for(int x=1;x<=k;x++) v[q[0]][x]=getpos(x+g.l-1); rt.erase(g);
	}
}
int main(){
	n=read(),k=read(); int sum=0; ans=19260817,pos=0;
	for(int i=0;i<n;i++) w[i]=w[i+n]=read(),sum+=w[i]; sum=sum/n;
	for(int i=0;i<(n<<1);i++) w[i]-=sum; c[0]=w[0];
	for(int i=1;i<(n<<1);i++) c[i]=c[i-1]+w[i];
	for(int i=1;i<=n;i++) addit(i,w[i-1]);
	for(int x=0;x<=18;x++) f[(n<<1)][x]=(n<<1);
	for(int i=(n<<1)-1;i>=0;i--){
		nxt[i]=ft[i%(k-1)][c[i]]; if(!nxt[i]) nxt[i]=(n<<1);
		f[i][0]=nxt[i],g[i][0]=(nxt[i]-i-1)/(k-1);
		for(int x=1;x<=18;x++) f[i][x]=f[f[i][x-1]][x-1],g[i][x]=g[i][x-1]+g[f[i][x-1]][x-1];
		if(i<n){
			int to=i+n-1,u=i,sum=0;
			for(int x=18;x>=0;x--) while(f[u][x]<=to) sum+=g[u][x],u=f[u][x];
			sum=sum+(to-u)/(k-1)+((to-u)%(k-1)>0);
			if(sum<=ans) ans=sum,pos=i;
		}
		if(i>0) ft[(i-1)%(k-1)][c[i]]=i;
	}
	int to=pos+n-1;
	while(nxt[pos]<=to) len++,e[len].l=pos+1,e[len].r=nxt[pos],pos=nxt[pos];
	len++,e[len].l=pos+1,e[len].r=to+1;
	for(int i=1;i<=len;i++){
		delit(e[i].l,e[i].r);
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		write(q[i]),putchar(' ');
		for(int x=1;x<=k;x++) write(v[i][x]+sum),putchar(' '); putchar('\n');
	}
	return 0;
}
//6 2
//0 1 0 1 4 0

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<int> ans[N];
int n, k, V, a[N * 2], s[N * 2];

I void gao(cr x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		reg hh = s[n] - s[n - k];
		ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		reg up = x + k - 1, hh = s[up] - s[x - 1];
		ans[tp].push_back(hh - s[up] + V), a[x] = hh - s[up]; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(cr i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	reg t1 = -1, t2 = -1;
	for(reg i = 1; i <= tp; i++) {
		for(reg j = 1; j <= k; j++) {
			if(ans[i][j] < 0) {
				t1 = ans[i][0], t2 = ans[i][j];
			}
		}
	} if(t1 != -1) printf("%d %d\n", t1, t2);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	}
	return 0;
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int n, k, V, a[N], s[N];
int tp; vector<int> ans[N];

I gao(cr x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		for(reg i = 1; i <= k; i++) ans[tp].push_back(V);
	} else {
		ans[tp].push_back(x - 1);
		reg up = x + k - 1, hh = s[up] - s[x - 1];
		ans[tp].push_back(hh - s[up] + V), a[x] = hh - s[up]; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(cr i) {
	if(i >= n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1);
	put(tp), puts("");
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<int> ans[N];
int n, k, V, a[N * 2], s[N * 2];

I void gao(cr x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		reg hh = s[n] - s[n - k];
		ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		reg up = x + k - 1, hh = s[up] - s[x - 1];
		ans[tp].push_back(hh - s[up] + V), a[x] = hh - s[up]; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(cr i) {
	if(i >= n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 100005;

int n, k;
int A = 0;
int a[2 * MX];
int need[2 * MX];

void Print(int L) {
	printf("%d ", (L - 1) % n);
	for (int i = L; i < L + k; i++) printf("%d ", a[i]);
	printf("\n");
}
void Set(int i, int x) {
	if (i <= n) {
		a[i] = a[i + n] = x;
	}
	else {
		a[i] = a[i - n] = x;
	}
}
void SetNeed(int i, int x) {
	if (i <= n) {
		need[i] = need[i + n] = x;
	}
	else {
		need[i] = need[i - n] = x;
	}
}
void Do(int L, int R)
{
	if (L > n) L -= n, R -= n;

	//printf("[%d %d]\n", L, R);

	if (R - L + 1 <= k) {
		bool ok = true;
		for (int i = L; i <= R; i++) {
			if (a[i] != need[i]) ok = false;
			Set(i, need[i]);
		}
		if (ok) return;

		Print(L);
		return;
	}

	int s = 0;
	for (int i = L; i < L + k; i++) s += a[i];
	int x = 0;
	for (int i = L; i < L + (k - 1); i++) x += need[i];

	//printf("s=%d, x=%d\n", s, x);

	if (s >= x) {
		for (int i = L; i < L + (k - 1); i++) {
			Set(i, need[i]);
		}
		Set(L + k - 1, s - x);
		Print(L);
		Do(L + k - 1, R);
	}
	else {
		SetNeed(L + k - 1, a[L + k - 1] + need[L + k - 1] + x - s);
		//printf("need[%d] = %d\n", L + k - 1, need[L + k - 1]);
		Do(L + k - 1, R);
		for (int i = L; i < L + k; i++) {
			Set(i, A);
		}
		Set(L, need[L]);
		Print(L);
	}
}

int p[MX];

map<int, vector<int>> M;

int up[2 * MX][20];
int last[2 * MX];

int main()
{
	memset(up, -1, sizeof up);
	memset(last, -1, sizeof last);

	ignore = scanf("%d %d", &n, &k);

	for (int i = 1; i <= n; i++) {
		ignore = scanf("%d", a + i);
		A += a[i];
	}
	A /= n;
	for (int i = 1; i <= n; i++) {
		p[i] = p[i - 1] + a[i] - A;
		a[i + n] = a[i];
		p[i + n] = p[i];
		M[p[i]].push_back(i);
	}

	int ans = n;
	int from = 1;

	for (auto& P : M) {
		vector<int>& v = P.second;
		//printf("v = { "); for (int i : v) printf("%d ", i); printf("}\n");

		int m = (int)v.size();
		for (int i = 0; i < m; i++) v.push_back(v[i] + n);

		for (int i = (int)v.size() - 1; i >= 0; i--) {
			int t = v[i];
			up[t][0] = last[(t + 1) % (k - 1)];
			for (int j = 0; j + 1 < 20 && up[t][j] != -1; j++) {
				up[t][j + 1] = up[up[t][j]][j];
			}
			last[t % (k - 1)] = t;
		}

		for (int i = 0; i < m; i++) {
			int cnt = 0;
			int t = v[i];
			for (int j = 19; j >= 0; j--) {
				if (up[t][j] == -1) continue;
				if (up[t][j] >= v[i] + n) continue;
				cnt += (up[t][j] - t - (1 << j)) / (k - 1);
				t = up[t][j];
			}
			//printf("v[i] = %d, t = %d\n", v[i], t);
			cnt += (v[i] + n - t + (k - 2)) / (k - 1);
			if (cnt <= ans) {
				ans = cnt;
				from = v[i];
			}
		}

		for (int i = 0; i < (int)v.size(); i++) {
			for (int j = 0; j < 20; j++) up[v[i]][j] = -1;
			last[v[i] % (k - 1)] = -1;
		}
	}

	//printf("from=%d, ans=%d\n", from, ans);
	printf("%d\n", ans);
	//for (int i = 1; i <= 2 * n; i++) printf("%d ", p[i]);
	//printf("\n");

	for (int i = 1; i <= 2 * n; i++) need[i] = A;
	for (int i = from + 1, pr = from; i <= from + n; i++) {
		if (p[i] == p[pr] && (i == from + n || (i - pr) % (k - 1) == 1)) {
			//printf("[%d %d]\n", pr + 1, i);
			Do(pr + 1, i);
			pr = i;
		}
	}
}

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))

using uint=unsigned;
using ull=unsigned long long;

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

void print(ll x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<endl;
	#else
	cout<<"Yes"<<endl;
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<endl;
	#else
	cout<<"No"<<endl;
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
int mask(int i){
	return (int(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
    static random_device rd;
    static mt19937_64 gen(rd());
    #endif
    return uniform_int_distribution<ll>(l, r)(gen);
}

pair<int,vi> sub(vi x,int m){
	//cerr<<x<<endl;
	int n=x.size();
	vi a(n,-1),b(n,-1);
	map<int,int> pos;
	per(i,n){
		if(pos.count((x[i]+1)%m))a[i]=pos[(x[i]+1)%m];
		if(pos.count(x[i]%m))b[i]=pos[x[i]%m];
		pos[x[i]%m]=i;
	}
	vc<pi> c(n,pi(-1,-1));
	c[0]=pi(0,-1);
	rep(i,n){
		if(a[i]!=-1)
			chmax(c[a[i]],pi(c[i].a+1,i));
		if(b[i]!=-1)
			chmax(c[b[i]],pi(c[i].a,i));
	}
	if(c[n-1].a==0)return make_pair(-1,vi{});
	vi res;
	int cur=n-1;
	while(c[cur].b!=-1){
		int nx=c[cur].b;
		if((x[cur]-x[nx])%m==1)
			res.pb(x[nx]);
		cur=nx;
	}
	reverse(all(res));
	//dmp(res);
	return make_pair(res.size(),res);
}

void waf(vi cur,int s,int k,int n,int off,vi&head,vvc<int>&val){
	vi ex(cur.size());
	int mn=inf;
	for(int ii=0;ii<(int)cur.size()-1;ii+=k-1){
		int i=min(ii,(int)cur.size()-k);
		int has=0;
		rng(j,i,i+k)
			has+=cur[j];
		int tar=0;
		rng(j,i,i+k)
			tar+=s+ex[j];
		if(has>=tar){
			head.pb((i+off)%n);
			val.eb(k);
			rep(j,k-1)
				has-=(val.back()[j]=s+ex[i+j]);
			val.back()[k-1]=has;
			cur[i+k-1]=has;
			for(int z=ii-(k-1);z>=mn;z-=(k-1)){
				head.pb((z+off)%n);
				val.eb(k,s);
				val.back()[0]=s+ex[z];
			}
			mn=inf;
		}else{
			ex[i+k-1]=tar-has;
			chmin(mn,i);
		}
	}
	assert(mn==inf);
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	int n,k;cin>>n>>k;
	vi a=readvi(n);

	int s=accumulate(all(a),int(0));
	s/=n;

	pair<int,vi> ans(0,vi{0});

	map<int,vi> pos;
	{
		int cur=0;
		rep(i,n){
			//cerr<<cur-s*i<<endl;
			pos[cur-s*i].pb(i);
			cur+=a[i];
		}
	}
	for(auto kv:pos){
		kv.b.pb(kv.b[0]+n);
		chmax(ans,sub(kv.b,k-1));
	}

	vi head;
	vvc<int> val;
	vi idx=ans.b;
	idx.pb(idx[0]+n);
	//cerr<<idx<<endl;
	rep(i,(int)idx.size()-1){
		int x=idx[i],y=idx[i+1];
		vi cur;
		rng(j,x,y)cur.pb(a[j%n]);
		waf(cur,s,k,n,x,head,val);
	}

	print(head.size());
	rep(i,head.size()){
		print(head[i],2);
		print(val[i]);
	}
}

#include <bits/stdc++.h>
using namespace std;

#define N 200200

int n, K, a[N], b[N];

map <int, int> mp;

int tot, s;

map <int, int> lst[N];

int nxt[N][20];

void solve(int l, int r, int rem = 0) {
    if (l + K > r) {
        int sum = 0;
        for (int i = l; i <= r; i ++) sum += b[i%n] - s, b[i%n] = s;
        b[l%n] += rem, b[r%n] += sum - rem;
        printf("%d", (l - 1) % n);
        for (int i = l; i < l + K; i ++) printf(" %d", b[i%n]);
        puts(""); return;
    }
    int sum = 0;
    for (int i = l; i < l + K; i ++) sum += b[i%n] - s;
    if (sum >= rem) {
        solve(l, l + K - 1, rem);
        solve(l + K - 1, r, 0);
    }
    else {
        sum -= b[l+K-1] - s;
        solve(l + K - 1, r, rem - sum);
        solve(l, l + K - 1, rem);
    }
}

typedef pair <int, int> pii;

int main() {
    scanf("%d %d", &n, &K);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[n+i] = a[i], s += a[i];
    for (int i = 1; i <= n; i ++) b[i%n] = a[i];
    s /= n;
    for (int i = 1; i <= 2 * n; i ++) a[i] = a[i] - s + a[i-1];
    for (int i = 2 * n; i; i --) {
        int &k = mp[a[i]];
        if (!k) k = ++tot;
        nxt[i][0] = lst[k][(i+1)%(K-1)];
        for (int j = 0; nxt[i][j]; j ++) nxt[i][j+1] = nxt[nxt[i][j]][j];
        lst[k][i%(K-1)] = i;
    }
    pii ans = pii(1e9, 0); int r = n % (K - 1);
    for (int i = 1; i <= n; i ++) {
        int k = 0, u = i;
        for (int j = 19; ~j; j --) {
            int v = nxt[u][j];
            if (v && v <= n + i) u = v, k ^= 1 << j;
        }
        int qr = k % (K - 1);
        if (qr >= r) k -= qr - r;
        else k -= qr - r + K - 1;
        ans = min(ans, pii((n - k) / (K - 1), i));
    }
    if (ans.first >= (n - 3 + K) / (K - 1)) printf("%d\n", (n - 3 + K) / (K - 1)), solve(1, n);
    else {
        printf("%d\n", ans.first);
        int u = ans.second, k = n - (K - 1) * ans.first;
        while (-- k) {
            if (u + 1 < nxt[u][0]) solve(u + 1, nxt[u][0]);
            u = nxt[u][0]; assert(u > 0);
        }
        if (u + 1 < n + ans.second) solve(u + 1, n + ans.second);
    }

    return 0;
}

//#define USEPB_DS
#define USETR1
#define CPPELEVEN
#define GPP

/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")

#include <math.h>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

#ifndef CPPELEVEN
#ifdef USETR1
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#endif
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef USEPB_DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
// binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
// splay_tree_tag, ov_tree_tag
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    tree_type;
#endif

#define mpr make_pair
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef map<string, int> mpsi;
typedef map<double, int> mpdi;
typedef map<int, int> mpii;

const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                        {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

template <class T>
inline T abs1(T a) {
  return a < 0 ? -a : a;
}

#ifndef CPPELEVEN
template <class T>
inline T max1(T a, T b) {
  return b < a ? a : b;
}
template <class T>
inline T max1(T a, T b, T c) {
  return max1(max1(a, b), c);
}
template <class T>
inline T max1(T a, T b, T c, T d) {
  return max1(max1(a, b, c), d);
}
template <class T>
inline T max1(T a, T b, T c, T d, T e) {
  return max1(max1(a, b, c, d), e);
}
template <class T>
inline T min1(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline T min1(T a, T b, T c) {
  return min1(min1(a, b), c);
}
template <class T>
inline T min1(T a, T b, T c, T d) {
  return min1(min1(a, b, c), d);
}
template <class T>
inline T min1(T a, T b, T c, T d, T e) {
  return min1(min1(a, b, c, d), e);
}
#else
template <typename t, typename t1>
t min1(t a, t1 b) {
  return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
  return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
  return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
  return max1(a, max1(arr...));
}
#endif

inline int jud(double a, double b) {
  if (abs(a) < eps && abs(b) < eps)
    return 0;
  else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
    return 0;
  if (a < b) return -1;
  return 1;
}
template <typename t>
inline int jud(t a, t b) {
  if (a < b) return -1;
  if (a == b) return 0;
  return 1;
}

template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
  if (na == 0) return 0;
  int be = 0, en = na - 1;
  if (*a <= *(a + na - 1)) {
    if (f_lb == 0)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != 1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != -1)
          en = mid;
        else
          be = mid + 1;
      }
    if (f_small && jud(*(a + be), val) == 1) be--;
    if (!f_small && jud(*(a + be), val) == -1) be++;
  } else {
    if (f_lb)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != -1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != 1)
          en = mid;
        else
          be = mid + 1;
      }
    if (!f_small && jud(*(a + be), val) == -1) be--;
    if (f_small && jud(*(a + be), val) == 1) be++;
  }
  return be;
}

template <class T>
inline T lowb(T num) {
  return num & (-num);
}
#ifdef GPP
inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(ull nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(ll nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(ui a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(ull a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
inline int bitmaxl(ll a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
#else
#endif

long long pow(long long n, long long m, long long mod = 0) {
  if (m < 0) return 0;
  long long ans = 1;
  long long k = n;
  while (m) {
    if (m & 1) {
      ans *= k;
      if (mod) ans %= mod;
    }
    k *= k;
    if (mod) k %= mod;
    m >>= 1;
  }
  return ans;
}

#define MOD 1000000007
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
  if (mod == -1) mod = MOD;
  a += b;
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
  cerr << endl;
}
template <class t>
void output2(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}

//...................................................................

const int maxn = 200100;
int orig[maxn], sum[maxn], diff[maxn], avg;
int st[maxn], lst, rst;
pair<ll, pll> nxt[maxn][20];
int n, k;
unordered_map<int, int> ump;

int GetSum(int a, int b) {
  if (a > b) {
    return sum[n - 1] - sum[a - 1] + sum[b];
  } else {
    int ans = sum[b];
    if (a) ans -= sum[a - 1];
    return ans;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
#ifdef DEBUG  //..................................................
  freopen("input.txt", "r", stdin);
#endif  //........................................................
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", orig + i);
    sum[i] = orig[i];
    if (i) sum[i] += sum[i - 1];
  }
  avg = sum[n - 1] / n;
  for (int i = 0; i < n; i++) {
    diff[i] = sum[i] - (i + 1) * avg;
  }

  for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
  for (int i = 0; i < k - 1; i++) {
    ump.clear();
    for (int j = i; j < n * 2; j += k - 1) {
      int last_no = (j - 1 + n) % n, cur_no = j % n;
      ump[diff[last_no]] = last_no;
      if (ump.find(diff[cur_no]) != ump.end()) {
        int to = ump[diff[cur_no]];
        if (to >= cur_no) {
          nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
        } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
          nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
        }
      }
    }
  }

  for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
    nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
    nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
  }

  for (int i = 0; i < 17; i++) {
    for (int j = 0; j < n; j++) {
      int to = nxt[j][i].first;
      pll rl = nxt[j][i].second;
      nxt[j][i + 1] = mpr(nxt[to][i].first,
                          mpr(nxt[to][i].second.first + rl.first,
                              nxt[to][i].second.second + rl.second));
    }
  }

  pll tans = mpr(n, -1);
  for (int i = 0; i < n; i++) {
    pll ans = mpr(0, 0);
    int last = i;
    for (int j = 17; j >= 0; j--) {
      if (nxt[last][j].second.second + ans.second < n) {
        ans.first += nxt[last][j].second.first;
        ans.second += nxt[last][j].second.second;
        last = nxt[last][j].first;
      }
    }
    ll rans = ans.first;
    if (nxt[last][0].second.second != 1) {
      rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
    }
    if (rans < tans.first) {
      tans = mpr(rans, i);
    }
  }

  printf("%d\n", (int) tans.first);
  if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
    for (int i = 0; i < n; i++) {
      if (GetSum(i, (i + k - 1) % n) > avg * k) {
        tans.second = (i + k - 1) % n;
        break;
      }
    }
  }

  for (int i = tans.second, cnt = 0; cnt < n; ) {
    lst = 0, rst = 0;
    int delta = 0;
    int cur = i;
    int rcnt = 0;
    auto push = [&]() {
      st[rst++] = orig[cur];
      delta += avg - orig[cur];
      cur = (cur - 1 + n) % n;
      rcnt++;
    };
    push();
    for (; rcnt < nxt[i][0].second.second; ) {
      do {
        for (int t = 0; t < k - 1; t++) {
          push();
        }
      } while (delta > 0 && rcnt < nxt[i][0].second.second);
      if (rst > 1) {
        for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
          int sum1 = 0, sum2 = 0;
          for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
          printf("%d", p);
          printf(" %d", !have * -delta + avg);
          orig[p] = !have * -delta + avg;
          sum2 += !have * -delta + avg;
          for (int t = 1; t < k - 1; t++) {
            printf(" %d", avg);
            orig[(t + p) % n] = avg;
            sum2 += avg;
          }
          printf(" %d\n", sum1 - sum2);
          orig[(k - 1 + p) % n] = sum1 - sum2;
          rdelta = avg - sum1 + sum2;
          have = 1;
        }
        rst = lst = 0;
        st[rst++] = -delta + avg;
      }
    }
    i = nxt[i][0].first;
    cnt += nxt[i][0].second.second;
  }

  return 0;
}

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

pii f[20][N * 2];
map<int, int> mp[N];
int tp; vector<int> ans[N];
int n, k, V, a[N * 2], s[N * 2];

I void gao(cr x) {
	++tp;
	if(x + k - 1 > n) {
		ans[tp].push_back(n - k);
		reg hh = s[n] - s[n - k];
		ans[tp].push_back(hh + V), a[n - k + 1] = hh, s[n - k + 1] = 0;
		for(reg i = n - k + 2; i <= n; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
	} else {
		ans[tp].push_back(x - 1);
		reg up = x + k - 1, hh = s[up] - s[x - 1];
		ans[tp].push_back(hh - s[up] + V), a[x] = hh - s[up]; s[x] = 0;
		for(reg i = x + 1; i < up; i++) ans[tp].push_back(V), a[i] = s[i] = 0;
		ans[tp].push_back(s[up] + V), a[up] = s[up];
	}
}

void dfs(cr i) {
	if(i > n) return ;
	if(a[i] == 0 && s[i] == 0) {dfs(i + 1); return ;}
	reg up = _min(n, i + k - 1);
	if(s[up] >= 0) gao(i), dfs(i + k - 1);
	else dfs(i + k - 1), gao(i);
}

int main() {
	n = read(), k = read();
	for(reg i = 1; i <= n; i++) a[i] = read(), V += a[i];
	V /= n; for(reg i = 0; i <= 17; i++) f[i][2 * n + 1] = pii{2 * n + 1, 0};
	for(reg i = 1; i <= n; i++) a[i] = a[i] - V, a[i + n] = a[i];
	for(reg i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + a[i];
	reg mn = 999999999, pos = -1;
	for(reg i = 2 * n; i >= 1; i--) {
		reg c = (i + 1) % (k - 1), nxt = 2 * n;
		if(mp[c][s[i]]) nxt = mp[c][s[i]], f[0][i] = pii{nxt, (nxt - i - 1) / (k - 1)};
		else f[0][i] = pii{2 * n + 1, 0};
		for(reg j = 1; j <= 17; j++) f[j][i] = pii{f[j - 1][f[j - 1][i].fi].fi, f[j - 1][i].se + f[j - 1][f[j - 1][i].fi].se};
		if(i <= n) {
			reg now = i, up = i + n, sum = 0;
			for(reg j = 17; j >= 0; j--) if(f[j][now].fi <= up){
				sum += f[j][now].se, now = f[j][now].fi;
			} sum += (up - now - 1 + k - 2) / (k - 1);
			if(sum < mn) mn = sum, pos = i;
		} mp[i % (k - 1)][s[i]] = i;
	} for(reg i = pos + 1; i <= n; i++) s[i - pos] = a[i];
	for(reg i = 1; i <= pos; i++) s[(i + n - pos - 1) % n + 1] = a[i];
	memcpy(a, s, sizeof(a)); for(reg i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	dfs(1), pr2(tp);
	reg tt = -1;
	for(reg i = 1; i <= tp; i++) {
		for(reg j = 1; j <= k; j++) {
			if(ans[i][j] < 0) {
				tt = (ans[i][0] + pos) % n;
			}
		}
	} if(tt != -1) pr2(tt);
	for(reg i = 1; i <= tp; i++) {
		pr1((ans[i][0] + pos) % n);
		for(reg j = 1; j <= k; j++) pr1(ans[i][j]);
		puts("");
	} return 0;
}

//#define DEBUG
    //#define USEPB_DS
    #define USETR1
    #define CPPELEVEN
    #define GPP

    /*
     * temp.cpp
     *
     *  Created on: 2012-7-18
     *      Author: BSBandme
     */
    //#pragma comment(linker, "/STACK:1024000000,1024000000")

    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cctype>
    #include <climits>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <deque>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <numeric>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <string>
    #include <tuple>
    #include <utility>
    #include <vector>

    using namespace std;

    #ifndef CPPELEVEN
    #ifdef USETR1
    #include <tr1/unordered_map>
    #include <tr1/unordered_set>
    using namespace tr1;
    #endif
    #else
    #include <unordered_map>
    #include <unordered_set>
    #endif

    #ifdef USEPB_DS
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace __gnu_pbds;
    // binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag
    typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq_type;
    // splay_tree_tag, ov_tree_tag
    typedef tree<int, null_type, less<int>, rb_tree_tag,
                 tree_order_statistics_node_update>
        tree_type;
    #endif

    #define mpr make_pair
    typedef unsigned int ui;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef map<string, int> mpsi;
    typedef map<double, int> mpdi;
    typedef map<int, int> mpii;

    const double pi = acos(0.0) * 2.0;
    const long double eps = 1e-10;
    const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                            {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

    template <class T>
    inline T abs1(T a) {
      return a < 0 ? -a : a;
    }

    #ifndef CPPELEVEN
    template <class T>
    inline T max1(T a, T b) {
      return b < a ? a : b;
    }
    template <class T>
    inline T max1(T a, T b, T c) {
      return max1(max1(a, b), c);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d) {
      return max1(max1(a, b, c), d);
    }
    template <class T>
    inline T max1(T a, T b, T c, T d, T e) {
      return max1(max1(a, b, c, d), e);
    }
    template <class T>
    inline T min1(T a, T b) {
      return a < b ? a : b;
    }
    template <class T>
    inline T min1(T a, T b, T c) {
      return min1(min1(a, b), c);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d) {
      return min1(min1(a, b, c), d);
    }
    template <class T>
    inline T min1(T a, T b, T c, T d, T e) {
      return min1(min1(a, b, c, d), e);
    }
    #else
    template <typename t, typename t1>
    t min1(t a, t1 b) {
      return a < b ? a : b;
    }
    template <typename t, typename... arg>
    t min1(t a, arg... arr) {
      return min1(a, min1(arr...));
    }
    template <typename t, typename t1>
    t max1(t a, t1 b) {
      return a > b ? a : b;
    }
    template <typename t, typename... arg>
    t max1(t a, arg... arr) {
      return max1(a, max1(arr...));
    }
    #endif

    inline int jud(double a, double b) {
      if (abs(a) < eps && abs(b) < eps)
        return 0;
      else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
        return 0;
      if (a < b) return -1;
      return 1;
    }
    template <typename t>
    inline int jud(t a, t b) {
      if (a < b) return -1;
      if (a == b) return 0;
      return 1;
    }

    template <typename it, typename t1>
    inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
      if (na == 0) return 0;
      int be = 0, en = na - 1;
      if (*a <= *(a + na - 1)) {
        if (f_lb == 0)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != 1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != -1)
              en = mid;
            else
              be = mid + 1;
          }
        if (f_small && jud(*(a + be), val) == 1) be--;
        if (!f_small && jud(*(a + be), val) == -1) be++;
      } else {
        if (f_lb)
          while (be < en) {
            int mid = (be + en + 1) / 2;
            if (jud(*(a + mid), val) != -1)
              be = mid;
            else
              en = mid - 1;
          }
        else
          while (be < en) {
            int mid = (be + en) / 2;
            if (jud(*(a + mid), val) != 1)
              en = mid;
            else
              be = mid + 1;
          }
        if (!f_small && jud(*(a + be), val) == -1) be--;
        if (f_small && jud(*(a + be), val) == 1) be++;
      }
      return be;
    }

    template <class T>
    inline T lowb(T num) {
      return num & (-num);
    }
    #ifdef GPP
    inline int bitnum(ui nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
    inline int bitnum(ull nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitnum(ll nValue) {
      return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
    }
    inline int bitmaxl(ui a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(int a) {
      if (a == 0) return 0;
      return 32 - __builtin_clz(a);
    }
    inline int bitmaxl(ull a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    inline int bitmaxl(ll a) {
      int temp = a >> 32;
      if (temp) return 32 - __builtin_clz(temp) + 32;
      return bitmaxl(int(a));
    }
    #else
    #endif

    long long pow(long long n, long long m, long long mod = 0) {
      if (m < 0) return 0;
      long long ans = 1;
      long long k = n;
      while (m) {
        if (m & 1) {
          ans *= k;
          if (mod) ans %= mod;
        }
        k *= k;
        if (mod) k %= mod;
        m >>= 1;
      }
      return ans;
    }

    #define MOD 1000000007
    template <class t1, class t2>
    inline void add(t1 &a, t2 b, int mod = -1) {
      if (mod == -1) mod = MOD;
      a += b;
      while (a >= mod) a -= mod;
      while (a < 0) a += mod;
    }
    template <class t>
    void output1(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
      cerr << endl;
    }
    template <class t>
    void output2(t arr) {
      for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
    }

    //...................................................................

    const int maxn = 200100;
    int orig[maxn], sum[maxn], diff[maxn], avg;
    int st[maxn], lst, rst;
    pair<ll, pll> nxt[maxn][20];
    int n, k;
    unordered_map<int, int> ump;
    vector<vi> ans;

    int GetSum(int a, int b) {
      if (a > b) {
        return sum[n - 1] - sum[a - 1] + sum[b];
      } else {
        int ans = sum[b];
        if (a) ans -= sum[a - 1];
        return ans;
      }
    }

    int main() {
      ios_base::sync_with_stdio(0);
    #ifdef DEBUG  //..................................................
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  //........................................................
      scanf("%d%d", &n, &k);
      for (int i = 0; i < n; i++) {
        scanf("%d", orig + i);
        sum[i] = orig[i];
        if (i) sum[i] += sum[i - 1];
      }
      avg = sum[n - 1] / n;
      for (int i = 0; i < n; i++) {
        diff[i] = sum[i] - (i + 1) * avg;
      }

      for (int i = 0; i < n; i++) nxt[i][0] = mpr(-1, mpr(0, 0));
      for (int i = 0; i < k - 1; i++) {
        ump.clear();
        for (int j = i; j < n * 2; j += k - 1) {
          int last_no = (j - 1 + n) % n, cur_no = j % n;
          ump[diff[last_no]] = last_no;
          if (ump.find(diff[cur_no]) != ump.end()) {
            int to = ump[diff[cur_no]];
            if (to >= cur_no) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to + n - 1) / (k - 1), cur_no - to + n));
            } else if ((cur_no - to) % (k - 1) == 1 || k == 2) {
              nxt[cur_no][0] = mpr(to, mpr((cur_no - to - 1) / (k - 1), cur_no - to));
            }
          }
        }
      }

      for (int i = 0; i < n; i++) if (nxt[i][0].first == -1) {
        nxt[i][0].second = mpr((n - 1 - 1) / (k - 1) + 1, ((n - 1 - 1) / (k - 1) + 1) * (k - 1) + 1);
        nxt[i][0].first = (i - nxt[i][0].second.second % n + n) % n;
      }

      for (int i = 0; i < 17; i++) {
        for (int j = 0; j < n; j++) {
          int to = nxt[j][i].first;
          pll rl = nxt[j][i].second;
          nxt[j][i + 1] = mpr(nxt[to][i].first,
                              mpr(nxt[to][i].second.first + rl.first,
                                  nxt[to][i].second.second + rl.second));
        }
      }

      pll tans = mpr(n, -1);
      for (int i = 0; i < n; i++) {
        pll ans = mpr(0, 0);
        int last = i;
        for (int j = 17; j >= 0; j--) {
          if (nxt[last][j].second.second + ans.second < n) {
            ans.first += nxt[last][j].second.first;
            ans.second += nxt[last][j].second.second;
            last = nxt[last][j].first;
          }
        }
        ll rans = ans.first;
        if (nxt[last][0].second.second != 1) {
          rans += max(1ll, (n - ans.second - 1 - 1) / (k - 1) + 1);
        }
        if (rans < tans.first) {
          tans = mpr(rans, i);
        }
      }

      printf("%d\n", (int) tans.first);
      if (tans.first == (n - 1 - 1) / (k - 1) + 1) {
        for (int i = 0; i < n; i++) {
          if (GetSum(i, (i + k - 1) % n) > avg * k) {
            tans.second = (i + k - 1) % n;
            break;
          }
        }
      }

      int cnt = 0, i, already = 0;
      for (i = tans.second; cnt < n; ) {
    	int brans = ans.size();
        lst = 0, rst = 0;
        int delta = 0;
        int cur = i;
        int rcnt = 0;
        auto push = [&]() {
          st[rst++] = orig[cur];
          delta += avg - orig[cur];
          cur = (cur - 1 + n) % n;
          rcnt++;
        };
        push();
        for (; rcnt < nxt[i][0].second.second; ) {
          do {
            for (int t = 0; t < k - 1; t++) {
              push();
            }
          } while (delta > 0 && rcnt < nxt[i][0].second.second);
          if (rst > 1) {
            for (int p = (cur + 1) % n, rdelta = delta, have = 0; rst > 1; p = (p + k - 1) % n, rst -= k - 1) {
              ll sum1 = 0, sum2 = 0;
              for (int t = 0; t < k; t++) sum1 += orig[(p + t) % n];
              vi p1;
              p1.push_back(p);
              if (!already && tans.first == 2408) {
            	  printf("399: %d\n", p);
              }
              p1.push_back(!have * -delta + avg);
              orig[p] = !have * -delta + avg;
              sum2 += !have * -delta + avg;
              for (int t = 1; t < k - 1; t++) {
                p1.push_back(avg);
                orig[(t + p) % n] = avg;
                sum2 += avg;
              }
              p1.push_back(sum1 - sum2);
              ans.push_back(p1);
              orig[(k - 1 + p) % n] = sum1 - sum2;
              rdelta = avg - sum1 + sum2;
              have = 1;
            }
            rst = lst = 0;
            st[rst++] = -delta + avg;
          }
        }

        cnt += nxt[i][0].second.second;
        i = nxt[i][0].first;
      }

//      if (ans.size() != tans.first) {
//    	  printf("426: %d %d %d %d %I64d\n", ans.size(), cnt, ans.back()[0], i, tans.second);
//      }
//      for (const auto& it : ans) {
//    	  for (int a : it) {
//    		  if (!(a >= 0)) {
//    			  printf("431: %d\n", a);
//    		  }
//    	  }
//      }
//      for (int i = 0; i < n; i++) {
//    	  if (orig[i] != avg) {
//    		  printf("437: %d %d\n", i, orig[i]);
//    	  }
//      }
      for (const auto& it : ans) {
    	  for (int a : it) {
    		  printf("%d ", a);
    	  }
    	  puts("");
      }

      return 0;
    }

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn * 2], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute2(int l, int r);
void distribute1(int l, int r){
    //cout << "d1 " << l << " " << r << endl;
    int len = r <= l ? r + n - l: r - l;
    if(len <= k){
        int ok = 1;
        for(int i = l;;){
            ok &= a[i] == avg;
            i = (i + 1) % n;
            if(i == r) break;
        }
        if(ok) return;
        int sum = 0;
        for(int i = l;;){
            sum += a[i];
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << l;
        for(int i = l;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == (r + n - 1) % n) break;
        }
        cout << " " << (a[(r + n - 1) % n] = sum - (len - 1) * avg);
        for(int i = r; i != (l + k) % n; i = (i + 1) % n)
            cout << " " << a[i];
        cout << "\n";
        return;
    }
    int sum = 0;
    for(int i = l;;){
        sum += a[i];
        i = (i + 1) % n;
        if(i == (l + k) % n) break;
    }
    if(sum >= (k - 1) * avg){
        cout << l;
        for(int i = l;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == (l + k - 1) % n) break;
        }
        cout << " " << (a[(l + k - 1) % n] = sum - (k - 1) * avg) << "\n";
        distribute1((l + k - 1) % n, r);
        return;
    }
    distribute2((l + k - 1) % n, r);
    distribute1(l, (l + k) % n);
}

void distribute2(int l, int r){
    //cout << "d2 " << l << " " << r << endl;
    int len = r <= l ? r + n - l: r - l;
    if(len <= k){
        int ok = 1;
        for(int i = l;;){
            ok &= a[i] == avg;
            i = (i + 1) % n;
            if(i == r) break;
        }
        if(ok) return;
        int sum = 0;
        for(int i = l;;){
            sum += a[i];
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << l;
        cout << " " << (a[l] = sum - (len - 1) * avg);
        for(int i = (l + 1) % n;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == (r + n - 1) % n) break;
        }
        cout << " " << (a[(r + n - 1) % n] = sum - (len - 1) * avg);
        for(int i = r; i != (l + k) % n; i = (i + 1) % n)
            cout << " " << a[i];
        cout << "\n";
        return;
    }
    int sum = 0;
    for(int i = (r + n - k) % n;;){
        sum += a[i];
        i = (i + 1) % n;
        if(i == r) break;
    }
    if(sum >= (k - 1) * avg){
        cout << (r + n - k) % n;
        cout << " " << (a[(r + n - k) % n] = sum - (k - 1) * avg);
        for(int i = (r + n - k + 1) % n;;){
            cout << " " << (a[i] = avg);
            i = (i + 1) % n;
            if(i == r) break;
        }
        cout << "\n";
        distribute2(l, (r + n - k + 1) % n);
        return;
    }
    distribute1(l, (r + n - k + 1) % n);
    distribute2((r + n - k) % n, r);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i + n] = a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        vector<int> pos(k - 1, c * 2);
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; i; i -= 1){
            next[i][0] = pos[(v[i] + 1) % (k - 1)];
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1) if(v[i] % (k - 1) == 0){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        //cout << "cut" << " " << cut[i] << endl;
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        interval.push_back({l, r});
        if(r <= l) r += n;
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute1(p.first, p.second);
    return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <unordered_set>
#include <algorithm>

using namespace std;
typedef long long ll;

int n, k;
vector<int> a, asum;

vector<vector<pair<ll,ll>>> dp(100010,vector<pair<ll,ll>>(25));

void init() {
    vector<map<int,int>> asumeq(n);
    for (int i=2*n; i>=0; --i) {
        if (i<n) {
            if (asumeq[(i+1)%(k-1)].count(asum[i]) && asumeq[(i+1)%(k-1)][asum[i]] <= n+i) {
                int x = asumeq[(i+1)%(k-1)][asum[i]] - i;
                dp[i][0] = make_pair(x, (x-1)/(k-1));
            }
            else dp[i][0] = make_pair(n, (n+k-3)/(k-1));
            asumeq[i%(k-1)][asum[i]] = i;
        }
    }
}

unordered_set<int> moved;
unordered_set<int> moves;

bool canMove(int a, int s) {
	if (moved.count(a)) return false;
	int b = a + k;
    return (asum[a] - asum[s] <= 0 || asum[a+1] - asum[s] <= 0) && (asum[b] - asum[s] >= 0 || asum[b-1] - asum[s] >= 0);
};

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(4*n); asum.resize(4*n+1);
    int avg = 0;
    for (int i=0; i<n; ++i) {
        cin >> a[i];
        avg += a[i];
    }
    avg /= n;
    bool eq = true;
    for (int i=0; i<n; ++i) {
        a[i] -= avg;
        if (a[i]!=0) eq = false;
    }
    if (eq) {
        cout << 0 << endl;
        return 0;
    }
    for (int i=0; i<n; ++i) a[i+3*n] = a[i+2*n] = a[i+n] = a[i];
    asum[0] = 0;
    for (int i=0; i<4*n; ++i) asum[i+1] = asum[i]+a[i];

    init();
    int start = -1, ans = n+1;
    for (int i=0; i<n; ++i) {
        int curr = i, cost = 0;
        for (int j = 24; j >= 0; --j) {
			if (curr + dp[curr % n][j].first < i + n) {
				cost += int(dp[curr % n][j].second);
				curr += int(dp[curr % n][j].first);
			}
		}
		cost += (i+n - curr - 1 + (k-2)) / (k-1);
		if (cost < ans) {
			ans = cost;
			start = i;
		}
    }
    cout << ans << endl;

    for (int s=start; s<start+n; ) {
		int en = s + int(dp[s%n][0].first);
		en = min(en, start + n);
        while ((en-s-1) % (k-1) != 0) {
			asum[en+1] = asum[en];
			en++;
        }

        moved.clear();
        moves.clear();
		for (int x = s; x+k <= en; x += k-1) {
			if (canMove(x,s)) {
				moves.insert(x);
			}
		}
		while (!moves.empty()) {
			int x = *moves.begin();
			moves.erase(moves.begin());
			moved.insert(x);
			int y = x+k;
			for (int i = 1; i <= k-1; ++i) {
				asum[x+i] = asum[s];
			}

			cout << (x%n);
			for (int i = 0; i < k; ++i) {
				if (x+i < start + n) {
					a[(x+i)%n] = asum[x+i+1] - asum[x+i];
				}
				cout << " " << avg + a[(x+i)%n];
			}
			cout << endl;

			if (x > s) {
				if (canMove(x - (k-1),s)) {
					moves.insert(x - (k-1));
				} else {
					moves.erase(x - (k-1));
				}
			}
			if (y < en) {
				if (canMove(x + (k-1),s)) {
					moves.insert(x + (k-1));
				} else {
					moves.erase(x + (k-1));
				}
			}
		}

		s = en;
	}

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 100000;
int a[maxn], p[maxn], n, k, avg;
map<int, vector<int>> mp;
void distribute(int l, int len, int need){
    //cout << "dis " << l << " " << len << " " << need << endl;
    //for(int i = 0; i < len; i += 1) cout << a[(l + i) % n] << " ";
    //cout << endl;
    if(len <= 1) return;
    if(a[l] == avg and need == 0){
        distribute((l + 1) % n, len - 1, 0);
        return;
    }
    int sum = 0, m = min(len, k);
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    if(sum >= avg * (m - 1) + need){
        sum -= a[l] = avg + need;
        for(int i = 1; i < m - 1; i += 1) sum -= a[(l + i) % n] = avg;
        a[(l + m - 1) % n] = sum;
        cout << l;
        for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
        cout << "\n";
        distribute((l + m - 1) % n, len - m + 1, 0);
        return;
    }
    distribute((l + m - 1) % n, len - m + 1, avg * (m - 1) + need - sum + a[(l + m - 1)]);
    sum = 0;
    for(int i = 0; i < m; i += 1) sum += a[(l + i) % n];
    for(int i = 1; i < m; i += 1) sum -= a[(l + i) % n] = avg;
    a[l] = sum;
    cout << l;
    for(int i = 0; i < k; i += 1) cout << " " << a[(l + i) % n];
    cout << "\n";
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i += 1){
        cin >> a[i];
        avg += a[i];
    }
    avg/= n;
    for(int i = 0; i < n; i += 1){
        p[i] = (i ? p[i - 1] + a[i] : a[0]) - avg;
        mp[p[i]].push_back(i);
    }
    int ans = 1;
    vector<int> cut(1);
    for(auto& p : mp){
        auto& v = p.second;
        int c = v.size();
        v.resize(c * 2);
        for(int i = 0; i < c; i += 1) v[i + c] = v[i] + n;
        map<int, int> pos;
        vector<int[20]> next(2 * c + 1);
        fill(*next.rbegin(), *next.rbegin() + 20, 2 * c);
        for(int i = 2 * c - 1; ~i; i -= 1){
            if(pos.count((v[i] + 1) % (k - 1)))
                next[i][0] = pos[(v[i] + 1) % (k - 1)];
            else next[i][0] = 2 * c;
            for(int j = 1; j < 20; j += 1) next[i][j] = next[next[i][j - 1]][j - 1];
            pos[v[i] % (k - 1)] = i;
        }
        int pans = 0, begin = 0;
        for(int i = 0; i < c; i += 1){
            int p = i, ppans = 0;
            for(int j = 19; ~j; j -= 1) if(next[p][j] <= i + c){
                p = next[p][j];
                ppans += 1 << j;
            }
            if(p == i + c and ppans > pans){
                pans = ppans;
                begin = i;
            }
        }
        if(pans > ans){
            ans = pans;
            cut.clear();
            for(int i = begin; i < begin + c; i = next[i][0])
                cut.push_back(v[i]);
        }
    }
    int time = 0;
    vector<pair<int, int>> interval;
    for(int i = 0; i < (int)cut.size(); i += 1){
        //cout << "cut" << " " << cut[i] << endl;
        int l = (cut[i] + 1) % n, r = (cut[(i + 1) % cut.size()] + 1) % n;
        if(r <= l) r += n;
        interval.push_back({l, r - l});
        time += (r - l + k - 3) / (k - 1);
    }
    cout << time << "\n";
    for(auto p : interval)
        distribute(p.first, p.second, 0);
    for(int i = 0; i < n; i += 1) if(a[i] != avg) cout << i << " " << a[i] << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 200200

int n, K, a[N], b[N];

map <int, int> mp;

int tot, s;

map <int, int> lst[N];

int nxt[N][20];

void solve(int l, int r, int rem = 0) {
    if (l + K > r) {
        int sum = 0;
        for (int i = l; i <= r; i ++) sum += b[i] - s, b[i] = s;
        b[l] += rem, b[r] += sum - rem;
        printf("%d", (l - 1) % n);
        for (int i = l; i < l + K; i ++) printf(" %d", b[i]);
        puts(""); return;
    }
    int sum = 0;
    for (int i = l; i < l + K; i ++) sum += b[i] - s;
    if (sum >= rem) {
        solve(l, l + K - 1, rem);
        solve(l + K - 1, r, 0);
    }
    else {
        sum -= b[l+K-1] - s;
        solve(l + K - 1, r, rem - sum);
        solve(l, l + K - 1, rem);
    }
}

typedef pair <int, int> pii;

int main() {
    scanf("%d %d", &n, &K);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[n+i] = a[i], s += a[i];
    for (int i = 1; i <= 2 * n; i ++) b[i] = a[i];
    s /= n;
    for (int i = 1; i <= 2 * n; i ++) a[i] = a[i] - s + a[i-1];
    for (int i = 2 * n; i; i --) {
        int &k = mp[a[i]];
        if (!k) k = ++tot;
        nxt[i][0] = lst[k][(i+1)%(K-1)];
        for (int j = 0; nxt[i][j]; j ++) nxt[i][j+1] = nxt[nxt[i][j]][j];
        lst[k][i%(K-1)] = i;
    }
    pii ans = pii(1e9, 0); int r = n % (K - 1);
    for (int i = 1; i <= n; i ++) {
        int k = 0, u = i;
        for (int j = 19; ~j; j --) {
            int v = nxt[u][j];
            if (v && v <= n + i) u = v, k ^= 1 << j;
        }
        int qr = k % (K - 1);
        if (qr >= r) k -= qr - r;
        else k -= qr - r + K - 1;
        ans = min(ans, pii((n - k) / (K - 1), i));
    }
    if (ans.first >= (n - 3 + K) / (K - 1)) printf("%d\n", (n - 3 + K) / (K - 1)), solve(1, n);
    else {
        printf("%d\n", ans.first);
        int u = ans.second, k = n - (K - 1) * ans.first;
        while (-- k) {
            if (u + 1 < nxt[u][0]) solve(u + 1, nxt[u][0]);
            u = nxt[u][0]; assert(u > 0);
        }
        if (u + 1 < n + ans.second) solve(u + 1, n + ans.second);
    }

    return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <unordered_set>
#include <algorithm>

using namespace std;
typedef long long ll;

int n, k;
vector<int> a, asum;

vector<vector<pair<ll,ll>>> dp(100010,vector<pair<ll,ll>>(25));

void init() {
    vector<map<int,int>> asumeq(n);
    for (int i=2*n; i>=0; --i) {
        if (i<n) {
            if (asumeq[(i+1)%(k-1)].count(asum[i]) && asumeq[(i+1)%(k-1)][asum[i]] <= n+i) {
                int x = asumeq[(i+1)%(k-1)][asum[i]] - i;
                dp[i][0] = make_pair(x, (x-1)/(k-1));
            }
            else dp[i][0] = make_pair(n, (n+k-3)/(k-1));
            asumeq[i%(k-1)][asum[i]] = i;
        }
    }
}

unordered_set<int> moved;
unordered_set<int> moves;

bool canMove(int a, int s) {
	if (moved.count(a)) return false;
	int b = a + k;
    return (asum[a] - asum[s] <= 0 || asum[a+1] - asum[s] <= 0) && (asum[b] - asum[s] >= 0 || asum[b-1] - asum[s] >= 0);
};

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(4*n); asum.resize(4*n);
    int avg = 0;
    for (int i=0; i<n; ++i) {
        cin >> a[i];
        avg += a[i];
    }
    avg /= n;
    for (int i=0; i<n; ++i) a[i] -= avg;
    for (int i=0; i<n; ++i) a[i+3*n] = a[i+2*n] = a[i+n] = a[i];
    asum[0] = 0;
    for (int i=0; i<4*n; ++i) asum[i+1] = asum[i]+a[i];

    init();
    int start = -1, ans = 2*n;
    for (int i=0; i<n; ++i) {
        int curr = i, cost = 0;
        for (int j = 24; j >= 0; --j) {
			if (curr + dp[curr % n][j].first < i + n) {
				cost += int(dp[curr % n][j].second);
				curr += int(dp[curr % n][j].first);
			}
		}
		cost += (i+n - curr - 1 + (k-2)) / (k-1);
		if (cost < ans) {
			ans = cost;
			start = i;
		}
    }
    cout << ans << endl;

    for (int s=start; s<start+n; ) {
		int en = s + int(dp[s%n][0].first);
		en = min(en, start + n);
        while ((en-s-1) % (k-1) != 0) {
			asum[en+1] = asum[en];
			en++;
        }

        moved.clear();
        moves.clear();
		for (int x = s; x+k <= en; x += k-1) {
			if (canMove(x,s)) {
				moves.insert(x);
			}
		}
		while (!moves.empty()) {
			int x = *moves.begin();
			moves.erase(moves.begin());
			moved.insert(x);
			int y = x+k;
			for (int i = 1; i <= k-1; ++i) {
				asum[x+i] = asum[s];
			}

			cout << (x%n);
			for (int i = 0; i < k; ++i) {
				if (x+i < start + n) {
					a[(x+i)%n] = asum[x+i+1] - asum[x+i];
				}
				cout << " " << avg + a[(x+i)%n];
			}
			cout << endl;

			if (x > s) {
				if (canMove(x - (k-1),s)) {
					moves.insert(x - (k-1));
				} else {
					moves.erase(x - (k-1));
				}
			}
			if (y < en) {
				if (canMove(x + (k-1),s)) {
					moves.insert(x + (k-1));
				} else {
					moves.erase(x + (k-1));
				}
			}
		}

		s = en;
	}

    return 0;
}