#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<int> > g(k + 1, vector<int>(k + 1));
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++) g[i][j] = k + 1;
  int mid = (k + 1) / 2;
  vector<int> pref(k + 1, 0);
  for (int i = 1; i <= k; i++) pref[i] = pref[i - 1] + abs(mid - i);
  while (n--) {
    int m;
    cin >> m;
    int best = 1e6, x = -1, yl = -1;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j + m - 1 <= k; j++) {
        int fir = j, las = j + m - 1;
        if (g[i][fir] == g[i][las] && g[i][fir] != 0) {
          int cost = (las - fir + 1) * abs(i - mid);
          cost += pref[las] - pref[fir - 1];
          if (cost < best) {
            best = cost;
            x = i;
            yl = fir;
          } else if (cost == best) {
            if (i < x) {
              x = i;
              yl = fir;
            } else if (i == x && fir < yl) {
              yl = fir;
            }
          }
        }
      }
    }
    if (yl == -1)
      cout << -1 << endl;
    else {
      for (int z = k, ne = k + 1; z >= 1; z--) {
        if (z < yl && g[x][z] != 0) g[x][z] = ne;
        if (yl <= z && z <= yl + m - 1) g[x][z] = 0;
        if (g[x][z] == 0) ne = z;
      }
      cout << x << " " << yl << " " << (yl + m - 1) << endl;
    }
  }
  return 0;
}
