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

using namespace std; 

#define sz(x) x.size
#define pb push_back
#define all(x) x.begin(),x.end()

// my vector
struct vec{
	int a[20100], size;
	vec() { memset(a,0,sizeof a); }
	void resize(int n) { size = n; }
	void assign(int n, int val) {
		size = n;
		for (int i = 0; i < n; i++)
			a[i] = val;
	}
	void push_back(int x) {
		a[size++] = x;
	}
	void clear() { size = 0; }
	int* begin() { return a; }
	int* end() { return a+size; }
	int& operator[] (int i) { return a[i]; }
};

vec a, b, chain, u, allshifts, save, ptr, st, oldptr;

int N, M, W, Q, shifts, v, cheaton;

// adds all chain of shifts to chain
void find_chain(int v) {
	while (true) {
		u[v] = 1;
		st.pb(v);
		chain.pb(v);
		save[v] = a[v];
		int val = a[v];
	   	int &to = ptr[val];
		while (to < N && (u[to] || a[to] == val) && b[to] == val)
			to++;
		if (to == N || b[to] != val)
			return;
		v = to;
	}
}

int main() {
	// input
	scanf("%d%d%d", &N, &M, &W);
	a.resize(N);
	ptr.resize(M+1);
	v = N-1;

	for (int i = 0; i < N; i++)
		scanf("%d", &a[i]);
	
	// b = sorted(a)
	b = save = a;
	stable_sort(all(b));

	// ptr[i] points at first 
	// position of i in sorted array b
	// O(MlgN)
	for (int i = 1; i <= M; i++)
		ptr[i] = lower_bound(all(b), i) - b.begin();

	Q = (N+W-2) / (W-1);
	// for every round out of Q
	printf("%d\n", Q);

	// next cycle works O(N/W) time
	while (Q--) {
		// if there is no need to
		// do anything, just output zero
		if (cheaton) {
			puts("0");
			continue;
		}

		shifts = 0;
		allshifts.clear();

		for (int i = 0; i <= M; i++)
			oldptr[i] = ptr[i];

		// until number of shifts < W
		while (shifts < W) {				// O(W)
			chain.clear();
			// find any number standing wrong
			while (v >= 0 && (u[v] || a[v] == b[v]))
				v--;
			if (v == -1) 
				break;
			find_chain(v);
			// add chain until we fillup shifts
			chain.resize(min(sz(chain), W-shifts));
			for (int i = 0; i < sz(chain); i++) {
				allshifts.pb(chain[i]);
				allshifts.pb(chain[(i+1)%sz(chain)]);
			}

			shifts += sz(chain);
		}

		// if there are no changes, 
		// no need to output anything but zeroes
		if (!shifts)
			cheaton = 1;

		// output all changes
		printf("%d ", shifts);
		for (int i = 0; i < sz(allshifts); i++)
			printf("%d ", allshifts[i]+1);
		printf("\n");

		// apply all chains now
		for (int i = 0; i < sz(allshifts); i += 2)
			a[allshifts[i+1]] = save[allshifts[i]];

		// move all ptrs as far as possible
		for (int i = 1; i <= M; i++) {
			ptr[i] = oldptr[i];
			int &o = ptr[i];
			while (o < N && b[o] == i && a[o] == i)
				o++;
		}

		// clear used array
		for (int i = 0; i < sz(st); i++)
			u[st[i]] = 0;
		st.clear();

	}

	return 0;
}
