#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
int A[52];
vector<pair<int, int> > ans;
bool board[2502][52]; // 0-indexed
void generateBoard(){
for(int i=0; i<n; i++) for(int j=0; j<A[i]; j++) board[j][i] = true;
}
int getMaxHeight(){
for(int row=0; row<=2501; row++){
bool ex = 0;
for(int col=0; col<n; col++) if(board[row][col]){
ex = 1;
break;
}
if(!ex) return row-1;
}
exit(1);
}
void updateBoard(){
int pnt = 0, max_height = getMaxHeight();
for(int row=0; row<=max_height; row++){
bool empty = !count(board[row], board[row]+n, true);
bool full = !count(board[row], board[row]+n, false);
if(full){
fill(board[row], board[row]+n, false);
empty = true;
}
if(empty) continue;
if(row != pnt){
copy(board[row], board[row]+n, board[pnt]);
fill(board[row], board[row]+n, false);
}
pnt++;
}
}
void printBoard(string msg){
#ifdef LOCAL
cout << "Board state: " << msg << "\n";
int max_height = getMaxHeight();
for(int row=max_height; row>=0; row--){
for(int col=0; col<n; col++) cout << (board[row][col] ? '#' : '.');
cout << "\n";
}
cout << "\n";
#endif
}
int main(){
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(NULL);
#endif
cin >> n >> k;
for(int i=0; i<n; i++) cin >> A[i];
// Check availability
{
vector<int> sum (k);
for(int i=0; i<n; i++) sum[i%k] += A[i];
int p = n%k;
for(int i=0; i<p; i++) if(sum[i] % k != sum[0] % k) {
cout << -1;
return 0;
}
for(int i=p; i<k; i++) if(sum[i] % k != sum[p] % k){
cout << -1;
return 0;
}
}
// Step 1
for(int i=1; i<n; i++){
while(A[i] < A[i-1]){
A[i] += k;
ans.push_back({1, i});
}
}
// Step 2
{
generateBoard();
printBoard("After Step 1");
int mx_height = getMaxHeight();
for(int row=0; row<=mx_height; row++){
int col = -1;
while(col+1 < n && !board[row][col+1]) col++;
for(int s=col-k+1; s>=0; s-=k){
ans.push_back({2, s});
board[row][s] = true;
for(int j=0; j<k; j++) board[row][s+j] = true;
}
}
// update board - clear full rows
updateBoard();
}
printBoard("After Step 2");
// Step 3
for(int col=0; col<=k-2; col++){
int max_height = getMaxHeight();
int h = 0;
while(board[h][col]) h++;
while(h <= max_height){
ans.push_back({1, col});
for(int j=0; j<k; j++) board[h+j][col] = true;
h += k;
}
updateBoard();
}
printBoard("After Step 3");
// Step 4
{
int maxHeight = getMaxHeight();
vector<int> height (n);
for(int col=0; col<n; col++){
for(int row=0; row<=maxHeight; row++) if(board[row][col]) height[col] = row;
}
int p = n%k;
for(int i=p; i<k; i++) assert(height[i] % k == 0);
int maxInterest = *max_element(height.begin() + p, height.begin() + k);
for(int i=0; i<k; i++){
while(height[i] < maxInterest){
ans.push_back({1, i});
for(int j=1; j<=k; j++) board[height[i]+j][i] = true;
height[i] += k;
}
}
updateBoard();
}
printBoard("After Step 4");
// Step 5
{
int maxHeight = getMaxHeight();
vector<int> height (n);
for(int col=0; col<n; col++){
for(int row=0; row<=maxHeight; row++) if(board[row][col]) height[col] = row;
}
int p = n%k;
int maxInterest = *max_element(height.begin(), height.begin() + p);
for(int i=0; i<p; i++){
while(height[i] < maxInterest){
ans.push_back({1, i});
for(int j=1; j<=k; j++) board[height[i]+j][i] = true;
height[i] += k;
}
}
updateBoard();
for(int i=0; i<p; i++) assert(height[i] == maxInterest);
for(int i=p; i<k; i++) assert(height[i] == 0);
for(int turns=0; turns<maxInterest; turns++){
for(int s=p; s<n; s+=k){
ans.push_back({2, s});
for(int j=0; j<k; j++) board[turns][s+j] = true;
}
}
updateBoard();
}
printBoard("After Step 5");
assert(getMaxHeight() == -1);
// Print
cout << ans.size() << "\n";
for(auto &p: ans){
p.second++;
cout << p.first << " " << p.second << "\n";
}
}