#include <bits/stdc++.h>
using namespace std;
int n, k, m;
int a[1000][1000], F[1000][1000], can[1000][1000];
bool free(int x, int y1, int y2) {
  int t = 0;
  if (y1 == 0)
    t = can[x][y2];
  else
    t = can[x][y2] - can[x][y1 - 1];
  return t == (y2 - y1 + 1);
}
int f(int x, int y1, int y2) {
  int t = 0;
  if (y1 == 0)
    t = F[x][y2];
  else
    t = F[x][y2] - F[x][y1 - 1];
  return t;
}
void solvex(int x) {
  can[x][0] = a[x][0];
  for (int i = 1; i < k; i++) {
    can[x][i] = can[x][i - 1] + a[x][i];
  }
}
void defree(int x, int y1, int y2) {
  for (int i = y1; i <= y2; i++) {
    a[x][i] = 0;
  }
  solvex(x);
}
inline void solve(int m) {
  int curf = 2000000000;
  int curx, cury = 0;
  for (int x = 0; x < k; x++) {
    for (int y = 0; y < k - m + 1; y++) {
      if (free(x, y, y + m - 1)) {
        int t = f(x, y, y + m - 1);
        if (t < curf || (t == curf && x < curx) ||
            (t == curf && x == curx && y < cury)) {
          curf = t;
          curx = x;
          cury = y;
        }
      }
    }
  }
  if (curf == 2000000000) {
    cout << -1 << endl;
    return;
  } else {
    cout << curx + 1 << " " << cury + 1 << " " << cury + m << endl;
  }
  defree(curx, cury, cury + m - 1);
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      a[i][j] = 1;
      if (j)
        F[i][j] = F[i][j - 1] + abs(i - k / 2) + abs(j - k / 2);
      else
        F[i][j] = abs(i - k / 2) + abs(j - k / 2);
    }
    solvex(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> m;
    solve(m);
  }
  return 0;
}
