#include <bits/stdc++.h>
const long long N = 100009;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
struct psnger {
  long long t;
  long long s, f, id;
  bool operator<(const psnger &o) const { return t < o.t; }
  bool operator<=(const psnger &o) const { return t <= o.t; }
  psnger(const long long _t, const long long _s, const long long _f)
      : t(_t), s(_s), f(_f) {}
  psnger() : t(0), s(0), f(0) {}
} b[N], a[N], Out, In, Come;
std::set<std::pair<long long, long long> > s1, s2;
long long ans[N], n, m, delta[N];
void work() {
  s2.insert(std::pair<long long, long long>(-INF, -1));
  s2.insert(std::pair<long long, long long>(INF, -1));
  s1.insert(std::pair<long long, long long>(-INF, -1));
  s1.insert(std::pair<long long, long long>(INF, -1));
  long long dir = 0, nowt = 0, nowf = 1, p_up = 0, p_down = 0, i = 1, test,
            d = 1;
  for (; i <= n || s1.size() - 2 || s2.size() - 2; ++d) {
    bool flg = false;
    if (d == 8) d = 8;
    if (s2.size() == 2)
      Out = psnger(INF, 0, 0);
    else {
      std::set<std::pair<long long, long long> >::iterator j =
          s2.lower_bound(std::pair<long long, long long>(nowf, 0));
      if (j->first == nowf) {
        Out = b[j->second];
        Out.t = nowt;
      } else {
        if (dir < 0) --j;
        if (j->second == -1)
          Out = psnger(INF, 0, 0);
        else {
          Out = b[j->second];
          Out.t = abs(nowf - Out.f) + nowt;
        }
      }
    }
    if (s1.size() == 2)
      In = psnger(INF, 0, 0);
    else {
      std::set<std::pair<long long, long long> >::iterator j =
          s1.lower_bound(std::pair<long long, long long>(nowf, 0));
      if (j->first == nowf) {
        In = b[j->second];
        In.t = nowt;
      } else {
        if (j->first != nowf && dir < 0) --j;
        if (j->second == -1)
          In = psnger(INF, 0, 0);
        else {
          In = b[j->second];
          In.t = abs(nowf - In.s) + nowt;
        }
      }
    }
    Come = a[i];
    if (Out < In && Out <= Come) {
      nowt = Out.t;
      nowf = Out.f;
      std::set<std::pair<long long, long long> >::iterator
          j = s2.lower_bound(std::pair<long long, long long>(nowf, 0)),
          k = j;
      for (; j->first == nowf; ++j) {
        ans[j->second] = nowt;
        if (dir > 0)
          --p_up;
        else
          --p_down;
      }
      s2.erase(k, j);
      if (In.t == nowt || Come.t == nowt) flg = true;
    } else if (In < Come) {
      nowf += dir * (In.t - nowt);
      nowt = In.t;
      std::set<std::pair<long long, long long> >::iterator
          j = s1.lower_bound(std::pair<long long, long long>(nowf, 0)),
          k = j;
      for (; j->first == nowf; ++j) {
        s2.insert(std::pair<long long, long long>(b[j->second].f, j->second));
        if (b[j->second].f > nowf)
          ++p_up;
        else
          ++p_down;
        if (delta[j->second] > 0)
          --p_up;
        else if (delta[j->second] < 0)
          --p_down;
      }
      s1.erase(k, j);
      if (Out.t == nowt || Come.t == nowt) flg = true;
    } else {
      s1.insert(std::pair<long long, long long>(Come.s, Come.id));
      nowf += dir * (Come.t - nowt);
      nowt = Come.t;
      if (Come.s > nowf) ++p_up, delta[Come.id] = 1;
      if (Come.s < nowf) ++p_down, delta[Come.id] = -1;
      if (Come.s == nowf) delta[Come.id] = 0;
      ++i;
      if (In.t == nowt || Out.t == nowt) flg = true;
    }
    if (flg) continue;
    if (!(s2.size() - 2) && !p_up && !p_down)
      dir = 0;
    else if (p_up >= p_down)
      dir = 1;
    else
      dir = -1;
  }
}
int main() {
  scanf("%I64d%I64d", &n, &m);
  for (long long i = 1; i <= n; ++i) {
    scanf("%I64d%I64d%I64d", &a[i].t, &a[i].s, &a[i].f);
    a[i].id = i;
    b[i] = a[i];
  }
  std::sort(a + 1, a + 1 + n);
  a[n + 1].t = INF;
  work();
  for (long long i = 1; i <= n; ++i) printf("%I64d\n", ans[i]);
}
