#include <bits/stdc++.h>
using namespace std;
void run();
int main() {
  ios::sync_with_stdio(0);
  run();
}
struct beautiful {
  int when, s, t;
  int id;
  bool operator<(beautiful const& other) const {
    return when != other.when ? when < other.when
           : s != other.s     ? s < other.s
           : t != other.t     ? t < other.t
                              : id < other.id;
  }
};
int n, people;
beautiful person[200000];
vector<int> pate[200000];
vector<int> wait[200000];
set<int> ev;
long long answer[200000];
long long fen[200000];
inline void insert(int x, int y = 1) {
  for (int i = x; i <= n + 2; i |= i + 1) fen[i] += y;
}
inline int resolve(int x) {
  int y = 0;
  for (int i = x; i >= 0; --(i &= i + 1)) y += fen[i];
  return y;
}
void run() {
  cin >> people >> n;
  for (int i = 0; i < people; i++)
    cin >> person[i].when >> person[i].s >> person[i].t, person[i].id = i;
  ev.insert(0);
  ev.insert(n + 2);
  sort(person, person + people);
  long long t = 1, pid = 0;
  for (long long x = 1, jobs = people * 3; jobs > 0;) {
    while (pid < people and person[pid].when == t) {
      wait[person[pid].s].push_back(pid);
      insert(person[pid].s, +1);
      ev.insert(person[pid].s);
      ++pid;
      --jobs;
    }
    for (int i = wait[x].size(); i--;) {
      long long y = wait[x][i];
      pate[person[y].t].push_back(y);
      insert(person[y].s, -1);
      insert(person[y].t, +1);
      ev.insert(person[y].t);
      --jobs;
    }
    wait[x].clear();
    for (int i = pate[x].size(); i--;) {
      long long y = pate[x][i];
      insert(person[y].t, -1);
      answer[person[y].id] = t;
      --jobs;
    }
    pate[x].clear();
    ev.erase(x);
    int pup = resolve(n + 2) - resolve(x);
    int pdn = resolve(x - 1);
    long long delta = 123456789ll, dx = 0;
    if (pup == 0 and pdn == 0)
      dx = 0;
    else
      dx = (pup >= pdn ? +1 : -1);
    if (dx) {
      auto it = ev.upper_bound(x);
      if (dx < 0) --it;
      int p = *it;
      if (p < 1) p = 1;
      if (p > n) p = n;
      delta = min(delta, abs(x - p));
    }
    if (pid < people) delta = min(delta, person[pid].when - t);
    x += dx * delta, t += delta;
  }
  for (int i = 0; i < people; i++) cout << answer[i] << endl;
}
