#include <bits/stdc++.h>
using namespace std;
long long n, m;
struct person {
  int t, s, f, n;
  bool operator<(const person &r) const { return t < r.t; }
};
vector<person> v;
priority_queue<int> below, inBelow;
priority_queue<int, vector<int>, greater<int> > above, inAbove;
map<int, vector<int> > waiting, inWaiting;
vector<long long> res;
int main() {
  long long i, j;
  scanf("%d %d", &n, &m);
  v.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d %d %d", &(v[i].t), &(v[i].s), &(v[i].f));
    v[i].n = i;
  }
  sort(v.begin(), v.end());
  long long nextPerson = 0;
  long long curT = 0;
  int curFloor = 1;
  res.resize(n);
  while (1) {
    if (below.empty() && above.empty() && inBelow.empty() && inAbove.empty()) {
      if (nextPerson == n) break;
      curT = v[nextPerson].t;
    }
    while (nextPerson < n && v[nextPerson].t == curT) {
      person &p = v[nextPerson];
      if (p.s < curFloor) {
        below.push(p.s);
        waiting[p.s].push_back(nextPerson);
      } else if (p.s > curFloor) {
        above.push(p.s);
        waiting[p.s].push_back(nextPerson);
      } else {
        if (p.f < curFloor) {
          inBelow.push(p.f);
          inWaiting[p.f].push_back(p.n);
        } else {
          inAbove.push(p.f);
          inWaiting[p.f].push_back(p.n);
        }
      }
      ++nextPerson;
    }
    if (inWaiting.count(curFloor)) {
      for (i = 0; i < inWaiting[curFloor].size(); ++i) {
        res[inWaiting[curFloor][i]] = curT;
      }
      inWaiting.erase(curFloor);
      while (!inAbove.empty() && inAbove.top() == curFloor) inAbove.pop();
      while (!inBelow.empty() && inBelow.top() == curFloor) inBelow.pop();
    }
    if (waiting.count(curFloor)) {
      for (i = 0; i < waiting[curFloor].size(); ++i) {
        person &p = v[waiting[curFloor][i]];
        inWaiting[p.f].push_back(p.n);
        if (p.f > curFloor) {
          inAbove.push(p.f);
        } else {
          inBelow.push(p.f);
        }
      }
      waiting.erase(curFloor);
      while (!above.empty() && above.top() == curFloor) above.pop();
      while (!below.empty() && below.top() == curFloor) below.pop();
    }
    if (below.empty() && above.empty() && inBelow.empty() && inAbove.empty()) {
      continue;
    }
    int up = above.size() + inAbove.size();
    int down = below.size() + inBelow.size();
    int nextFloor;
    if (up >= down) {
      nextFloor = 100001;
      if (!above.empty()) nextFloor = min(nextFloor, above.top());
      if (!inAbove.empty()) nextFloor = min(nextFloor, inAbove.top());
      if (nextPerson < n)
        nextFloor =
            min((long long)nextFloor, curFloor + (v[nextPerson].t - curT));
    } else {
      nextFloor = 0;
      if (!below.empty()) nextFloor = max(nextFloor, below.top());
      if (!inBelow.empty()) nextFloor = max(nextFloor, inBelow.top());
      if (nextPerson < n)
        nextFloor =
            max((long long)nextFloor, curFloor - (v[nextPerson].t - curT));
    }
    curT += abs(curFloor - nextFloor);
    curFloor = nextFloor;
  }
  for (i = 0; i < n; ++i) {
    printf("%I64d\n", res[i]);
  }
  return 0;
}
