#include <bits/stdc++.h>
using namespace std;
int n, m, F[100020];
long long cur, tim, z[100020];
set<pair<int, int> > u1, u2, d1, d2;
struct N {
  int s, f, r;
  long long t;
} a[100010];
bool operator<(const N& a, const N& b) { return a.t < b.t; }
inline void push(int x, int y) {
  if (x == cur)
    z[y] = tim;
  else if (x > cur)
    u1.insert(pair<int, int>(x, y));
  else if (x < cur)
    d1.insert(pair<int, int>(x, y));
}
inline long long getu(const set<pair<int, int> >& s) {
  if (s.size() == 0) return 0x3f3f3f3f3f3f3fLL;
  return s.begin()->first;
}
inline long long getd(const set<pair<int, int> >& s) {
  if (s.size() == 0) return 0;
  return s.rbegin()->first;
}
inline void move(long long dt) {
  while (dt) {
    if (u1.size() + u2.size() == 0 && d1.size() + d2.size() == 0) break;
    if (u1.size() + u2.size() >= d1.size() + d2.size()) {
      long long pos = min(getu(u1), getu(u2));
      pos = min(pos, cur + dt);
      tim += pos - cur;
      dt -= pos - cur;
      cur = pos;
      while (u1.size() && u1.begin()->first == pos) {
        z[u1.begin()->second] = tim;
        u1.erase(u1.begin());
      }
      while (u2.size() && u2.begin()->first == pos) {
        push(F[u2.begin()->second], u2.begin()->second);
        u2.erase(u2.begin());
      }
    } else {
      long long pos = max(getd(d1), getd(d2));
      pos = max(pos, cur - dt);
      tim += cur - pos;
      dt -= cur - pos;
      cur = pos;
      while (d1.size() && d1.rbegin()->first == pos) {
        z[d1.rbegin()->second] = tim;
        d1.erase(--d1.end());
      }
      while (d2.size() && d2.rbegin()->first == pos) {
        push(F[d2.rbegin()->second], d2.rbegin()->second);
        d2.erase(--d2.end());
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%I64d%d%d", &a[i].t, &a[i].s, &a[i].f), a[i].r = i, F[i] = a[i].f;
  sort(a, a + n);
  cur = 1;
  a[n].t = 6000000020LL;
  for (int i = 0; i < n; i++) {
    tim = a[i].t;
    if (cur == a[i].s)
      push(a[i].f, a[i].r);
    else if (cur < a[i].s)
      u2.insert(pair<int, int>(a[i].s, a[i].r));
    else if (cur > a[i].s)
      d2.insert(pair<int, int>(a[i].s, a[i].r));
    move(a[i + 1].t - a[i].t);
  }
  for (int i = 0; i < n; i++) printf("%I64d\n", z[i]);
  return 0;
}
