#include <bits/stdc++.h>
using namespace std;
int n, m;
long long Ans[200009];
int read() {
  int x = 1;
  char ch;
  while (ch = getchar(), ch < '0' || ch > '9')
    if (ch == '-') x = -1;
  int s = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
  return x * s;
}
struct passenger {
  int t, s, f, pos;
  void scan(int i) { t = read(), s = read(), f = read(), pos = i; }
  bool operator<(const passenger &rhs) const { return t < rhs.t; }
} p[200009], q[200009];
struct node {
  int id, pos;
  node(int id = 0, int pos = 0) : id(id), pos(pos) {}
  bool operator<(const node &rhs) const { return pos < rhs.pos; }
  bool operator>(const node &rhs) const { return pos > rhs.pos; }
};
multiset<node, less<node> > to_up, from_up;
multiset<node, greater<node> > to_down, from_down;
int pos = 1, finish = 0, id = 1;
long long t = 1;
void add_from() {
  while (p[id].t == t) {
    if (pos == p[id].s) {
      if (p[id].f > pos)
        to_up.insert(node(p[id].pos, p[id].f));
      else
        to_down.insert(node(p[id].pos, p[id].f));
    } else {
      if (p[id].s > pos)
        from_up.insert(node(p[id].pos, p[id].s));
      else
        from_down.insert(node(p[id].pos, p[id].s));
    }
    id++;
  }
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) p[i].scan(i), q[i] = p[i];
  sort(p + 1, p + n + 1);
  to_up.insert(node(0, 0x3f3f3f3f)), from_up.insert(node(0, 0x3f3f3f3f));
  to_down.insert(node(0, -0x3f3f3f3f)), from_down.insert(node(0, -0x3f3f3f3f));
  while (finish < n) {
    int size1 = to_up.size() + from_up.size();
    int size2 = to_down.size() + from_down.size();
    if (size1 == 2 && size2 == 2) {
      if (id > n) break;
      t = p[id].t, add_from();
      continue;
    }
    if (size1 >= size2) {
      node now1 = *(to_up.begin());
      node now2 = *(from_up.begin());
      if (id <= n && p[id].t - t < now1.pos - pos &&
          p[id].t - t < now2.pos - pos) {
        pos += p[id].t - t;
        t = p[id].t, add_from();
      } else {
        if (now1 > now2) swap(now1, now2);
        t += (long long)now1.pos - pos;
        pos = now1.pos;
        node now;
        while ((now = *(to_up.begin())).pos == pos) {
          Ans[now.id] = t;
          to_up.erase(to_up.find(now));
          finish++;
        }
        while ((now = *(from_up.begin())).pos == pos) {
          from_up.erase(from_up.find(now));
          if (q[now.id].f > pos)
            to_up.insert(node(now.id, q[now.id].f));
          else
            to_down.insert(node(now.id, q[now.id].f));
        }
        add_from();
      }
    } else {
      node now1 = *(to_down.begin());
      node now2 = *(from_down.begin());
      if (id <= n && p[id].t - t < pos - now1.pos &&
          p[id].t - t < pos - now2.pos) {
        pos -= p[id].t - t;
        t = p[id].t, add_from();
      } else {
        if (now1 < now2) swap(now1, now2);
        t += (long long)pos - now1.pos;
        pos = now1.pos;
        node now;
        while ((now = *(to_down.begin())).pos == pos) {
          Ans[now.id] = t;
          to_down.erase(to_down.find(now));
          finish++;
        }
        while ((now = *(from_down.begin())).pos == pos) {
          from_down.erase(from_down.find(now));
          if (q[now.id].f > pos)
            to_up.insert(node(now.id, q[now.id].f));
          else
            to_down.insert(node(now.id, q[now.id].f));
        }
        add_from();
      }
    }
  }
  for (int i = 1; i <= n; i++) printf("%lld\n", Ans[i]);
  return 0;
}
