#include <bits/stdc++.h>
using namespace std;
const int K = 99;
int n, k, m, hall[K][K];
int main() {
  cin >> n >> k;
  memset(hall, 0, sizeof(hall));
  auto f = [&](int a, int b, int c, int k) {
    int res = 0, xc = ceil((k - 1) / (double)2), yc = xc;
    res += ((c - b) + 1) * abs(a - xc);
    for (int i = b; i <= c; i++) res += abs(i - yc);
    return res;
  };
  for (int i = 0; i < n; i++) {
    cin >> m;
    vector<pair<int, int> > tmp;
    vector<int> vtmp;
    for (int g = 0; g < k; g++) {
      int curr = 0;
      for (int p = 0; p < k; p++) {
        if (!hall[g][p]) {
          curr++;
          if (curr == m) {
            tmp.push_back({g, p - m + 1});
            vtmp.push_back(f(g, p - m + 1, p, k));
            curr--;
          }
        } else {
          curr = 0;
        }
      }
    }
    int xidx, yidx, mv = 0x3f3f3f3f;
    for (int i = 0; i < vtmp.size(); i++) {
      if (vtmp[i] < mv) {
        mv = vtmp[i];
        xidx = tmp[i].first, yidx = tmp[i].second;
      } else if (vtmp[i] == mv) {
        if (tmp[i].first == xidx) {
          yidx = min(yidx, tmp[i].second);
        } else if (tmp[i].first < xidx) {
          xidx = tmp[i].first, yidx = tmp[i].second;
        }
      }
    }
    if (!vtmp.size()) {
      cout << -1 << endl;
    } else {
      cout << xidx + 1 << " " << yidx + 1 << " " << yidx + m << endl;
      for (int i = yidx; i <= yidx + m - 1; i++) hall[xidx][i] = 1;
    }
  }
}
