#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 1e2 + 7;
char a[N][N], prov[N][N];
int s[N*N*N], numS = 0;

int main()
{
    ios_base::sync_with_stdio(false);
    int n, m, kol = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            //cout << i << ' ' << j << endl;
            cin >> a[i][j];
            if (a[i][j] == '*') {
                kol++;
            }
            prov[i][j] = '.';
        }
    }
    /*for (int i = 1; i <= n; ++i) {
        cout << endl;
        for (int j = 1; j <= m; ++j) {
            cout << a[i][j];
        }
    }*/
    if (kol == 0) {
        printf("0");
        return 0;
    }
    if (kol < 5 && kol > 0) {
        printf("-1");
        return 0;
    }
    for (int i = 2; i < n; ++i) {
        for (int j = 2; j < m; ++j) {
            if (a[i][j] == '*') {
                int v = i - 1, n = i + 1,
                    p = j + 1, l = j - 1,
                    razmv = 0, razmn = 0,
                    razmp = 0, razml = 0,
                    razzv;
                while (a[v][j] == '*') {
                    v--, razmv++;
                }
                while (a[n][j] == '*') {
                    n++, razmn++;
                }
                while (a[i][p] == '*') {
                    p++, razmp++;
                }
                while (a[i][l] == '*') {
                    l--, razml++;
                }
                razzv = min({razmv, razml, razmn, razmp});
                if (razzv == 0) {
                    continue;
                }
                s[numS++] = i, s[numS++] = j, s[numS++] = razzv;
            }
        }
    }
    for (int w = 0; w < numS; w += 3) {
        int i = s[w], j = s[w + 1], razzv = s[w + 2];
        prov[i][j] = '*';
        int n = razzv, coor = i + 1;
        while (n > 0) {
            prov[coor++][j] = '*';
            n--;
        }
        n = razzv, coor = i - 1;
        while (n > 0) {
            prov[coor--][j] = '*';
            n--;
        }
        n = razzv, coor = j + 1;
        while (n > 0) {
            prov[i][coor++] = '*';
            n--;
        }
        n = razzv, coor = j - 1;
        while (n > 0) {
            prov[i][coor--] = '*';
            n--;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (a[i][j] != prov[i][j]) {
                printf("-1");
                return 0;
            }
        }
    }
    cout << (numS - 1) / 3 + 1 << endl;
    for (int i = 0; i < numS; i += 3) {
        cout << s[i] << ' ' << s[i + 1] << ' ' << s[i + 2] << endl;
    }

    return 0;
}


#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#define LMT 102
#define eps 1e8
using namespace std;
bool gra[LMT][LMT];
int l,r1,r2,value,wid,mid;
deque<int>noq;
int vn;
int abs(int x) {
	return x>0?x:-x;
}
void solve(int vv,int i,int rr1,int rr2) {
	if(value>=vv) {
		value=vv;
		l=i;
		r1=rr1;
		r2=rr2;
	}
}
int main() {
	int n,k;
	scanf("%d%d",&n,&k);
	mid=(k+1)>>1;
	memset(gra,1,sizeof(gra));
	while(n--) {
		scanf("%d",&wid);
		value=eps;
		for(int i=k; i>=1; i--) {
			noq.clear();
			vn=0;
			if(k<wid)break;
			for(int j=k; j>k-wid; j--) {
				if(!gra[i][j])noq.push_back(j);
				vn+=abs(i-mid)+abs(j-mid);
			}

			if(noq.empty())solve(vn,i,k-wid+1,k);
			for(int j=k-wid; j>=1; j--) {
				while(!noq.empty()&&(*noq.begin())-wid>=j)noq.pop_front();
				if(!gra[i][j])noq.push_back(j);
				vn=vn-(abs(i-mid)+abs(j+wid-mid))+abs(i-mid)+abs(j-mid);
				if(noq.empty())solve(vn,i,j,j+wid-1);
			}
		}
		if(value<eps) {
			printf("%d %d %d\n",l,r1,r2);
			for(int i=r1; i<=r2; i++)gra[l][i]=0;
		} else printf("-1\n");
	}
	return 0;
}


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

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}

bool a[110][110];

int main() {
    int n=read(),k=read();
    int xc=(k+1)/2,yc=(k+1)/2;
    while (n--) {
        int x=read();
        int X,Y,Min=2147483647;
        for (re int i=1;i<=k;i++)
            for (re int j=1;j<=k-x+1;j++) {
                bool f=1; int s=0;
                for (re int t=j;t<j+x;t++) {
                    if (a[i][t]) { f=0; break; }
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if (Min==2147483647) puts("-1");
        else {
            for (re int i=Y;i<Y+x;i++) a[X][i]=1;
            printf("%d %d %d\n",X,Y,Y+x-1);
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
#define re register
using namespace std;
const int N=101;
const int INF=2147483647;
bool a[N][N];
int main()
{
    int n,k;
	cin>>n>>k;
	int xc=ceil(double(k/2.0)),yc=ceil(double(k/2.0));
    while(n--)
	{
        int x;
		cin>>x;
        int X,Y,Min=INF;
        for(int i=1;i<=k;i++)
            for(int j=1;j<=k-x+1;j++)
			{
                bool f=1;
				int s=0;
                for(register int t=j;t<j+x;t++)
				{
                    if(a[i][t])
					{
						f=0;
						break;
					}
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if (Min==INF)cout<<"-1\n";
        else {
            for (re int i=Y;i<Y+x;i++) a[X][i]=1;
			cout<<X<<' '<<Y<<' '<<Y+x-1<<' '<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

const int MAX_K = 100;
const int INF = 1000000;
int seats[MAX_K][MAX_K];
bool used[MAX_K][MAX_K];

int main()
{
    int n, k;
    cin >> n >> k;

    //中心位置
    int cx, cy;
    cx = cy = (k + 1) / 2;

    //填充距离
    for (int i = 1 ; i <= k ; i++){
        for (int j = 1 ; j <= k ; j++){
            seats[i][j] = abs(cx - i) + abs(cy - j);
        }
    }

    for (int i = 0 ; i < n ; i++){
        int m;
        cin >> m;

        int sum = INF;
        int tx = -1;
        int ty = -1;
        for (int j = 1 ; j <= k; j++){
            for (int l = 1 ; l <= k - m + 1; l++){
                if (used[j][l]){
                    continue;
                }

                int temp = seats[j][l];
                for (int t = 1 ; t < m ; t++){
                    if (used[j][l + t]){
                        temp = INF;
                        break;
                    }else{
                        temp += seats[j][l + t];
                        if (temp > sum){
                            temp = INF;
                            break;
                        }
                    }
                }

                if (temp != INF && temp < sum){
                    sum = min(sum, temp);
                    tx = j;
                    ty = l;
                }
            }
        }

        if (sum == INF){
            cout << -1 << endl;
        }else{
            cout << tx << " " << ty << " " << ty + m - 1<< endl;
            for (int j = 0; j < m ; j++){
                used[tx][ty + j] = true;
            }
        }
    }
}


#include <iostream>
#include <cmath>
#include <vector>
#include <unordered_map>
using namespace std;
int c = 0;

int dist(int x, int yl, int yr) {
    int dist = (yr - yl + 1) * abs(x - c);
    for (int y = yl; y <= yr; y++) {
        dist += abs(y - c);
    }
    return dist;
}

int main() {
    int n, k;
    cin>>n>>k;
    c = (k + 1) / 2;
    vector<int> requests;
    for (int i = 0; i < n; i++) {
        int r;
        cin>>r;
        requests.push_back(r);
    }

    vector<int> rows;
    rows.push_back((k + 1) / 2);
    for (int i = 1; i < (k + 1) / 2; i++) {
        rows.push_back((k + 1) / 2 - i);
        rows.push_back((k + 1) / 2 + i);
    }

    unordered_map<int, vector<int>> seats;
    for (int row : rows) {
        seats[row].push_back(k);
    }

    for (int req : requests) {
        if (req > k) {
            cout<<-1<<endl;
            continue;
        }
        int min_dist = -1, x = 0, yl = 0, yr = 0;
        vector<int> seat;
        for (int row : rows) {
            if (seats[row].size() == 1) {
                int _yl = (k - req) / 2 + 1;
                int _yr = _yl + req - 1;
                int d = dist(row, _yl, _yr);
                if (min_dist == -1 || min_dist > d || (min_dist == d && row < x)) {
                    x = row;
                    yl = _yl;
                    yr = _yr;
                    min_dist = d;
                    seat.clear();
                    seat.push_back(yl - 1);
                    seat.push_back(k - yr);
                }
            } else {
                if (seats[row][0] >= req) {
                    int _yr = seats[row][0];
                    int _yl = _yr - req + 1;
                    int d = dist(row, _yl, _yr);
                    if (min_dist == -1 || min_dist > d || (min_dist == d && row < x)) {
                        x = row;
                        yl = _yl;
                        yr = _yr;
                        min_dist = d;
                        seat = seats[row];
                        seat[0] -= req;
                    }
                } else if (seats[row][1] >= req) {
                    int _yl = k - seats[row][1] + 1;
                    int _yr = _yl + req - 1;
                    int d = dist(row, _yl, _yr);
                    if (min_dist == -1 || min_dist > d || (min_dist == d && row < x)) {
                        x = row;
                        yl = _yl;
                        yr = _yr;
                        min_dist = d;
                        seat = seats[row];
                        seat[1] -= req;
                    }
                }
            }
        }
        if (min_dist == -1) {
            cout<<-1<<endl;
        } else {
            cout<<x<<" "<<yl<<" "<<yr<<endl;
            seats[x] = seat;
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <unordered_map>
using namespace std;
int c = 0;

int dist(int x, int yl, int yr) {
    int dist = (yr - yl + 1) * abs(x - c);
    for (int y = yl; y <= yr; y++) {
        dist += abs(y - c);
    }
    return dist;
}

int main() {
    int n, k;
    cin>>n>>k;
    c = (k + 1) / 2;
    vector<int> requests;
    for (int i = 0; i < n; i++) {
        int r;
        cin>>r;
        requests.push_back(r);
    }

    vector<int> rows;
    rows.push_back((k + 1) / 2);
    for (int i = 1; i < (k + 1) / 2; i++) {
        rows.push_back((k + 1) / 2 - i);
        rows.push_back((k + 1) / 2 + i);
    }

    unordered_map<int, vector<int>> seats;
    for (int row : rows) {
        seats[row].push_back(k);
    }

    for (int req : requests) {
        if (req > k) {
            cout<<-1<<endl;
            continue;
        }
        int min_dist = -1, x = 0, yl = 0, yr = 0;
        vector<int> seat;
        for (int row : rows) {
            if (seats[row].size() == 1) {
                int _yl = (k - req) / 2 + 1;
                int _yr = _yl + req - 1;
                int d = dist(row, _yl, _yr);
                if (min_dist == -1 || min_dist > d || (min_dist == d && row < x)) {
                    x = row;
                    yl = _yl;
                    yr = _yr;
                    min_dist = d;
                    seat.clear();
                    seat.push_back(yl - 1);
                    seat.push_back(k - yr);
                }
            } else {
                if (seats[row][0] >= req) {
                    int _yr = seats[row][0];
                    int _yl = _yr - req + 1;
                    int d = dist(row, _yl, _yr);
                    if (min_dist == -1 || min_dist > d || (min_dist == d && row < x)) {
                        x = row;
                        yl = _yl;
                        yr = _yr;
                        min_dist = d;
                        seat = seats[row];
                        seat[0] -= req;
                    }
                }
                if (seats[row][1] >= req) {
                    int _yl = k - seats[row][1] + 1;
                    int _yr = _yl + req - 1;
                    int d = dist(row, _yl, _yr);
                    if (min_dist == -1 || min_dist > d || (min_dist == d && row < x)) {
                        x = row;
                        yl = _yl;
                        yr = _yr;
                        min_dist = d;
                        seat = seats[row];
                        seat[1] -= req;
                    }
                }
            }
        }
        if (min_dist == -1) {
            cout<<-1<<endl;
        } else {
            cout<<x<<" "<<yl<<" "<<yr<<endl;
            seats[x] = seat;
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <unordered_map>
using namespace std;
int c = 0;

int dist(int x, int yl, int yr) {
    int dist = (yr - yl + 1) * abs(x - c);
    for (int y = yl; y <= yr; y++) {
        dist += abs(y - c);
    }
    return dist;
}

int main() {
    int n, k;
    cin>>n>>k;
    c = (k + 1) / 2;
    vector<int> requests;
    for (int i = 0; i < n; i++) {
        int r;
        cin>>r;
        requests.push_back(r);
    }

    vector<int> rows;
    rows.push_back((k + 1) / 2);
    for (int i = 1; i < (k + 1) / 2; i++) {
        rows.push_back((k + 1) / 2 - i);
        rows.push_back((k + 1) / 2 + i);
    }

    unordered_map<int, vector<int>> seats;
    for (int row : rows) {
        seats[row].push_back(k);
    }

    for (int req : requests) {
        if (req > k) {
            cout<<-1<<endl;
            continue;
        }
        int min_dist = -1, x = 0, yl = 0, yr = 0;
        vector<int> seat;
        for (int row : rows) {
            if (seats[row].size() == 1) {
                int _yl = (k - req) / 2 + 1;
                int _yr = _yl + req - 1;
                int d = dist(row, _yl, _yr);
                if (min_dist == -1 || min_dist > d) {
                    x = row;
                    yl = _yl;
                    yr = _yr;
                    min_dist = d;
                    seat.clear();
                    seat.push_back(yl - 1);
                    seat.push_back(k - yr);
                }
            } else {
                if (seats[row][0] >= req) {
                    int _yr = seats[row][0];
                    int _yl = _yr - req + 1;
                    int d = dist(row, _yl, _yr);
                    if (min_dist == -1 || min_dist > d) {
                        x = row;
                        yl = _yl;
                        yr = _yr;
                        min_dist = d;
                        seat = seats[row];
                        seat[0] -= req;
                    }
                } else if (seats[row][1] >= req) {
                    int _yl = k - seats[row][1] + 1;
                    int _yr = _yl + req - 1;
                    int d = dist(row, _yl, _yr);
                    if (min_dist == -1 || min_dist > d) {
                        x = row;
                        yl = _yl;
                        yr = _yr;
                        min_dist = d;
                        seat = seats[row];
                        seat[1] -= req;
                    }
                }
            }
        }
        if (min_dist == -1) {
            cout<<-1<<endl;
        } else {
            cout<<x<<" "<<yl<<" "<<yr<<endl;
            seats[x] = seat;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e2+10;
int n,m,k;
int mp[maxn][maxn],a[maxn];
bool v[maxn][maxn];

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            mp[i][j]=abs(i-(k+1)/2)+abs(j-(k+1)/2);
//            cout<<mp[i][j]<<" ";
        }
//        cout<<endl;
    }
//    cout<<endl;
    memset(v,0,sizeof(v));
    int tt,cnt,l,r,sum1,sum;
    for(int x=1; x<=n; x++)
    {
        sum=inf;
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                cnt=0;sum1=0;
                for(int j1=j;j1<=k;j1++){
                    if(v[i][j1]==0){
                        cnt++;sum1+=mp[i][j1];
                        if(cnt==a[x]){
                            if(sum>sum1){
                                sum=sum1;
                                tt=i;
                                r=j1;
                                l=r-a[x]+1;
//                                cout<<tt<<" "<<l<<" "<<r<<endl;
                            }
                            sum1=0;
                            cnt=0;
                            break;
                        }
                        if(cnt>a[x]){
                            break;
                        }
                    }
                    else break;
                }
            }
        }
        if(sum==inf)
        {
            printf("-1\n");
        }
        else
        {
            for(int y=r; y>=l; y--)
            {
                v[tt][y]=1;
            }
            printf("%d %d %d\n",tt,l,r);
        }
//        for(int i=1;i<=k;i++){
//            for(int j=1;j<=k;j++){
//                cout<<v[i][j]<<" ";
//            }
//            cout<<endl;
//        }
//        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e2+10;
int n,m,k;
int mp[maxn][maxn],a[maxn];
bool v[maxn][maxn];

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            mp[i][j]=abs(i-k/2)+abs(j-k/2);
//            cout<<mp[i][j]<<" ";
        }
//        cout<<endl;
    }
//    cout<<endl;
    memset(v,0,sizeof(v));
    int tt,cnt,l,r,sum1,sum;
    for(int x=1; x<=n; x++)
    {
        bool flag=0;
        for(int i=1; i<=k; i++)
        {
            cnt=0;sum1=0;sum=inf;
            for(int j=1; j<=k; j++)
            {
                if(v[i][j]==0){
                    cnt++;
                    sum1+=mp[i][j];
                    if(cnt==a[x]&&sum>sum1){
                        sum=sum1;
                        tt=i;
                        r=j;
                        l=r-a[x]+1;
                        for(int y=r;y>=l;y--){
                            v[i][y]=1;
                        }
                        flag=1;
                        break;
                    }
                }
            }
            if(flag)break;
        }
        if(sum==inf){
            printf("-1\n");
        }
        else printf("%d %d %d\n",tt,l,r);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e2+10;
int n,m,k;
int mp[maxn][maxn],a[maxn];
bool v[maxn][maxn];

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            mp[i][j]=abs(i-(k+1)/2)+abs(j-(k+1)/2);
//            cout<<mp[i][j]<<" ";
        }
//        cout<<endl;
    }
//    cout<<endl;
    memset(v,0,sizeof(v));
    int tt,cnt,l,r,sum1,sum;
    for(int x=1; x<=n; x++)
    {
        bool flag=0;
        cnt=0;
        sum1=0;
        sum=inf;
        for(int i=1; i<=k; i++)
        {
            cnt=0;sum1=0;
            for(int j=1; j<=k; j++)
            {
                if(v[i][j]==0)
                {
                    cnt++;
                    sum1+=mp[i][j];
                    if(cnt==a[x]&&sum>sum1)
                    {
                        sum=sum1;
                        tt=i;
                        r=j;
                        l=r-a[x]+1;
                        cnt=0;
                        sum1=0;
                    }
                    else if(sum1>=sum){
                        cnt=0;sum1=0;
                    }

                }
                else {
                    cnt=0;sum1=0;
                }
            }
        }
        if(sum==inf)
        {
            printf("-1\n");
        }
        else
        {
            for(int y=r; y>=l; y--)
            {
                v[tt][y]=1;
            }
            printf("%d %d %d\n",tt,l,r);
        }
//        for(int i=1;i<=k;i++){
//            for(int j=1;j<=k;j++){
//                cout<<v[i][j]<<" ";
//            }
//            cout<<endl;
//        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e2+10;
const int maxm=1e3+10;
int n,m,k;
int mp[maxn][maxn],a[maxm];
bool v[maxn][maxn];

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            mp[i][j]=abs(i-(k+1)/2)+abs(j-(k+1)/2);
//            cout<<mp[i][j]<<" ";
        }
//        cout<<endl;
    }
//    cout<<endl;
    memset(v,0,sizeof(v));
    int tt,cnt,l,r,sum1,sum;
    for(int x=1; x<=n; x++)
    {
        sum=inf;
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                cnt=0;sum1=0;
                for(int j1=j;j1<=k;j1++){
                    if(v[i][j1]==0){
                        cnt++;sum1+=mp[i][j1];
                        if(cnt==a[x]){
                            if(sum>sum1){
                                sum=sum1;
                                tt=i;
                                r=j1;
                                l=r-a[x]+1;
//                                cout<<tt<<" "<<l<<" "<<r<<endl;
                            }
                            sum1=0;
                            cnt=0;
                            break;
                        }
                        if(cnt>a[x]){
                            break;
                        }
                    }
                    else break;
                }
            }
        }
        if(sum==inf)
        {
            printf("-1\n");
        }
        else
        {
            for(int y=r; y>=l; y--)
            {
                v[tt][y]=1;
            }
            printf("%d %d %d\n",tt,l,r);
        }
//        for(int i=1;i<=k;i++){
//            for(int j=1;j<=k;j++){
//                cout<<v[i][j]<<" ";
//            }
//            cout<<endl;
//        }
//        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define re register
using namespace std;
inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}
bool a[110][110];
int main() {
    int n=read(),k=read();
    int xc=(k+1)/2,yc=(k+1)/2;
    while (n--) {
        int x=read();
        int X,Y,Min=2147483647;
        for (re int i=1;i<=k;i++)
            for (re int j=1;j<=k-x+1;j++) {
                bool f=1; int s=0;
                for (re int t=j;t<j+x;t++) {
                    if (a[i][t]) { f=0; break; }
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if(Min==2147483647) puts("-1");
        else{
            for(int i=Y;i<Y+x;i++) a[X][i]=1;
            printf("%d %d %d\n",X,Y,Y+x-1);
        }
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,k,gra[105][105],x[1005];
int r,c;
int cost(int len,int sx,int sy)//花费
{
	int mid=k-k/2;
	int bg=sy-len+1,ans=0;
	for(int i=bg;i<=sy;++i)
	{
		ans+=abs(mid-i);
	}
	return ans+len*abs(sx-mid);
}
void slove(int len)
{
	for(int i=1;i<=k;++i)
	{
		int cnt=0;
		for(int j=1;j<=k;++j)
		{
			if(!gra[i][j])
			{
				++cnt;
				if(cnt<len)
				{
					continue;
				}
				if(c==-1||cost(len,i,j)<cost(len,r,c))
				{
					r=i;c=j;
				}
				else if(cost(len,i,j)==cost(len,r,c))
				{
					if(i<r||i==r&&j<c)
					{
						r=i;c=j;
					}
				}
			}
			else
			{
				cnt=0;
			}
		}
	}
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;++i)
	{
		scanf("%d",&x[i]);
	}
	for(int i=0;i<n;++i)
	{
		r=c=-1;
		slove(x[i]);
		if(r!=-1&&c!=-1)
		{
			printf("%d %d %d\n",r,c-x[i]+1,c);
			for(int j=c-x[i]+1;j<=c;++j)
			{
				gra[r][j]=1;
			}
		}
		else
		{
			printf("-1\n");
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#define ll long long
#define maxn 200001
#define inf 2147483647
#define mod 10003
#define eps 1e-6
#define pi acos(-1.0)
#define de(x) ((x)*(x))
using namespace std;
inline int read(){
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
    return x*f;
}
inline void put(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) put(x/10) ;
	putchar(x%10+48);
}
bool a[110][110];
signed main(){
	int n=read(),k=read();
	int xx=(k+1)/2,yy=(k+1)/2;
	while(n--){
		int x=read();
		int dx,dy,minn=inf;
		for(int i=1;i<=k;i++)
		for(int j=1;j<=k-x+1;j++){
			bool flag=true; int s=0;
			for(int kk=j;kk<j+x;kk++){
				if(a[i][kk]){flag=false;break;}
				s+=abs(i-xx)+abs(kk-yy);
			}
			if(flag&&s<minn) minn=s,dx=i,dy=j;
		}
		if(minn==inf) puts("-1");
		else{
			for(int i=dy;i<dy+x;i++) a[dx][i]=1;
			printf("%d %d %d\n",dx,dy,dy+x-1);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define reg register

using namespace std;

const int kMax = 1e3 + 10;
const int inf = 0x7f7f7f7f;

int seat[kMax][kMax];

int N, K;

int main() {
  scanf("%d%d", &N, &K);
  int x, xc, yc;
  xc = yc = ceil(K * 1.0 / 2);
  for (; N; N--) {
  	scanf("%d", &x);
  	int ox, oy, Min = inf;
  	for (reg int i = 1; i <= K; ++i)
  	  for (reg int j = 1; j + x - 1 <= K; ++j) {
  	  	int ok = 1, sum = 0;
  	  	for (reg int k = j; k <= j + x - 1; ++k) {
  	  	  if (seat[i][k]) { ok = 0; break; }
  	  	  sum += abs(i - xc) + abs(k - yc);
		}
		if (ok && sum < Min) Min = sum, ox = i, oy = j;
	  }
	if (Min == inf) {
	  printf("-1\n");
	} else {
	  for (reg int i = oy; i <= oy + x - 1; ++i)
	    seat[ox][i] = 1;
	  printf("%d %d %d\n", ox, oy, oy + x - 1);
	}
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1005, K = 105;
int n, k, xc, yc;
int a[N];
bool vis[K][K];
int main(){
	memset(vis, 0, sizeof(vis));
	std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//	freopen("test_file/in.txt", "r", stdin);
//	freopen("test_file/out.out", "w", stdout);
	cin >> n >> k;
	xc = yc = (k+1)/2-1;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++){
		int ansx, ansy, mn=0x3f3f3f3f;
		for (int x = 0; x < k; x++){
			for (int y = 0; y <= k-a[i]; y++){
				bool ok = 1;
				int s = 0;
				for (int t = y; t < y+a[i]; t++){
					if (vis[x][t]){
						ok = 0;
						break;
					}
					s += abs(x-xc)+abs(t-yc);
				}
				if (ok && s < mn) ansx = x, ansy = y, mn = s;
			}
		}
		if (mn < 0x3f3f3f3f){
			for (int t = ansy; t < ansy+a[i]; t++) vis[ansx][t] = 1;
			cout << ansx+1 << ' ' << ansy+1 << ' ' << ansy+a[i] << endl;
		}else{
			cout << -1 << endl;
			break;
		}
	}
    return 0;
}


#include<stdio.h>
using namespace std;
const int INF=2147483647;
int a[105][105];
int hh(int k){
	if(k<0)return (-1)*k;
	return k;
}
int main(){
	int n,k,m,h,i,j,l,sum,sum1;
    scanf("%d%d",&n,&k);
    sum=sum1=(k+1)/2;
    for(m=1;m<=n;m++){
        scanf("%d",&h);
        int b1,b2,min=INF;
        for(i=1;i<=k;i++){
        	for(j=1;j<=k-h+1;j++) {
                int flag=1,ans=0;
				for(l=j;l<=j+h-1&&flag;l++) {
                    if(a[i][l])flag=0;
                    else ans=ans+hh(i-sum)+hh(l-sum1);
                }
                if(ans<min&&flag){
                	b1=i;b2=j;min=ans;
				}
            }
		}
        if(min==INF){
        	printf("-1\n");continue;
		}
		int s=b2+h-1;
		printf("%d %d %d\n",b1,b2,s);
    	for(i=b2;i<=s;i++)a[b1][i]=1;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
inline int read() {
	int X=0,w=1;
	char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
	while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
	return X*w;
}
bool a[110][110];
int main(){
	int n=read(),k=read();
	int xc=(k+1)/2,yc=(k+1)/2;
	while(n--){
		int x=read();
		int X,Y,Min=2147483647;
		for(int i=1;i<=k;i++)
			for(int j=1;j<=k-x+1;j++){
				bool f=1;int s=0;
				for(int t=j;t<j+x;t++){
					if(a[i][t]){f=0;break;}
					s+=abs(i-xc)+abs(t-yc);
				}
				if (f&&s<Min) Min=s,X=i,Y=j;
			}
		if(Min==2147483647)puts("-1");
		else{
			for(int i=Y;i<Y+x;i++)a[X][i]=1;
			printf("%d %d %d\n",X,Y,Y+x-1);
		}
	}
}


#include<cstdio>
#include<cmath>
#define in register int
#define rr read()
#define re register
#define il inline
#define end putchar('\n')
#define bl putchar(' ')
using namespace std;
inline bool isdigit(re char ch){return ch>='0'&&ch<='9';}
inline int read()
{
	re bool f=1;re char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(f=0);
	in x=ch-'0';while(isdigit(ch=getchar()))x=(x<<1)+(x<<3)+ch-'0';
	return f?x:-x;
}
inline void print(in x)
{
	if(x<0)putchar('-'),x=-x;
	if(x>=10)print(x/10);
	putchar(x%10+'0');
}
#define mm 100
#define min(x,y) (x<y?x:y)
bool ma[mm][mm];
int cost[mm][mm];
signed main()
{
	in n=rr,k=rr,mid=((k+1)>>1);
	for(in i=1;i<=k;i++)
		for(in j=1;j<=k;j++)
			cost[i][j]=abs(i-mid)+abs(j-mid);
	while(n--)
	{
		in x=rr,minn=1<<30,where1=0,where2=0;
		for(in j=1;j<=k;j++)
		{
			for(in p=1;p+x<=k+1;p++)
			{
				in alcos=0,num=0;
				re bool f=1;
				for(in q=p;q<=k&&num<x;q++)
				{
					if(ma[j][q])continue;
					alcos+=cost[j][q];
					num++;
					f=0;
				}
				if(!f&&alcos<minn&&num==x)minn=alcos,where2=p,where1=j;
			}
		}
		if(!where1)print(-1),end;
		else
		{
			for(in j=where2;j+x<=k+1;j++)
				ma[where1][j]=1;
			print(where1),bl,print(where2),bl,print(where2+x-1),end;
		}
	}
	return 0;
}


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

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}

bool a[110][110];

int main() {
    int n=read(),k=read();
    int xc=(k+1)/2,yc=(k+1)/2;
    while (n--) {
        int x=read();
        int X,Y,Min=2147483647;
        for (re int i=1;i<=k;i++)
            for (re int j=1;j<=k-x+1;j++) {
                bool f=1; int s=0;
                for (re int t=j;t<j+x;t++) {
                    if (a[i][t]) { f=0; break; }
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if (Min==2147483647) puts("-1");
        else {
            for (re int i=Y;i<Y+x;i++) a[X][i]=1;
            printf("%d %d %d\n",X,Y,Y+x-1);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

bool a[110][110];

int main()
{
	int n,k;
	cin>>n>>k;
    int xc = (k + 1)/2,yc = (k +1)/2;
    while(n--)
    {
    	int x; cin>>x;
    	int X,Y,Min = 9999999999;
    	for(int i = 1; i <= k ; i ++)
    	  for(int j = 1; j <= k - x +1; j ++)
    	  {
    	  	bool f = 1; int s = 0;
    	  	for(int t = j; t < j + x; t ++)
    	  	{
    	  		if(a[i][t]){ f = 0; break;}
    	  		s += abs(i - xc) + abs(t - yc);
			}
			if(f && s < Min) Min = s,X = i , Y = j;
		  }

		if(Min == 9999999999) puts("-1");
		else {
			for(int i = Y; i < Y + x; i ++) a[X][i] = 1;
			printf("%d %d %d\n", X,Y,Y + x - 1);
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define re register
using namespace std;
inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}
bool a[105][105];
int main() {
    int n=read(),k=read();
    int xc=(k+1)/2,yc=(k+1)/2;
    while (n--) {
        int x=read();
        int X,Y,Min=2147483647;
        for (re int i=1;i<=k;i++)
            for (re int j=1;j<=k-x+1;j++) {
                bool f=1; int s=0;
                for (re int t=j;t<j+x;t++) {
                    if (a[i][t]) { f=0; break; }
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if (Min==2147483647) puts("-1");
        else {
            for (re int i=Y;i<Y+x;i++) a[X][i]=1;
            printf("%d %d %d\n",X,Y,Y+x-1);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool a[110][110];
int main() {
	int n,k;
	cin>>n>>k;
	int xc=(k+1)/2,yc=(k+1)/2;
	while (n--) {
		int x;
		cin>>x;
		int x1,y1,Min=0x3f3f3f3f;
		for (int i=1;i<=k;i++)
			for (int j=1;j<=k-x+1;j++) {
				bool f=1; int s=0;
				for (int t=j;t<j+x;t++) {
					if (a[i][t]) { f=0; break; }
					s+=abs(i-xc)+abs(t-yc);
				}
				if (f&&s<Min) Min=s,x1=i,y1=j;
			}
		if (Min==0x3f3f3f3f) cout<<-1;
		else {
			for (int i=y1;i<y1+x;i++) a[x1][i]=1;
			cout<<x1<<" "<<y1<<" "<<y1+x-1<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1005, K = 105;
int n, k, xc, yc;
int a[N];
bool vis[K][K];
int main(){
	memset(vis, 0, sizeof(vis));
	std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	freopen("test_file/in.txt", "r", stdin);
//	freopen("test_file/out.out", "w", stdout);
	cin >> n >> k;
	xc = yc = k/2;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++){
		int ansx, ansy, mn=0x3f3f3f3f;
		for (int x = 0; x < k; x++){
			for (int y = 0; y < k-a[i]+1; y++){
				bool ok = 1;
				int s = 0;
				for (int t = y; t < y+a[i]; t++){
					if (vis[x][t]){
						ok = 0;
						break;
					}
					s += abs(x-xc)+abs(t-yc);
				}
				if (ok && s < mn) ansx = x, ansy = y, mn = s;
			}
		}
		if (mn < 0x3f3f3f3f){
			for (int t = ansy; t < ansy+a[i]; t++) vis[ansx][t] = 1;
			cout << ansx+1 << ' ' << ansy+1 << ' ' << ansy+a[i] << endl;
		}else{
			cout << -1 << endl;
			break;
		}
	}
    return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <numeric>

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

const int INF = 1e9;
bool v[100][100];
int xc, yc, N, K, a;

int wt(int x, int y0, int y1)
{
	int f=0;
	for(int i=y0;i<=y1;++i)
	{
		if(v[x][i]) return INF;
		f += std::abs(x-xc)+std::abs(i-yc);
	}
	return f;
}

int main()
{
	scanf("%d%d", &N, &K);
	xc=K+1>>1, yc=xc;
	for(int i=0;i<N;++i)
	{
		scanf("%d", &a);
		--a;
		int x=-1, y0=-1, y1=-1, w=INF;
		for(int j=1;j<=K;++j)
		{
			for(int k=1;k+a<=K;++k)
				if(ckmin(w, wt(j, k, k+a)))
					x=j, y0=k, y1=k+a;
		}
		if(~x)
			std::fill(v[x]+y0, v[x]+y1+1, true), printf("%d %d %d\n", x, y0, y1);
		else
			printf("-1\n");
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
#define endl "\n"
#define int long long
#define mp make_pair
inline int in() {int32_t x ; scanf("%d",&x) ; return x ; }
inline char getCh() {char ch ; scanf(" %c",&ch) ; return ch ; }
inline string getStr() {char ch[20] ; scanf("%s",ch) ; return ch ;}
const int MAX_N = 1e3 + 1;
const int MAX_K = 105;
int m[MAX_N];
int psum[MAX_K][MAX_K];
int n , k;
inline int dist(int x , int y , int x2 , int y2)
{
    return abs(x - x2) + abs(y - y2);
}
inline int getNum(int x , int y)
{
    return x*k + y + 1;
}
bool vis[MAX_K][MAX_K];
vector<pair<int , int>> vc[MAX_K];
int32_t main()
{
    n = in() , k = in();
    for (int i = 0 ; i < n ; i ++)
        m[i] = in();
    int centerX = (k + 1) / 2;
    int centerY = (k + 1) / 2;
    for (int r = 0 ; r < k ; r ++)
    {
        for (int c = 0 ; c < k ; c ++)
        {
            psum[r][c] = dist(centerX , centerY , r + 1 , c + 1) + (c ? psum[r][c-1] : 0);
        }
    }
    for (int i = 0 ; i < n ; i ++)
    {
        int res = 1e9;
        int x = -1;
        int y;
        for (int row = 0 ; row < k ; row ++)
        {
            for (int j = 0 ; j + m[i] - 1 < k ; j ++){
                int en = j + m[i] - 1;
                bool fl = true;
                for (int pt = j ; pt <= en ; pt ++)
                {
                    if (vis[row][pt])
                    {
                        fl = false;
                        break;
                    }
                }
                if (!fl) continue;
                if ((psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ) < res )){
                    res = (psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ));
                    x = row;
                    y = j;
                }
            }
        }
        if (x == -1)
        {
            cout << x << "\n";
            continue;
        }
        cout << x + 1 << " "  << y + 1 << " " << y+m[i] << "\n";
        for (int j = y ; j <= y + m[i] - 1 ; j ++)
            vis[x][j] = true;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
#define endl "\n"
#define int long long
#define mp make_pair
inline int in() {int32_t x ; scanf("%d",&x) ; return x ; }
inline char getCh() {char ch ; scanf(" %c",&ch) ; return ch ; }
inline string getStr() {char ch[20] ; scanf("%s",ch) ; return ch ;}
const int MAX_N = 1e3 + 1;
const int MAX_K = 105;
int m[MAX_N];
int psum[MAX_K][MAX_K];
//int st[4*MAX_K*MAX_K];
//bool lazy[4*MAX_K*MAX_K];
int n , k;
inline int dist(int x , int y , int x2 , int y2)
{
    return abs(x - x2) + abs(y - y2);
}
/*
inline void push(int p , int l , int r)
{
    if (lazy[p] == 0) return;
    lazy[p] = 0;
    lazy[2*p + 1] = lazy[2*p + 2] = 1;
    int mid = (l + r) >> 1;
    st[2*p + 1] = mid - l + 1;
    st[2*p + 2] = r - mid;
    st[p] = r - l + 1;
}
inline int get(int p , int l , int r , int x , int y)
{
    if (l > y || r < x)
        return 0;
    if (l >= x && r <= y)
        return st[p];
    push(p , l , r);
    int mid = (l + r) >> 1;
    return get(2*p + 1 , l , mid , x , y) + get(2*p + 2 , mid + 1 , r , x , y);
}
inline void update(int p , int l , int r , int x , int y) // val = 1
{
    if (l > y || r < x)
        return ;
    if (l >= x && r <= y){
        lazy[p] = 1;
        st[p] = r - l + 1;
        return;
    }
    push(p , l , r);
    int mid = (l + r) >> 1;
    update(2*p + 1 , l , mid , x , y);
    update(2*p + 2 , mid + 1 , r , x , y);
    st[p] = st[2*p+1] + st[2*p + 2];
}
 */
inline int getNum(int x , int y)
{
    return x*k + y + 1;
}
vector<pair<int , int>> vc[MAX_K];
int32_t main()
{
    n = in() , k = in();
    for (int i = 0 ; i < n ; i ++)
        m[i] = in();
    int centerX = (k + 1) / 2;
    int centerY = (k + 1) / 2;
    for (int r = 0 ; r < k ; r ++)
    {
        for (int c = 0 ; c < k ; c ++)
        {
            psum[r][c] = dist(centerX , centerY , r + 1 , c + 1) + (c ? psum[r][c-1] : 0);
        }
    }
    for (int i = 0 ; i < n ; i ++)
    {
        int res = 1e9;
        int x = -1;
        int y;
        for (int row = 0 ; row < k ; row ++)
        {
            for (int j = 0 ; j + m[i] - 1 < k ; j ++){
                if ((psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ) < res )){
                    int Left = getNum(row , j);
                    int Right = getNum(row , j + m[i] - 1);
                if (upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1)  ) == vc[row].end() ||
                    vc[row][upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1) ) - vc[row].begin()].first > j + m[i] - 1)
                {

                    res = (psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ));
                    x = row;
                    y = j;
                }
                }
            }
        }
        if (x == -1)
        {
            cout << x << " ";
            continue;
        }
        cout << x + 1 << " "  <<y + 1 << " " << y+m[i] << "\n";
        int Left = getNum(x,y);
        int Right = getNum(x , y + m[i] - 1);
        vc[x].push_back({y,y + m[i] - 1});
        sort(vc[x].begin(),vc[x].end());
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
#define endl "\n"
#define int long long
#define mp make_pair
inline int in() {int32_t x ; scanf("%d",&x) ; return x ; }
inline char getCh() {char ch ; scanf(" %c",&ch) ; return ch ; }
inline string getStr() {char ch[20] ; scanf("%s",ch) ; return ch ;}
const int MAX_N = 1e3 + 1;
const int MAX_K = 105;
int m[MAX_N];
int psum[MAX_K][MAX_K];
//int st[4*MAX_K*MAX_K];
//bool lazy[4*MAX_K*MAX_K];
int n , k;
inline int dist(int x , int y , int x2 , int y2)
{
    return abs(x - x2) + abs(y - y2);
}
/*
inline void push(int p , int l , int r)
{
    if (lazy[p] == 0) return;
    lazy[p] = 0;
    lazy[2*p + 1] = lazy[2*p + 2] = 1;
    int mid = (l + r) >> 1;
    st[2*p + 1] = mid - l + 1;
    st[2*p + 2] = r - mid;
    st[p] = r - l + 1;
}
inline int get(int p , int l , int r , int x , int y)
{
    if (l > y || r < x)
        return 0;
    if (l >= x && r <= y)
        return st[p];
    push(p , l , r);
    int mid = (l + r) >> 1;
    return get(2*p + 1 , l , mid , x , y) + get(2*p + 2 , mid + 1 , r , x , y);
}
inline void update(int p , int l , int r , int x , int y) // val = 1
{
    if (l > y || r < x)
        return ;
    if (l >= x && r <= y){
        lazy[p] = 1;
        st[p] = r - l + 1;
        return;
    }
    push(p , l , r);
    int mid = (l + r) >> 1;
    update(2*p + 1 , l , mid , x , y);
    update(2*p + 2 , mid + 1 , r , x , y);
    st[p] = st[2*p+1] + st[2*p + 2];
}
 */
inline int getNum(int x , int y)
{
    return x*k + y + 1;
}
vector<pair<int , int>> vc[MAX_K];
int32_t main()
{
    n = in() , k = in();
    for (int i = 0 ; i < n ; i ++)
        m[i] = in();
    int centerX = (k + 1) / 2;
    int centerY = (k + 1) / 2;
    for (int r = 0 ; r < k ; r ++)
    {
        for (int c = 0 ; c < k ; c ++)
        {
            psum[r][c] = dist(centerX , centerY , r + 1 , c + 1) + (c ? psum[r][c-1] : 0);
        }
    }
    for (int i = 0 ; i < n ; i ++)
    {
        int res = 1e9;
        int x = -1;
        int y;
        for (int row = 0 ; row < k ; row ++)
        {
            for (int j = 0 ; j + m[i] - 1 < k ; j ++){
                if ((psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ) < res )){
                    int Left = getNum(row , j);
                    int Right = getNum(row , j + m[i] - 1);
                if (upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1)  ) == vc[row].end() ||
                    vc[row][upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1) ) - vc[row].begin()].first > j + m[i] - 1)
                {

                    res = (psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ));
                    x = row;
                    y = j;
                }
                }
            }
        }
        if (x == -1)
        {
            cout << x << " ";
            continue;
        }
        printf("%d %d %d\n",x + 1,y + 1,y+m[i]);

        int Left = getNum(x,y);
        int Right = getNum(x , y + m[i] - 1);
        vc[x].push_back({y,y + m[i] - 1});
        sort(vc[x].begin(),vc[x].end());
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
#define endl "\n"
#define int long long
#define mp make_pair
inline int in() {int32_t x ; scanf("%d",&x) ; return x ; }
inline char getCh() {char ch ; scanf(" %c",&ch) ; return ch ; }
inline string getStr() {char ch[20] ; scanf("%s",ch) ; return ch ;}
const int MAX_N = 1e3 + 1;
const int MAX_K = 105;
int m[MAX_N];
int psum[MAX_K][MAX_K];
int n , k;
inline int dist(int x , int y , int x2 , int y2)
{
    return abs(x - x2) + abs(y - y2);
}
inline int getNum(int x , int y)
{
    return x*k + y + 1;
}
vector<pair<int , int>> vc[MAX_K];
int32_t main()
{
    n = in() , k = in();
    for (int i = 0 ; i < n ; i ++)
        m[i] = in();
    int centerX = (k + 1) / 2;
    int centerY = (k + 1) / 2;
    for (int r = 0 ; r < k ; r ++)
    {
        for (int c = 0 ; c < k ; c ++)
        {
            psum[r][c] = dist(centerX , centerY , r + 1 , c + 1) + (c ? psum[r][c-1] : 0);
        }
    }
    for (int i = 0 ; i < n ; i ++)
    {
        int res = 1e9;
        int x = -1;
        int y;
        for (int row = 0 ; row < k ; row ++)
        {
            for (int j = 0 ; j + m[i] - 1 < k ; j ++){
                if ((psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ) < res )){
                    int Left = getNum(row , j);
                    int Right = getNum(row , j + m[i] - 1);
                if (upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1)  ) == vc[row].end() ||
                    vc[row][upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1) ) - vc[row].begin()].first > j + m[i] - 1)
                {

                    res = (psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ));
                    x = row;
                    y = j;
                }
                }
            }
        }
        if (x == -1)
        {
            cout << x << "\n";
            continue;
        }
        cout << x + 1 << " "  <<y + 1 << " " << y+m[i] << "\n";
        int Left = getNum(x,y);
        int Right = getNum(x , y + m[i] - 1);
        vc[x].push_back({x,y + m[i] - 1});
        sort(vc[x].begin(),vc[x].end());
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
#define endl "\n"
#define int long long
#define mp make_pair
inline int in() {int32_t x ; scanf("%d",&x) ; return x ; }
inline char getCh() {char ch ; scanf(" %c",&ch) ; return ch ; }
inline string getStr() {char ch[20] ; scanf("%s",ch) ; return ch ;}
const int MAX_N = 1e3 + 1;
const int MAX_K = 105;
int m[MAX_N];
int psum[MAX_K][MAX_K];
int n , k;
inline int dist(int x , int y , int x2 , int y2)
{
    return abs(x - x2) + abs(y - y2);
}
inline int getNum(int x , int y)
{
    return x*k + y + 1;
}
vector<pair<int , int>> vc[MAX_K];
int32_t main()
{
    n = in() , k = in();
    for (int i = 0 ; i < n ; i ++)
        m[i] = in();
    int centerX = (k + 1) / 2;
    int centerY = (k + 1) / 2;
    for (int r = 0 ; r < k ; r ++)
    {
        for (int c = 0 ; c < k ; c ++)
        {
            psum[r][c] = dist(centerX , centerY , r + 1 , c + 1) + (c ? psum[r][c-1] : 0);
        }
    }
    for (int i = 0 ; i < n ; i ++)
    {
        int res = 1e9;
        int x = -1;
        int y;
        for (int row = 0 ; row < k ; row ++)
        {
            for (int j = 0 ; j + m[i] - 1 < k ; j ++){
                if ((psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ) < res )){
                    int Left = getNum(row , j);
                    int Right = getNum(row , j + m[i] - 1);
                if (upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1)  ) == vc[row].end() ||
                    vc[row][upper_bound(vc[row].begin(),vc[row].end(), mp(-1LL,j - 1) ) - vc[row].begin()].first > j + m[i] - 1)
                {

                    res = (psum[row][j + m[i] - 1] - (j ? psum[row][j-1] : 0 ));
                    x = row;
                    y = j;
                }
                }
            }
        }
        if (x == -1)
        {
            cout << x << " ";
            continue;
        }
        cout << x + 1 << " "  <<y + 1 << " " << y+m[i] << "\n";
        int Left = getNum(x,y);
        int Right = getNum(x , y + m[i] - 1);
        vc[x].push_back({x,y + m[i] - 1});
        sort(vc[x].begin(),vc[x].end());
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 7;
int a[111][110];
int n, k, m;

int aa( int x )
{
  return x < 0 ? -x : x;
}

int main()
{
  scanf( "%d %d", &n, &k );
  int c = k >> 1;
  forn( i,n )
  {
    scanf( "%d", &m );

    int ans = inf;
    int ansi = -1, ansj = -1;
    forn( i,k )
      forn( j,k-m+1 )
      {
        int cur = 0;
        bool ok = 1;
        forn( q,m )
          if ( a[i][j+q] == 1 )
          {
            ok = 0;
            break;
          }
          else
            cur += aa( i-c ) + aa( j+q-c );

        if ( ok && cur < ans )
        {
          ans = cur;
          ansi = i;
          ansj = j;
        }
      }

    if ( ans == inf ) printf( "-1\n" );
    else
    {
      printf( "%d %d %d\n", ansi+1, ansj+1, ansj+m );
      forn( q,m )
        a[ansi][ansj+q] = 1;
    }
  }
}


#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 7;
int a[111][110];
int n, k, m;

int aa( int x )
{
  return x < 0 ? -x : x;
}

int main()
{
  scanf( "%d %d", &n, &k );
  int c = k >> 1;
  for(int i=0;i<n;i++)
  {
    scanf( "%d", &m );

    int ans = 1e9;
    int ansi = -1, ansj = -1;
    for(int i=0;i<k;i++)
      for(int j=0;j<k-m+1;j++)
      {
        int cur = 0;
        bool ok = 1;
        for(int q=0;q<m;q++)
          if ( a[i][j+q] == 1 )
          {
            ok = 0;
            break;
          }
          else
            cur += aa( i-c ) + aa( j+q-c );

        if ( ok && cur < ans )
        {
          ans = cur;
          ansi = i;
          ansj = j;
        }
      }

    if ( ans == 1e9 ) printf( "-1\n" );
    else
    {
      printf( "%d %d %d\n", ansi+1, ansj+1, ansj+m );
      for(int q=0;q<m;q++)
        a[ansi][ansj+q] = 1;
    }
  }
}


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

int main() {
    int n, k, m;
    scanf("%d %d", &n, &k);

    int c = (k/2) + 1;

    bool a[k][k];
    memset(a, 0, sizeof(bool)*k*k);

    while (n--) {
        scanf("%d", &m);

        int best_row = -1, best_d = INT_MAX, best_idx = -1;
        for (int i = 0; i < k; i++) {

            for (int j = 0; j < k; j++) {
                int l = 0, d = 0;
                while ((j+l) < k && l < m && !a[i][j+l]) {
                    d += abs(i-c+1) + abs(j+l-c+1);
                    l++;
                }
                if (l != m) {
                    j += l;
                    continue;
                }

                if (d < best_d) {
                    best_d = d;
                    best_row = i;
                    best_idx = j;
                }
            }
        }

        if (best_row == -1 ) {
            printf("-1\n");
        } else {
            memset(a[best_row] + best_idx, 1, sizeof(bool)*m);
            printf("%d %d %d\n", best_row+1, best_idx+1, best_idx+m);
        }
    }

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int xc,yc;
inline int abs(int x)
{
    return (x>0)?x:-x;
}
int fun(int x,int y,int z,int w)
{
    int sum=abs(x-xc)*w;
    int i;
    for(i=y;i<=z;++i)
        sum += abs(i-yc);
    return sum;
}
int main()
{
    int num,k,i,j,m,n,h,val,best1,best2,best3,temp,mincost=9999999;
    cin>>num>>k;
    xc=yc=k/2;
    vector<int> order;
    vector<vector<bool> > sit(k,vector<bool>(k,false));
    for(i=0,j=k-1;i<j;++i,--j)
    {
        order.push_back(j);
        order.push_back(i);
    }
    order.push_back(i);
    for(i=0;i<num;++i)
    {
        cin>>val;
        for(m=k-1;m>=0;--m)
        {
            for(n=0;n<=k-val;++n)
            {
                for(h=n;h<n+val;++h)
                {
                    if(sit[order[m]][h])
                        break;
                }
                if(h==n+val)
                {
                    temp = fun(order[m],n,h-1,val);
                    if(temp<mincost)
                    {
                        mincost = temp;
                        best1 = order[m];
                        best2 = n;
                        best3 = h-1;
                    }
                    else if(temp==mincost)
                    {
                        if(order[m]<best1)
                        {
                            best1 = order[m];
                            best2 = n;
                            best3 = h-1;
                        }
                        else if(order[m]==best1)
                        {
                            if(n<best2)
                            {
                                best2 = n;
                                best3 = h-1;
                            }
                        }
                    }
                }
            }
        }
        if(mincost==9999999)
            cout<<-1<<endl;
        else
        {
            cout<<best1+1<<" "<<best2+1<<" "<<best3+1<<endl;
            mincost = 9999999;
            for(m=best2;m<=best3;++m)
                sit[best1][m] = true;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int xc,yc;
inline int abs(int x)
{
    return (x>0)?x:-x;
}
int fun(int x,int y,int z,int w)
{
    int sum=abs(x-xc)*w;
    int i;
    for(i=y;i<=z;++i)
        sum += abs(i-yc);
    return sum;
}
int main()
{
    int num,k,i,j,m,n,h,val,best1,best2,best3,temp,mincost=9999999;
    cin>>num>>k;
    xc=yc=k/2;
    vector<vector<bool> > sit(k,vector<bool>(k,false));
    for(i=0;i<num;++i)
    {
        cin>>val;
        for(m=0;m<k;++m)
        {
            for(n=0;n<=k-val;++n)
            {
                for(h=n;h<n+val;++h)
                {
                    if(sit[m][h])
                        break;
                }
                if(h==n+val)
                {
                    temp = fun(m,n,h-1,val);
                    if(temp<mincost)
                    {
                        mincost = temp;
                        best1 = m;
                        best2 = n;
                        best3 = h-1;
                    }
                    else if(temp==mincost)
                    {
                        if(m<best1)
                        {
                            best1 = m;
                            best2 = n;
                            best3 = h-1;
                        }
                        else if(m==best1)
                        {
                            if(n<best2)
                            {
                                best2 = n;
                                best3 = h-1;
                            }
                        }
                    }
                }
            }
        }
        if(mincost==9999999)
            cout<<-1<<endl;
        else
        {
            cout<<best1+1<<" "<<best2+1<<" "<<best3+1<<endl;
            mincost = 9999999;
            for(m=best2;m<=best3;++m)
                sit[best1][m] = true;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int xc,yc;
inline int abs(int x)
{
    return (x>0)?x:-x;
}
int fun(int x,int y,int z,int w)
{
    int sum=abs(x-xc)*w;
    int i;
    for(i=y;i<=z;++i)
        sum += abs(i-yc);
    return sum;
}
int main()
{
    int num,k,i,j,m,n,h,val,best1,best2,best3,temp,mincost=9999999;
    cin>>num>>k;
    xc=yc=k/2;
    vector<int> order;
    vector<vector<bool> > sit(k,vector<bool>(k,false));
    for(i=0,j=k-1;i<j;++i,--j)
    {
        order.push_back(j);
        order.push_back(i);
    }
    order.push_back(i);
    for(i=0;i<num;++i)
    {
        cin>>val;
        for(m=k-1;m>=0;--m)
        {
            for(n=0;n<=k-val;++n)
            {
                for(h=n;h<n+val;++h)
                {
                    if(sit[order[m]][h])
                        break;
                }
                if(h==n+val)
                {
                    temp = fun(order[m],n,h-1,val);
                    if(temp<mincost)
                    {
                        mincost = temp;
                        best1 = order[m];
                        best2 = n;
                        best3 = h-1;
                    }
                }
            }
        }
        if(mincost==9999999)
            cout<<-1<<endl;
        else
        {
            cout<<best1+1<<" "<<best2+1<<" "<<best3+1<<endl;
            mincost = 9999999;
            for(m=best2;m<=best3;++m)
                sit[best1][m] = true;
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

struct Row
{
    int head, tail;
} row[100];

int abs(int x)
{
    return x > 0 ? x : -x;
}

int sumy(int n)
{
    return (1 + n) * n / 2;
}

int main(void)
{
    int n, k;
    cin >> n >> k;
    int xc = k / 2 + 1, yc = k / 2 + 1;

    //init
    int for_row = xc + 1, bac_row = xc;

    unsigned int mini, sum;
    int i, m, rec_i, rec_head, rec_tail;
    while (n--)
    {
        //init
        cin >> m;
        mini = -1;
        sum = -1;
        //newline_1
        i = for_row - 1;
        if (i > 0 && m <= k)
            sum = sumy(m / 2) + sumy(m / 2 + m % 2 - 1) + abs(i - xc) * m;
        if (sum < mini)
        {
            mini = sum;
            rec_i = i;
            rec_head = yc - m / 2;
            rec_tail = yc + m / 2 + m % 2 - 1;
        }
        //oldline
        for (i = for_row; i <= bac_row; i++)
        {
            if (row[i].head - m > 0)
            {
                sum = (xc - i + row[i].head - yc) * m + sumy(m);
                if (sum < mini)
                {
                    mini = sum;
                    rec_i = i;
                    rec_head = row[i].head - m;
                    rec_tail = 0;
                }
            }
            if (row[i].tail + m <= k)
            {
                sum = (abs(xc - i) + row[i].tail - yc) * m + sumy(m);
                if (sum < mini)
                {
                    mini = sum;
                    rec_i = i;
                    rec_head = 0;
                    rec_tail = row[i].tail + m;
                }
            }
        }
        //newline_2
        i = bac_row + 1;
        if (i <= k && m <= k)
            sum = sumy(m / 2) + sumy(m / 2 + m % 2 - 1) + abs(i - xc) * m;
        if (sum < mini)
        {
            bac_row = i;
            row[i].head = yc - m / 2;
            row[i].tail = yc + m / 2 + m % 2 - 1;
            cout << i << ' ' << row[i].head << ' ' << row[i].tail << endl;
        }
        else
        {
            if (mini == (unsigned int)(-1))
            {
                cout << -1 << endl;
                continue;
            }

            if (rec_head > 0 && rec_tail > 0)
            {
                for_row = rec_i;
                row[rec_i].head = rec_head;
                row[rec_i].tail = rec_tail;
                cout << rec_i << ' ' << rec_head << ' ' << rec_tail << endl;
            }
            else if (rec_head)
            {
                cout << rec_i << ' ' << rec_head << ' ' << row[rec_i].head - 1 << endl;
                row[rec_i].head = rec_head;
            }
            else
            {
                cout << rec_i << ' ' << row[rec_i].tail + 1 << ' ' << rec_tail << endl;
                row[rec_i].tail = rec_tail;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

struct Row
{
    int head, tail;
} row[100];

int abs(int x)
{
    return x > 0 ? x : -x;
}

int sumy(int n)
{
    return (1 + n) * n / 2;
}

int main(void)
{
    int n, k;
    cin >> n >> k;
    int xc = k / 2 + 1, yc = k / 2 + 1;

    //init
    int for_row = xc + 1, bac_row = xc;

    unsigned int mini, sum;
    int i, m, rec_i, rec_head, rec_tail;
    while (n--)
    {
        //init
        cin >> m;
        mini = -1;
        sum = -1;
        //newline_1
        i = for_row - 1;
        if (i > 0 && m <= k)
            sum = sumy(m / 2) + sumy(m / 2 + m % 2 - 1) + abs(i - xc) * m;
        if (sum < mini)
        {
            mini = sum;
            rec_i = i;
            rec_head = yc - m / 2;
            rec_tail = yc + m / 2 + m % 2 - 1;
        }
        //oldline
        for (i = for_row; i <= bac_row; i++)
        {
            if (row[i].head - m > 0)
            {
                sum = (abs(xc - i) + abs(row[i].head - yc)) * m + sumy(m);
                if (sum < mini)
                {
                    mini = sum;
                    rec_i = i;
                    rec_head = row[i].head - m;
                    rec_tail = 0;
                }
            }
            if (row[i].tail + m <= k)
            {
                sum = (abs(xc - i) + abs(row[i].tail - yc)) * m + sumy(m);
                if (sum < mini)
                {
                    mini = sum;
                    rec_i = i;
                    rec_head = 0;
                    rec_tail = row[i].tail + m;
                }
            }
        }
        //newline_2
        i = bac_row + 1;
        if (i <= k && m <= k)
            sum = sumy(m / 2) + sumy(m / 2 + m % 2 - 1) + abs(i - xc) * m;
        if (sum < mini)
        {
            bac_row = i;
            row[i].head = yc - m / 2;
            row[i].tail = yc + m / 2 + m % 2 - 1;
            cout << i << ' ' << row[i].head << ' ' << row[i].tail << endl;
        }
        else
        {
            if (mini == (unsigned int)(-1))
            {
                cout << -1 << endl;
                continue;
            }

            if (rec_head > 0 && rec_tail > 0)
            {
                for_row = rec_i;
                row[rec_i].head = rec_head;
                row[rec_i].tail = rec_tail;
                cout << rec_i << ' ' << rec_head << ' ' << rec_tail << endl;
            }
            else if (rec_head)
            {
                cout << rec_i << ' ' << rec_head << ' ' << row[rec_i].head - 1 << endl;
                row[rec_i].head = rec_head;
            }
            else
            {
                cout << rec_i << ' ' << row[rec_i].tail + 1 << ' ' << rec_tail << endl;
                row[rec_i].tail = rec_tail;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

struct Row
{
    int head, tail;
} row[100];

int abs(int x)
{
    return x > 0 ? x : -x;
}

int sumy(int n)
{
    return (1 + n) * n / 2;
}

int main(void)
{
    int n, k;
    cin >> n >> k;
    int xc = k / 2 + 1, yc = k / 2 + 1;

    //init
    int for_row = xc + 1, bac_row = yc - 1;

    unsigned int maxi, sum;
    int i, m, rec_i, rec_head, rec_tail;
    while (n--)
    {
        //init
        cin >> m;
        maxi = -1;
        sum = -1;
        //oldline
        for (i = for_row; i <= bac_row; i++)
        {
            if (row[i].head - m > 0)
            {
                sum = (xc - i + row[i].head - yc) * m + sumy(m);
                if (sum < maxi)
                {
                    maxi = sum;
                    rec_i = i;
                    rec_head = row[i].head - m;
                    rec_tail = 0;
                }
            }
            if (row[i].tail + m <= k)
            {
                sum = (abs(xc - i) + row[i].tail - yc) * m + sumy(m);
                if (sum < maxi)
                {
                    maxi = sum;
                    rec_i = i;
                    rec_head = 0;
                    rec_tail = row[i].tail + m;
                }
            }
        }
        //newline
        i = (xc - for_row) > (bac_row - xc) ? (bac_row + 1) : (for_row - 1);
        if (i > 0 && i <= k)
            sum = sumy(m / 2) + sumy(m / 2 + m % 2 - 1) + abs(i - xc) * m;
        if (sum < maxi)
        {
            if (for_row > i)
                for_row = i;
            if (bac_row < i)
                bac_row = i;
            row[i].head = yc - m / 2;
            row[i].tail = yc + m / 2 + m % 2 - 1;
            cout << i << ' ' << row[i].head << ' ' << row[i].tail << endl;
        }
        else
        {
            if (maxi == (unsigned int)(-1))
            {
                cout << -1 << endl;
                continue;
            }

            if (rec_head)
            {
                cout << rec_i << ' ' << rec_head << ' ' << row[rec_i].head - 1 << endl;
                row[rec_i].head = rec_head;
            }
            else
            {
                cout << rec_i << ' ' << row[rec_i].tail + 1 << ' ' << rec_tail << endl;
                row[rec_i].tail = rec_tail;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

struct Row
{
    int head, tail;
} row[100];

int abs(int x)
{
    return x > 0 ? x : -x;
}

int sumy(int n)
{
    return (1 + n) * n / 2;
}

int main(void)
{
    int n, k;
    cin >> n >> k;
    int xc = k / 2 + 1, yc = k / 2 + 1;

    //init
    int for_row = xc + 1, bac_row = yc - 1;

    unsigned int maxi, sum;
    int i, m, rec_i, rec_head, rec_tail;
    while (n--)
    {
        //init
        cin >> m;
        maxi = -1;
        sum = -1;
        //oldline
        for (i = for_row; i <= bac_row; i++)
        {
            if (row[i].head - m > 0)
            {
                sum = (xc - i + row[i].head - yc) * m + sumy(m);
                if (sum < maxi)
                {
                    maxi = sum;
                    rec_i = i;
                    rec_head = row[i].head - m;
                    rec_tail = 0;
                }
            }
            if (row[i].tail + m <= k)
            {
                sum = (abs(xc - i) + row[i].tail - yc) * m + sumy(m);
                if (sum < maxi)
                {
                    maxi = sum;
                    rec_i = i;
                    rec_head = 0;
                    rec_tail = row[i].tail + m;
                }
            }
        }
        //newline
        i = (xc - for_row) > (bac_row - xc) ? (bac_row + 1) : (for_row - 1);
        if (i > 0 && i <= k && m <= k)
            sum = sumy(m / 2) + sumy(m / 2 + m % 2 - 1) + abs(i - xc) * m;
        if (sum < maxi)
        {
            if (for_row > i)
                for_row = i;
            if (bac_row < i)
                bac_row = i;
            row[i].head = yc - m / 2;
            row[i].tail = yc + m / 2 + m % 2 - 1;
            cout << i << ' ' << row[i].head << ' ' << row[i].tail << endl;
        }
        else
        {
            if (maxi == (unsigned int)(-1))
            {
                cout << -1 << endl;
                continue;
            }

            if (rec_head)
            {
                cout << rec_i << ' ' << rec_head << ' ' << row[rec_i].head - 1 << endl;
                row[rec_i].head = rec_head;
            }
            else
            {
                cout << rec_i << ' ' << row[rec_i].tail + 1 << ' ' << rec_tail << endl;
                row[rec_i].tail = rec_tail;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define re register
using namespace std;
inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}
bool a[110][110];
int main() {
    int n=read(),k=read();
    int xc=(k+1)/2,yc=(k+1)/2;
    while (n--) {
        int x=read();
        int X,Y,Min=2147483647;
        for (re int i=1;i<=k;i++)
            for (re int j=1;j<=k-x+1;j++) {
                bool f=1; int s=0;
                for (re int t=j;t<j+x;t++) {
                    if (a[i][t]) { f=0; break; }
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if(Min==2147483647) puts("-1");
        else{
            for(int i=Y;i<Y+x;i++) a[X][i]=1;
            printf("%d %d %d\n",X,Y,Y+x-1);
        }
    }
    return 0;
}//hello


#include <cstdio>
#define abs(x) ((x)>0?(x):-(x))
const int inf=101010;
const int maxn=1000+7;
int n,k,t;
bool v[maxn][maxn];
int f(int x,int y){
    return abs(x-((k+1)>>1))+abs(y-((k+1)>>1));
}
int main(){
  //freopen("in","r",stdin);
  scanf("%d%d",&n,&k);
  while(n--){
    scanf("%d",&t);
    int opt=inf,x,yl,yr;
    for(int i=1;i<=k;++i){
      int temp=0;
      for(int j=1;j<=k;++j){
        temp+=v[i][j]?inf:f(i,j);
        if(j>=t){
          if(temp<opt){
            opt=temp;x=i;yl=j-t+1,yr=j;
          }
          temp-=v[i][j-t+1]?inf:f(i,j-t+1);
        }
      }
    }
    if(opt==inf){
      printf("-1\n");
    }else{
      printf("%d %d %d\n",x,yl,yr);
      for(int i=yl;i<=yr;++i)v[x][i]=1;
    }
  }
}


#include <cstdio>
#define abs(x) (x)>0?(x):-(x)
const int inf=101010;
const int maxn=1000+7;
int n,k,t;
bool v[maxn][maxn];
int f(int x,int y){
    return abs(x-(k+1)/2)+abs(y-(k+1)/2);
}
int main(){
  //freopen("in","r",stdin);
  scanf("%d%d",&n,&k);
  while(n--){
    scanf("%d",&t);
    int opt=inf,x,yl,yr;
    for(int i=1;i<=k;++i){
      int temp=0;
      for(int j=1;j<=k;++j){
        temp+=v[i][j]?inf:f(i,j);
        if(j>=t){
          if(temp<opt){
            opt=temp;x=i;yl=j-t+1,yr=j;
          }
          temp-=v[i][j-t+1]?inf:f(i,j-t+1);
        }
      }
    }
    if(opt==inf){
      printf("-1\n");
    }else{
      printf("%d %d %d\n",x,yl,yr);
      for(int i=yl;i<=yr;++i)v[x][i]=1;
    }
  }
}


#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int M[1010];
int leave[110];
int visit[110][110];
int buff[110];
int caldistance(int x, int k, int left, int m)
{
    int Xc=k/2+1;
    int sumx=m*fabs(x-Xc);
    int sumy=0;
    if(left<=Xc)
        sumy+=(Xc-left)*(Xc-left+1)/2;
    if(left+m-1>Xc)
        sumy+=(left+m-Xc-1)*(left+m-Xc)/2;
    return sumy+sumx;

}
int check(int x,int row,int m)
{
    if(memcmp(visit[x]+row,buff,m*sizeof(int))==0)
        return 1;
    else
        return 0;
}
struct Node
{
    int x,left,M;
    Node(int x=-1,int left=-1,int M=0):x(x),left(left),M(M){}

}No[1010];
int main()
{
    int k,n;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>M[i];
    for(int i=1; i<=k; i++)
        leave[i]=k;
    memset(visit,0,sizeof(visit));
    memset(buff,0,sizeof(buff));
    for(int i=1; i<=n; i++)
    {
        int distance=100000000, bestx,bestrow,row;
        for(int x=1; x<=k; x++)
        {
            for( row=1; row<=k-M[i]+1; row++)
            {
                if(check(x,row,M[i])&&leave[x]>=M[i])
                {
                    if(caldistance(x,k,row,M[i])<distance)
                        {distance=caldistance(x,k,row,M[i]);bestx=x;bestrow=row;
                           // cout<<"check"<<check(x,row,M[i])<<"--"<<x<<"--"<<row<<"dis"<<caldistance(x,k,row,M[i])<<endl;
                        }

                }

            }

        }
        if(distance>100000)
                cout<<"-1"<<endl;
            else
            {
                //cout<<"-----------------"<<endl;
                cout<<bestx<<" "<<bestrow<<" "<<bestrow+M[i]-1<<endl;
                //memset(visit[bestx]+bestrow,-1,sizeof(int)*M[i]);
                for(int l=bestrow;l<=bestrow+M[i]-1;l++)
                    visit[bestx][l]=1;
                leave[bestx]-=M[i];
                /*for(int l=1;l<=k; l++)
                {
                    for(int p=1; p<=k; p++)
                        cout<<visit[l][p]<<" ";
                    cout<<endl;
                }
                */
                //cout<<"-----------------"<<endl;
            }

    }
    //cout<<caldistance(3,5,2,3)<<endl;
    //cout<<caldistance(3,5,1,3)<<endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int M[1010];
int leave[110];
int visit[110][110];
int buff[110];
int caldistance(int x, int k, int left, int m)
{
    int Xc=k/2+1;
    int sumx=m*fabs(x-Xc);
    int sumy=0;
    if(left<=Xc)
        sumy+=(Xc-left);
    if(left+m>Xc)
        sumy+=left+m-Xc-1;
    return sumy+sumx;

}
int check(int x,int row,int m)
{
    if(memcmp(visit[x]+row,buff,m*sizeof(int))==0)
        return 1;
    else
        return 0;
}
struct Node
{
    int x,left,M;
    Node(int x=-1,int left=-1,int M=0):x(x),left(left),M(M){}

}No[1010];
int main()
{
    int k,n;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>M[i];
    for(int i=1; i<=n; i++)
        leave[i]=k;
    memset(visit,0,sizeof(visit));
    memset(buff,0,sizeof(buff));
    for(int i=1; i<=n; i++)
    {
        int distance=100000000, bestx,bestrow,row;
        for(int x=1; x<=k; x++)
        {
            for( row=1; row<=k-M[i]+1; row++)
            {
                if(check(x,row,M[i])&&leave[x]>=M[i])
                {
                    if(caldistance(x,k,row,M[i])<distance)
                        {distance=caldistance(x,k,row,M[i]);bestx=x;bestrow=row;}
                }
            }

        }
        if(distance>100000)
                cout<<"-1"<<endl;
            else
            {
                //cout<<"-----------------"<<endl;
                cout<<bestx<<" "<<bestrow<<" "<<bestrow+M[i]-1<<endl;
                //memset(visit[bestx]+bestrow,-1,sizeof(int)*M[i]);
                for(int l=bestrow;l<=bestrow+M[i]-1;l++)
                    visit[bestx][l]=1;
                leave[bestx]-=M[i];
                /*for(int l=1;l<=k; l++)
                {
                    for(int p=1; p<=k; p++)
                        cout<<visit[l][p]<<" ";
                    cout<<endl;
                }
                */
                //cout<<"-----------------"<<endl;
            }

    }
    //cout<<caldistance(1,2,1,2)<<endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int M[1010];
int leave[110];
int visit[110][110];
int buff[110];
int caldistance(int x, int k, int left, int m)
{
    int Xc=k/2+1;
    int sumx=m*fabs(x-Xc);
    int sumy=0;
    if(left<=Xc&&left+m-1>=Xc)
        sumy=(Xc-left)*(Xc-left+1)/2+(left+m-Xc-1)*(left+m-Xc)/2;
    else if(left>Xc)
        sumy=m*(left-Xc+left+m-Xc-1)/2;
    else
        sumy=m*(Xc-left+Xc-left-m+1)/2;
    return sumy+sumx;

}
int check(int x,int row,int m)
{
    if(memcmp(visit[x]+row,buff,m*sizeof(int))==0)
        return 1;
    else
        return 0;
}
struct Node
{
    int x,left,M;
    Node(int x=-1,int left=-1,int M=0):x(x),left(left),M(M){}

}No[1010];
int main()
{
    int k,n;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>M[i];
    for(int i=1; i<=k; i++)
        leave[i]=k;
    memset(visit,0,sizeof(visit));
    memset(buff,0,sizeof(buff));
    for(int i=1; i<=n; i++)
    {
        int distance=100000000, bestx,bestrow,row;
        for(int x=1; x<=k; x++)
        {
            for( row=1; row<=k-M[i]+1; row++)
            {
                if(check(x,row,M[i])&&leave[x]>=M[i])
                {
                    if(caldistance(x,k,row,M[i])<distance)
                        {distance=caldistance(x,k,row,M[i]);bestx=x;bestrow=row;
                           // cout<<"check"<<check(x,row,M[i])<<"--"<<x<<"--"<<row<<"dis"<<caldistance(x,k,row,M[i])<<endl;
                        }

                }

            }

        }
        if(distance>100000)
                cout<<"-1"<<endl;
            else
            {
                //cout<<"-----------------"<<endl;
                cout<<bestx<<" "<<bestrow<<" "<<bestrow+M[i]-1<<endl;
                //memset(visit[bestx]+bestrow,-1,sizeof(int)*M[i]);
                for(int l=bestrow;l<=bestrow+M[i]-1;l++)
                    visit[bestx][l]=1;
                leave[bestx]-=M[i];
                /*for(int l=1;l<=k; l++)
                {
                    for(int p=1; p<=k; p++)
                        cout<<visit[l][p]<<" ";
                    cout<<endl;
                }
                */
                //cout<<"-----------------"<<endl;
            }

    }
    //cout<<caldistance(12,19,12,3)<<endl;
    //cout<<caldistance(10,19,14,3)<<endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int M[1010];
int leave[110];
int visit[110][110];
int buff[110];
int caldistance(int x, int k, int left, int m)
{
    int Xc=k/2+1;
    int sumx=m*fabs(x-Xc);
    int sumy=0;
    if(left<=Xc)
        sumy+=(Xc-left);
    if(left+m>Xc)
        sumy+=left+m-Xc-1;
    return sumy+sumx;

}
int check(int x,int row,int m)
{
    if(memcmp(visit[x]+row,buff,m*sizeof(int))==0)
        return 1;
    else
        return 0;
}
struct Node
{
    int x,left,M;
    Node(int x=-1,int left=-1,int M=0):x(x),left(left),M(M){}

}No[1010];
int main()
{
    int k,n;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>M[i];
    for(int i=1; i<=k; i++)
        leave[i]=k;
    memset(visit,0,sizeof(visit));
    memset(buff,0,sizeof(buff));
    for(int i=1; i<=n; i++)
    {
        int distance=100000000, bestx,bestrow,row;
        for(int x=1; x<=k; x++)
        {
            for( row=1; row<=k-M[i]+1; row++)
            {
                if(check(x,row,M[i])&&leave[x]>=M[i])
                {
                    if(caldistance(x,k,row,M[i])<distance)
                        {distance=caldistance(x,k,row,M[i]);bestx=x;bestrow=row;
                           // cout<<"check"<<check(x,row,M[i])<<"--"<<x<<"--"<<row<<"dis"<<caldistance(x,k,row,M[i])<<endl;
                        }

                }

            }

        }
        if(distance>100000)
                cout<<"-1"<<endl;
            else
            {
                //cout<<"-----------------"<<endl;
                cout<<bestx<<" "<<bestrow<<" "<<bestrow+M[i]-1<<endl;
                //memset(visit[bestx]+bestrow,-1,sizeof(int)*M[i]);
                for(int l=bestrow;l<=bestrow+M[i]-1;l++)
                    visit[bestx][l]=1;
                leave[bestx]-=M[i];
                /*for(int l=1;l<=k; l++)
                {
                    for(int p=1; p<=k; p++)
                        cout<<visit[l][p]<<" ";
                    cout<<endl;
                }
                */
                //cout<<"-----------------"<<endl;
            }

    }
    //cout<<caldistance(2,3,2,1)<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int m;
    int n,k; cin >> n >> k;
    int c=ceil(k/2.0); c--;
    bool a[99][99]={};
    for(int t = 0 ; t < n ; t++)
    {
        cin >> m;
        int mnD=1e5,mnX,mnL,mnR;
        for(int x = 0 ; x < k ; x++)
        {
            int d=0,z=0;
            for(int y = 0 ; y < k ; y++)
            {
                if(a[x][y]==0)
                {
                    z++;
                    d+=abs(y-c)+abs(x-c);
                    if(z>m) d-=abs(y-m-c)+abs(x-c);
                    if(z>=m && d<mnD)
                    {
                        mnD=d;
                        mnL=y-m+1;
                        mnR=y;
                        mnX=x;
                    }
                }
                else
                {
                    d=0;
                    z=0;
                }
            }
        }
        if(mnD==1e5) cout << "-1\n";
        else
        {for(int j = mnL ; j<=mnR ; j++) a[mnX][j]=1;
        cout << mnX+1 << ' ' << mnL+1 << ' ' << mnR+1 << "\n";}
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int m;
    int n,k; cin >> n >> k;
    int c=ceil(k/2.0); c--;
    bool a[99][99]={};
    for(int t = 0 ; t < n ; t++)
    {
        cin >> m;
        int mnD=300,mnX,mnL,mnR;
        for(int x = 0 ; x < k ; x++)
        {
            int d=0,z=0;
            for(int y = 0 ; y < k ; y++)
            {
                if(a[x][y]==0)
                {
                    z++;
                    d+=abs(y-c)+abs(x-c);
                    if(z>m) d-=abs(y-m-c)+abs(x-c);
                    if(z>=m && d<mnD)
                    {
                        mnD=d;
                        mnL=y-m+1;
                        mnR=y;
                        mnX=x;
                    }
                }
                else
                {
                    d=0;
                    z=0;
                }
            }
        }
        if(mnD==300) cout << "-1\n";
        else
        {for(int j = mnL ; j<=mnR ; j++) a[mnX][j]=1;
        cout << mnX+1 << ' ' << mnL+1 << ' ' << mnR+1 << "\n";}
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int m;
    int n,k; cin >> n >> k;
    int c=ceil(k/2.0); c--;
    bool a[99][99]={};
    for(int t = 0 ; t < n ; t++)
    {
        cin >> m;
        int mnD=300,mnX,mnL,mnR;
        for(int x = 0 ; x < k ; x++)
        {
            int d=0,z=0;
            for(int y = 0 ; y < k ; y++)
            {
                if(a[x][y]==0)
                {
                    z++;
                    d+=abs(y-c)+abs(x-c);
                    if(z>m) d-=abs(y-m-c)+abs(x-c);
                    if(z>=m && d<mnD)
                    {
                        mnD=d;
                        mnL=y-m+1;
                        mnR=y;
                        mnX=x;
                    }
                }
                else
                {
                    z=0;
                }
            }
        }
        if(mnD==300) cout << "-1\n";
        else
        {for(int j = mnL ; j<=mnR ; j++) a[mnX][j]=1;
        cout << mnX+1 << ' ' << mnL+1 << ' ' << mnR+1 << "\n";}
    }
    return 0;
}


// Harry Potter
#include <bits/stdc++.h>
using namespace std;
int dp[1001][100], xc, yc, n, k;
int main(){
	scanf("%d%d",&n,&k);
	xc = yc = (k+1)/2;
	for(int x = 1; x <= k; x ++)
		for(int y = 1; y <= k; y ++)
			dp[x][y] += dp[x][y-1] + abs(x-xc)+abs(y-yc);

	for(int j = 1; j <= n; j ++){
		int p; scanf("%d", &p);
		int yl, yr, x=0, mn = 1e8;

		for(int i = 1; i <= k; i ++)
			for(int ytl = 1; ytl <= k-p+1; ytl++)
				if(mn > dp[i][ytl+p-1]-dp[i][ytl-1]){
					x = i; yl = ytl; yr = ytl+p-1;
					mn = dp[i][ytl+p-1]-dp[i][ytl-1];
				}

		if(!x) {cout<<-1<<endl; continue;}
		cout<<x<<" "<<yl<<" "<<yr<<endl;

		for(int i = yl; i <= k; i ++)
			if(i<=yr) dp[x][i]=dp[x][i-1]+1e8;
			else dp[x][i]=dp[x][i-1]+abs(x-xc)+abs(i-yc);
	}
}


// Harry Potter
#include <bits/stdc++.h>
using namespace std;
int dp[1001][100], xc, yc, n, k,dp2[1001][100];
int main(){
	scanf("%d%d",&n,&k);
	xc = yc = (k+1)/2;
	for(int x = 1; x <= k; x ++)
		for(int y = 1; y <= k; y ++)
			dp[x][y] += dp[x][y-1] + abs(x-xc)+abs(y-yc),
			dp2[x][y] = abs(x-xc)+abs(y-yc);

	for(int j = 1; j <= n; j ++){
		int p; scanf("%d", &p);
		int yl, yr, x=0, mn = 1e8;

		for(int i = 1; i <= k; i ++)
			for(int ytl = 1; ytl <= k-p+1; ytl++)
				if(mn > dp[i][ytl+p-1]-dp[i][ytl-1]){
					x = i; yl = ytl; yr = ytl+p-1;
					mn = dp[i][ytl+p-1]-dp[i][ytl-1];
				}

		if(!x) {cout<<-1<<endl; continue;}
		cout<<x<<" "<<yl<<" "<<yr<<endl;

		for(int i = yl; i <= k; i ++){
			if(i<=yr) dp2[x][i] = 1e8;
			dp[x][i]=dp[x][i-1]+dp2[x][i];
		}
	}
}


// Harry Potter
#include <bits/stdc++.h>
using namespace std;
int dp[1001][100], xc, yc, n, k;
int main(){
	scanf("%d%d",&n,&k);
	xc = yc = (k+1)/2;
	for(int x = 1; x <= k; x ++)
		for(int y = 1; y <= k; y ++)
			dp[x][y] += dp[x][y-1] + abs(x-xc)+abs(y-yc);

	for(int j = 1; j <= n; j ++){
		int p; scanf("%d", &p);
		int yl, yr, x=0, mn = 1e8;
		for(int i = 1; i <= k; i ++)
			for(int ytl = 1; ytl <= k-p+1; ytl++)
				if(mn > dp[i][ytl+p-1]-dp[i][ytl-1]){
					x = i; yl = ytl; yr = ytl+p-1;
					mn = dp[i][ytl-p+1]-dp[i][ytl-1];
				}
		if(!x) {cout<<-1<<endl; continue;}
		cout<<x<<" "<<yl<<" "<<yr<<endl;

		for(int i = yl; i <= k; i ++)
			if(i<=yr) dp[x][i]=dp[x][i-1]+1e8;
			else dp[x][i]=dp[x][i-1]+abs(x-xc)+abs(i-yc);
	}
}


// Harry Potter
#include <bits/stdc++.h>
using namespace std;
int dp[1001][100], xc, yc, n, k,dp2[1001][100];
int main(){
	scanf("%d%d",&n,&k);
	xc = yc = (k+1)/2;
	for(int x = 1; x <= k; x ++)
		for(int y = 1; y <= k; y ++) {
			dp[x][y] += dp[x][y-1] + abs(x-xc)+abs(y-yc);
			dp2[x][y] = abs(x-xc)+abs(y-yc);
		}

	for(int j = 1; j <= n; j ++){
		int p; scanf("%d", &p);
		int yl, yr, x=0, mn = 1e4;

		for(int i = 1; i <= k; i ++)
			for(int ytl = 1; ytl <= k-p+1; ytl++)
				if(mn > dp[i][ytl+p-1]-dp[i][ytl-1]){
					x = i; yl = ytl; yr = ytl+p-1;
					mn = dp[i][ytl+p-1]-dp[i][ytl-1];
				}

		if(!x) {cout<<-1<<endl; continue;}
		cout<<x<<" "<<yl<<" "<<yr<<endl;
//		cout<<mn<<endl;
		for(int i = yl; i <= k; i ++){
			if(i<=yr) dp2[x][i] = 1e4;
			dp[x][i]=dp[x][i-1]+dp2[x][i];
		}
	}
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,k,x;
int mp[1002][1002];
void f(int x)
{
    int px=(k+1)/2,py=(k+1)/2;
    int ans1=0,ans2=0,ans3=0,ans4=1e9;
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=k-x+1;j++)
        {
            int sum=0,flag=1;
            for(int t=j;t<j+x;t++)
            {
                if(mp[i][t])
                {
                    flag=0;
                    break;
                }
                sum+=abs(t-py)+abs(i-px);
            }
            if(flag==1&&sum<ans4)
            {
                ans4=sum;
                ans1=i;
                ans2=j;
                ans3=j+x-1;
            }
        }
    }
    if(ans4==1e9)printf("-1\n");
    else
    {
        for(int i=ans2;i<=ans3;i++)
            mp[ans1][i]=1;
        printf("%d %d %d\n",ans1,ans2,ans3);
    }
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        f(x);
    }
    return 0;
}


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

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}

bool a[110][110];

int main() {
    int n=read(),k=read();
    int xc=(k+1)/2,yc=(k+1)/2;
    while (n--) {
        int x=read();
        int X,Y,Min=2147483647;
        for (re int i=1;i<=k;i++)
            for (re int j=1;j<=k-x+1;j++) {
                bool f=1; int s=0;
                for (re int t=j;t<j+x;t++) {
                    if (a[i][t]) { f=0; break; }
                    s+=abs(i-xc)+abs(t-yc);
                }
                if (f&&s<Min) Min=s,X=i,Y=j;
            }
        if (Min==2147483647) puts("-1");
        else {
            for (re int i=Y;i<Y+x;i++) a[X][i]=1;
            printf("%d %d %d\n",X,Y,Y+x-1);
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

unsigned int n;
vector<vector<bool>> cinema;

bool empty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        if (cinema[u][i]) {
            return false;
        }
    }
    return true;
}

void notEmpty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        cinema[u][i] = true;
    }
}

bool correct(int i, int l, int r) {
    return i < n && i >= 0 &&
           l < n && l >= 0 &&
           r < n && r >= 0;
}

int main() {
    int q;
    cin >> q >> n;
    cinema.resize(n, vector<bool>(n, false));
    int ic = n / 2, jc = n / 2;
    for (int i = 0; i < q; ++i) {
        int m;
        cin >> m;
        bool success = false;
        for (int j = 0; j <= n; ++j) {
            for (int dist = -j / m; dist * m <= j && !success; dist++) {
                for (int d = -1; d < 2; d += 2) {
                    int ni = ic + dist, njr = jc + d * (j - abs(dist) * m) + m / 2 - !(m % 2),
                            njl = jc + d * (j - abs(dist) * m) - m / 2;
//                    cout << ni << " " << njl << " " << njr << "\n";
                    if (correct(ni, njl, njr) && empty(ni, njl, njr)) {
                        notEmpty(ni, njl, njr);
                        success = true;
                        cout << ni + 1 << " " << njl + 1 << " " << njr + 1 << "\n";
                        break;
                    }
                    if (!(m % 2) && correct(ni, njl + 1, njr + 1) &&
                        empty(ni, njl + 1, njr + 1)) {
                        notEmpty(ni, njl + 1, njr + 1);
                        success = true;
                        cout << ni + 1 << " " << njl + 2 << " " << njr + 2 << "\n";
                        break;
                    }
                }
            }
        }
        if (!success) {
            cout << "-1\n";
        }
//        for (int j = 0; j < n; ++j) {
//            for (int k = 0; k < n; ++k) {
//                cout << cinema[j][k];
//            }
//            cout << "\n";
//        }
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

const int INF = (int) 1e9;

unsigned int n;
vector<vector<bool>> cinema;
int ic = 0, jc = 0;

int empty(int u, int l, int r) {
    int ans = abs(u - ic) * (r - l + 1);
    for (int j = l; j <= r; ++j) {
        if (cinema[u][j]) {
            return -1;
        }
        ans += abs(j - jc);
    }
    return ans;
}

void notEmpty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        cinema[u][i] = true;
    }
}

int main() {
    int q;
    cin >> q >> n;
    ic = n / 2, jc = ic;
    cinema.resize(n, vector<bool>(n, false));
    for (int t = 0; t < q; ++t) {
        int m;
        cin >> m;
        int minDist = INF;
        int y = 0, x = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int e = empty(i, j, j + m - 1);
//                cout << i << " " << j << " " << e << "\n";
                if (e != -1 && e < minDist) {
                    minDist = e;
                    y = i;
                    x = j;
                }
            }
        }
        if (minDist != INF) {
            notEmpty(y, x, x + m - 1);
            cout << y + 1 << " " << x + 1 << " " << x + m << "\n";
        } else {
            cout << "-1\n";
        }
//        for (int i = 0; i < n; ++i) {
//            for (int j = 0; j < n; ++j) {
//                cout << cinema[i][j];
//            }
//            cout << "\n";
//        }
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

unsigned int n;
vector<vector<bool>> cinema;

bool empty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        if (cinema[u][i]) {
            return false;
        }
    }
    return true;
}

void notEmpty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        cinema[u][i] = true;
    }
}

bool correct(int i, int l, int r) {
    return i < n && i >= 0 &&
           l < n && l >= 0 &&
           r < n && r >= 0;
}

int main() {
    int q;
    cin >> q >> n;
    cinema.resize(n, vector<bool>(n, false));
    int ic = n / 2, jc = n / 2;
    for (int i = 0; i < q; ++i) {
        int m;
        cin >> m;
        bool success = false;
        for (int j = 0; j <= n; ++j) {
            int njl = jc - m / 2 - j % m, njr = jc + m / 2 - !(m % 2) - j % m,
                    nid = ic - j / m, niu = ic + j / m;
//            cout << j << " " << nid << " " << njl << " " << njr << " " << niu << "\n";
            if (correct(nid, njl, njr) && empty(nid, njl, njr)) {
                notEmpty(nid, njl, njr);
                success = true;
                cout << nid + 1 << " " << njl + 1 << " " << njr + 1 << "\n";
                break;
            }
            if (!(m % 2) && correct(nid, njl + 1, njr + 1) && empty(nid, njl + 1, njr + 1)) {
                notEmpty(nid, njl + 1, njr + 1);
                success = true;
                cout << nid + 1 << " " << njl + 2 << " " << njr + 2 << "\n";
                break;
            }
            int nnl = jc - j - m / 2, nnr = jc - j + m / 2 - !(m % 2);
            if (correct(ic, nnl, nnr) && empty(ic, nnl, nnr)) {
                notEmpty(ic, nnl, nnr);
                success = true;
                cout << ic + 1 << " " << nnl + 1 << " " << nnr + 1 << "\n";
                break;
            }
            if (!(m % 2) && correct(ic, nnl + 1, nnr + 1) && empty(ic, nnl + 1, nnr + 1)) {
                notEmpty(ic, nnl + 1, nnr + 1);
                success = true;
                cout << ic + 1 << " " << nnl + 3 << " " << nnr + 2 << "\n";
                break;
            }
            if (correct(niu, njl, njr) && empty(niu, njl, njr)) {
                notEmpty(niu, njl, njr);
                success = true;
                cout << niu + 1 << " " << njl + 1 << " " << njr + 1 << "\n";
                break;
            }
            if (!(m % 2) && correct(niu, njl + 1, njr + 1) && empty(niu, njl + 1, njr + 1)) {
                notEmpty(niu, njl + 1, njr + 1);
                success = true;
                cout << niu + 1 << " " << njl + 2 << " " << njr + 2 << "\n";
                break;
            }
        }
        if (!success) {
            cout << "-1\n";
        }
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

const int INF = (int) 1e9;

unsigned int n;
vector<vector<bool>> cinema;
int ic = n / 2, jc = n / 2;

int empty(int u, int l, int r) {
    int ans = abs(u - ic) * (r - l + 1);
    for (int i = l; i <= r; ++i) {
        if (cinema[u][i]) {
            return 0;
        } else {
            ans += abs(i - jc);
        }
    }
    return ans;
}

void notEmpty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        cinema[u][i] = true;
    }
}

int main() {
    int q;
    cin >> q >> n;
    cinema.resize(n, vector<bool>(n, false));
    for (int t = 0; t < q; ++t) {
        int m;
        cin >> m;
        int minDist = INF;
        int y = 0, x = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int e = empty(i, j, j + m - 1);
                if (e && e < minDist) {
                    minDist = e;
                    y = i;
                    x = j;
                }
            }
        }
        if (minDist != INF) {
            notEmpty(y, x, x + m - 1);
            cout << y + 1 << " " << x + 1 << " " << x + m << "\n";
        } else {
            cout << "-1\n";
        }
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

const int INF = (int) 1e9;

unsigned int n;
vector<vector<bool>> cinema;
int ic = 0, jc = 0;

int empty(int u, int l, int r) {
    int ans = abs(u - ic) * (r - l + 1);
    for (int j = l; j <= r; ++j) {
        if (cinema[u][j]) {
            return -1;
        }
        ans += abs(j - jc);
    }
    return ans;
}

void notEmpty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        cinema[u][i] = true;
    }
}

int main() {
    int q;
    cin >> q >> n;
    ic = n / 2, jc = ic;
    cinema.resize(n, vector<bool>(n, false));
    for (int t = 0; t < q; ++t) {
        int m;
        cin >> m;
        int minDist = INF;
        int y = 0, x = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int e = empty(i, j, j + m - 1);
//                cout << i << " " << j << " " << e << "\n";
                if (e != -1 && e < minDist) {
                    minDist = e;
                    y = i;
                    x = j;
                }
            }
        }
        if (minDist != INF) {
            notEmpty(y, x, x + m - 1);
            cout << y + 1 << " " << x + 1 << " " << x + m << "\n";
        } else {
            cout << "-1\n";
        }
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

const int INF = (int) 1e9;

unsigned int n;
vector<vector<bool>> cinema;
int ic = 0, jc = 0;

int empty(int u, int l, int r) {
    int ans = abs(u - ic) * (r - l + 1);
    for (int j = l; j <= r; ++j) {
        if (cinema[u][j]) {
            return -1;
        }
        ans += abs(j - jc);
    }
    return ans;
}

void notEmpty(int u, int l, int r) {
    for (int i = l; i <= r; ++i) {
        cinema[u][i] = true;
    }
}

int main() {
    int q;
    cin >> q >> n;
    ic = n / 2;
    jc = n / 2;
    cinema.resize(n, vector<bool>(n, false));
    for (int t = 0; t < q; ++t) {
        int m;
        cin >> m;
        int minDist = INF;
        int y = 0, x = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n - m + 1; ++j) {
                int e = empty(i, j, j + m - 1);
//                cout << i << " " << j << " " << e << "\n";
                if (e != -1 && e < minDist) {
                    minDist = e;
                    y = i;
                    x = j;
                }
            }
        }
        if (minDist != INF) {
            notEmpty(y, x, x + m - 1);
            cout << y + 1 << " " << x + 1 << " " << x + m << "\n";
        } else {
            cout << "-1\n";
        }
//        for (int i = 0; i < n; ++i) {
//            for (int j = 0; j < n; ++j) {
//                cout << cinema[i][j];
//            }
//            cout << "\n";
//        }
    }
//    system("pause");
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n , k , m ;
int acum[108][108],sum[108][108],c;

void initialize(){
	for(int i = 1 ; i <= k ; i ++ ) {
		for(int j = 1 ; j <= k ; j ++ ) {
			acum[i][j]=acum[i][j-1]+1;
		}
	}
}

int x , yr ;

int sumatory(int a){
	return a*(a+1)/2;
}

int calcMinSum(int i , int j ) {
	int yl = j- m + 1 ;
	int ans = abs(i-c) * m ;
	if(yl<=c && c <= j ) {
		ans += sumatory(j-c)+sumatory(c-yl);
	}else{
		int limUpper = m-1+c-min(abs(c-yl),abs(c-j));
		ans += sumatory(limUpper)-sumatory(limUpper-m);
	}
	//cout << "m: " << m << " i: " << i << " j: " << j << " ans: " << ans << "\n";
	return ans;
}

void reserve(){
	int minSum = INT_MAX;
	for( int i = 1 ; i <= k ; i ++ ) {
		for(int j = 1 ; j <= k ; j ++ ) {
			if(acum[i][j]>=m){
				int sum = calcMinSum(i,j);
				if(minSum>sum){
					minSum=sum;
					x = i , yr = j ;
				}
			}
		}
	}
	if(minSum!=INT_MAX){
		cout << x << " " << yr-m+1 << " "<<yr;
		///recalc row
		for(int j = yr - m +1  ; j <= yr ; j ++ ) acum[x][j]=0;
		int newAcum = 0;
		for(int j = yr + 1 ; j <= k && acum[x][j] ; j ++ ) acum[x][j]=++newAcum;
	}else{
		cout << "-1";
	}
	cout << "\n";
}

int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n >> k ;
	c=k/2+1;
	initialize();
	while(n--){
		cin >> m ;
		reserve();
	}
    	return 0;

}


#include<bits/stdc++.h>

using namespace std;

int n , k , m ;
int acum[108][108],sum[108][108],c;

void initialize(){
	for(int i = 1 ; i <= k ; i ++ ) {
		for(int j = 1 ; j <= k ; j ++ ) {
			acum[i][j]=acum[i][j-1]+1;
		}
	}
}

int x , yr ;

int sumatory(int a){
	return a*(a+1)/2;
}

int calcMinSum(int i , int j ) {
	int yl = j- m + 1 ;
	int ans = abs(i-c) * m ;
	if(yl<=c && c <= j ) {
		ans += sumatory(j-c)+sumatory(c-yl);
	}else{
		//int limUpper = m-1+c-min(abs(c-yl),abs(c-j));
		int limUpper = m+min(abs(c-yl),abs(c-j))-1;
		ans += sumatory(limUpper)-sumatory(limUpper-m);
	}
	//cout << "m: " << m << " i: " << i << " j: " << j << " ans: " << ans << "\n";
	return ans;
}

void reserve(){
	int minSum = INT_MAX;
	for( int i = 1 ; i <= k ; i ++ ) {
		for(int j = 1 ; j <= k ; j ++ ) {
			if(acum[i][j]>=m){
				int sum = calcMinSum(i,j);
				if(minSum>sum){
					minSum=sum;
					x = i , yr = j ;
				}
			}
		}
	}
	if(minSum!=INT_MAX){
		cout << x << " " << yr-m+1 << " "<<yr;
		///recalc row
		for(int j = yr - m +1  ; j <= yr ; j ++ ) acum[x][j]=0;
		int newAcum = 0;
		for(int j = yr + 1 ; j <= k && acum[x][j] ; j ++ ) acum[x][j]=++newAcum;
	}else{
		cout << "-1";
	}
	cout << "\n";
}

int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n >> k ;
	c=k/2+1;
	initialize();
	while(n--){
		cin >> m ;
		reserve();
	}
    	return 0;

}