#include <bits/stdc++.h>
using namespace std;
struct Point {
  int x, y;
};
bool operator<(const Point &x, const Point &y) {
  return x.x < y.x || (x.x == y.x && x.y < y.y);
}
multiset<Point> nu, nd, wu, wd;
set<Point>::iterator it;
Point li[100050];
int n, m, dir, v[100050][2], sn, sw, flr, ri, up, dn;
long long now, ans[100050], INF;
inline int Read() {
  int x = 0;
  char y;
  do y = getchar();
  while (y < '0' || y > '9');
  do x = x * 10 + y - '0', y = getchar();
  while (y >= '0' && y <= '9');
  return x;
}
inline long long min(long long x, long long y) { return x < y ? x : y; }
inline long long Calc() { return ri == n + 1 ? INF : li[ri].x; }
long long calc() {
  if (!dir || !sw)
    return INF;
  else if (dir == 1) {
    it = wu.lower_bound((Point){flr, -1});
    return it == wu.end() ? INF : it->x - flr + now;
  } else {
    it = wd.lower_bound((Point){-flr, -1});
    return it == wd.end() ? INF : now + flr + it->x;
  }
}
long long cAlc() {
  if (!dir || !sn)
    return INF;
  else if (dir == 1) {
    it = nu.lower_bound((Point){flr, -1});
    return it == nu.end() ? INF : it->x - flr + now;
  } else {
    it = nd.lower_bound((Point){-flr, -1});
    return it == nd.end() ? INF : now + flr + it->x;
  }
}
void Release() {
  do {
    it = nu.lower_bound((Point){flr, -1});
    if (it == nu.end() || it->x != flr) return;
    sn--;
    int q = it->y;
    ans[q] = now;
    nu.erase(it);
    it = nd.lower_bound((Point){-flr, q});
    nd.erase(it);
    if (dir == 2)
      dn--;
    else
      up--;
  } while (1);
}
void Absorb() {
  do {
    it = wu.lower_bound((Point){flr, -1});
    if (it == wu.end() || it->x != flr) return;
    sw--;
    int q = it->y;
    wu.erase(it);
    it = wd.lower_bound((Point){-flr, q});
    wd.erase(it);
    nu.insert((Point){v[q][1], q});
    nd.insert((Point){-v[q][1], q});
    sn++;
    if (dir == 2)
      dn--;
    else if (dir == 1)
      up--;
    if (v[q][1] > flr)
      up++;
    else
      dn++;
  } while (1);
}
void Treat() {
  while (ri <= n && li[ri].x == now) {
    int q = li[ri++].y;
    sw++;
    wu.insert((Point){v[q][0], q});
    wd.insert((Point){-v[q][0], q});
    up += v[q][0] > flr;
    dn += v[q][0] < flr;
    if (v[q][0] == flr)
      if (dir == 2)
        dn++;
      else if (dir == 1)
        up++;
  }
}
void Solve() {
  now = 0;
  flr = 1;
  ri = 1;
  while (sn || sw || ri <= n) {
    long long q = Calc(), w = calc(), e = cAlc(), s = now;
    now = min(min(q, w), e);
    if (dir == 1)
      flr += now - s;
    else if (dir == 2)
      flr -= now - s;
    Treat();
    Release();
    Absorb();
    if (dn > up)
      dir = 2;
    else
      dir = 1;
    if (!dn && !up) dir = 0;
  }
}
int main() {
  n = Read();
  m = Read();
  for (int i = 1; i <= n; i++)
    li[i].x = Read(), li[i].y = i, v[i][0] = Read(), v[i][1] = Read();
  sort(li + 1, li + n + 1);
  INF = (long long)(1e12);
  Solve();
  for (int i = 1; i <= n; i++) printf("%I64d\n", ans[i]);
  return 0;
}
