#include <bits/stdc++.h>
using namespace std;
struct person {
 public:
  int t, s, f, id;
  person(){};
  person(int t, int s, int f, int id) : t(t), s(s), f(f), id(id){};
};
const long long INF = 2 * (long long)1e17;
vector<int> exit_floor, input_floor;
int nn = 0;
void init(vector<int>& t, int sz) {
  nn = sz + 5;
  t.assign(nn, 0);
}
int sum(vector<int>& t, int r) {
  int res = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) res += t[r];
  return res;
}
void add(vector<int>& t, int i, int delta) {
  for (; i < nn; i = (i | (i + 1))) t[i] += delta;
}
int sum(vector<int>& t, int l, int r) { return sum(t, r) - sum(t, l - 1); }
struct cmp_by_floor {
  bool operator()(const person& a, const person& b) { return a.f < b.f; }
};
bool cmp_by_time(const person& a, const person& b) { return a.t < b.t; }
int main() {
  int n, m;
  cin >> n >> m;
  init(exit_floor, m);
  init(input_floor, m);
  vector<person> all;
  for (int i = 0; i < n; i++) {
    int ti, si, fi;
    cin >> ti >> si >> fi;
    all.push_back(person(ti, si, fi, i));
  }
  vector<long long> ans(n, -1);
  vector<vector<person> > in(m + 1);
  vector<vector<person> > waits(m + 1);
  long long curt = 1, x = 1;
  sort(all.begin(), all.end(), cmp_by_time);
  set<int> floors;
  set<int>::iterator it;
  int pos = 0;
  while (true) {
    while (pos < n && curt >= all[pos].t) {
      person it = all[pos];
      waits[it.s].push_back(all[pos]);
      floors.insert(all[pos].s);
      add(input_floor, all[pos].s, 1);
      pos++;
    }
    while (waits[x].size() > 0) {
      person it = waits[x].back();
      waits[x].pop_back();
      in[it.f].push_back(it);
      add(exit_floor, it.f, 1);
      floors.insert(it.f);
      add(input_floor, x, -1);
    }
    while (in[x].size() > 0) {
      person it = in[x].back();
      in[x].pop_back();
      ans[it.id] = curt;
      add(exit_floor, x, -1);
    }
    int p_up = 0, p_down = 0;
    p_up = sum(exit_floor, x + 1, m) + sum(input_floor, x + 1, m);
    p_down = sum(exit_floor, 1, x - 1) + sum(input_floor, 1, x - 1);
    if (p_up == 0 && p_down == 0) {
      if (pos >= n) break;
      curt = all[pos].t;
      continue;
    }
    long long nextt = INF, nextx = -1;
    if (pos < n) nextt = all[pos].t;
    floors.insert(x);
    if (p_up >= p_down) {
      nextx = INF;
      it = floors.find(x);
      it++;
      if (it != floors.end()) {
        nextx = *it;
      }
      if (nextt == INF && nextx == INF) break;
      if (nextt - curt < nextx - x) {
        nextx = x + nextt - curt;
      } else {
        nextt = curt + nextx - x;
      }
    } else {
      nextx = -INF;
      it = floors.find(x);
      if (it != floors.begin()) {
        it--;
        nextx = *it;
      }
      if (nextt == INF && nextx == -INF) break;
      if (nextt - curt < x - nextx) {
        nextx = x - (nextt - curt);
      } else {
        nextt = curt + x - nextx;
      }
    }
    floors.erase(x);
    curt = nextt;
    x = nextx;
  }
  for (int i = 0; i < n; i++) cout << ans[i] << endl;
  return 0;
}
