#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, k, eqi;
int cnt(int yl, int yr) {
  if (yl > yr) return 0;
  if (eqi < yl)
    return ((yl - eqi + yr - eqi) * (yr - yl + 1)) / 2;
  else if (eqi > yr)
    return ((eqi - yl + eqi - yr) * (yr - yl + 1)) / 2;
  else
    return cnt(yl, eqi - 1) + cnt(eqi + 1, yr);
}
struct pos {
  int row, yl, yr;
  pos(int a, int b, int c) : row(a), yl(b), yr(c) {}
  bool operator<(const pos &a) const {
    int s1 = abs(row - eqi) * (yr - yl + 1);
    int s2 = abs(a.row - eqi) * (a.yr - a.yl + 1);
    int cnt1 = cnt(yl, yr), cnt2 = cnt(a.yl, a.yr);
    if (s1 + cnt1 != s2 + cnt2) return s1 + cnt1 < s2 + cnt2;
    if (row != a.row) return row < a.row;
    return yl < a.yl;
  }
  bool operator==(const pos &a) const {
    return row == a.row && yl == a.yl && yr == a.yr;
  }
};
bool tab[100][100];
int main() {
  scanf("%d %d", &n, &k);
  eqi = (k + 1) / 2;
  for (int i = 0; i < (n); ++i) {
    int m;
    pos best = pos(1000, 1000, 10000);
    scanf("%d", &m);
    for (int i = 1; i <= (k); ++i) {
      int pocz = 0;
      for (int j = 1; j <= (k); ++j) {
        if (tab[i][j]) pocz = j;
        if (j - pocz >= m) {
          pos temp = pos(i, j - m + 1, j);
          if (temp < best) best = temp;
        }
      }
    }
    if (best == pos(1000, 1000, 10000))
      printf("-1\n");
    else {
      printf("%d %d %d\n", best.row, best.yl, best.yr);
      for (int i = best.yl; i <= (best.yr); ++i) tab[best.row][i] = 1;
    }
  }
}
