#include <bits/stdc++.h>
using namespace std;
int k;
int dist(int x, int y1, int y2) {
  int ans = 0;
  int r = (k + 1) / 2;
  for (int y = y1; y <= y2; ++y) ans += abs(r - x) + abs(r - y);
  return ans;
}
int main() {
  int n;
  cin >> n >> k;
  vector<int> esq(k + 1, k + 1), dre(k + 1, 0);
  int centre = (k + 1) / 2;
  for (int i = 0; i < n; ++i) {
    int m;
    cin >> m;
    int solx = -1, soly = -1;
    int r = 1000000000;
    int lo, hi;
    for (int x = 1; x <= k; ++x) {
      if (dre[x] == 0) {
        lo = centre - m / 2;
        hi = centre + (m - 1) / 2;
        int p = dist(x, lo, hi);
        if (p < r) {
          r = p;
          solx = x;
          soly = lo;
        }
      } else {
        if (esq[x] - m > 0) {
          int p = dist(x, esq[x] - m, esq[x] - 1);
          if (p < r) {
            r = p;
            solx = x;
            soly = esq[x] - m;
          }
        }
        if (dre[x] + m <= k) {
          int p = dist(x, dre[x] + 1, dre[x] + m);
          if (p < r) {
            r = p;
            solx = x;
            soly = dre[x] + 1;
          }
        }
      }
    }
    if (solx < 0)
      cout << -1 << endl;
    else {
      esq[solx] = min(esq[solx], soly);
      dre[solx] = max(soly + m - 1, dre[solx]);
      cout << solx << " " << soly << " " << soly + m - 1 << endl;
    }
  }
  cin >> n;
}
