#include <bits/stdc++.h>
using namespace std;
const int M = (int)1e5 + 5, N = (int)2e5 + 5;
struct BIT {
  int val[N];
  void add(int x, int v) {
    while (x < N) {
      val[x] += v;
      x += x & (-x);
    }
  }
  int sum(int x) {
    int re = 0;
    while (x) {
      re += val[x];
      x -= x & (-x);
    }
    return re;
  }
} TD;
struct W {
  int t, s, f, id;
  bool operator<(const W &a) const { return t < a.t; }
} A[M];
int n, m;
long long ans[M];
struct T {
  int s, f, id;
  bool operator<(const T &a) const {
    if (s != a.s) return s < a.s;
    if (f != a.f) return f < a.f;
    if (id != a.id) return id < a.id;
    return 0;
  }
};
set<T> st_in, st_out;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d %d %d", &A[i].t, &A[i].s, &A[i].f), A[i].id = i;
  sort(A + 1, A + n + 1);
  long long t = 0, nxt;
  for (int i = 1, now = 1, cnt = 0, shang, xia; cnt < n;) {
    while (i <= n && t >= A[i].t) {
      TD.add(A[i].s, 1);
      st_in.insert((T){A[i].s, A[i].f, A[i].id});
      i++;
    }
    for (;;) {
      static set<T>::iterator it;
      it = st_in.lower_bound((T){now, 0, 0});
      if (it == st_in.end()) break;
      if ((*it).s != now) break;
      TD.add(now, -1);
      TD.add((*it).f, 1);
      st_out.insert((T){(*it).f, 0, (*it).id});
      st_in.erase(it);
    }
    for (;;) {
      static set<T>::iterator it;
      it = st_out.lower_bound((T){now, 0, 0});
      if (it == st_out.end()) break;
      if ((*it).s != now) break;
      TD.add(now, -1);
      ans[(*it).id] = t;
      cnt++;
      st_out.erase(it);
    }
    xia = TD.sum(now);
    shang = i - cnt - xia - 1;
    if (shang == 0 && xia == 0) {
      t = A[i].t;
    } else if (shang >= xia) {
      if (i <= n)
        nxt = A[i].t;
      else
        nxt = 1e18;
      static set<T>::iterator it;
      it = st_in.lower_bound((T){now, 0, 0});
      if (it != st_in.end()) {
        if ((*it).s - now + t <= nxt) nxt = (*it).s - now + t;
      }
      it = st_out.lower_bound((T){now, 0, 0});
      if (it != st_out.end()) {
        if ((*it).s - now + t <= nxt) nxt = (*it).s - now + t;
      }
      now += nxt - t;
      t = nxt;
      for (;;) {
        it = st_in.lower_bound((T){now, 0, 0});
        if (it == st_in.end()) break;
        if ((*it).s != now) break;
        TD.add(now, -1);
        TD.add((*it).f, 1);
        st_out.insert((T){(*it).f, 0, (*it).id});
        st_in.erase(it);
      }
      for (;;) {
        it = st_out.lower_bound((T){now, 0, 0});
        if (it == st_out.end()) break;
        if ((*it).s != now) break;
        TD.add(now, -1);
        ans[(*it).id] = t;
        cnt++;
        st_out.erase(it);
      }
    } else {
      if (i <= n)
        nxt = A[i].t;
      else
        nxt = 1e18;
      static set<T>::iterator it;
      it = st_in.upper_bound((T){now, 0, 0});
      if (it != st_in.begin()) {
        it--;
        if (now - (*it).s + t <= nxt) nxt = now - (*it).s + t;
      }
      it = st_out.upper_bound((T){now, 0, 0});
      if (it != st_out.begin()) {
        it--;
        if (now - (*it).s + t <= nxt) nxt = now - (*it).s + t;
      }
      now -= nxt - t;
      t = nxt;
      for (;;) {
        it = st_in.lower_bound((T){now, 0, 0});
        if (it == st_in.end()) break;
        if ((*it).s != now) break;
        TD.add(now, -1);
        TD.add((*it).f, 1);
        st_out.insert((T){(*it).f, 0, (*it).id});
        st_in.erase(it);
      }
      for (;;) {
        it = st_out.lower_bound((T){now, 0, 0});
        if (it == st_out.end()) break;
        if ((*it).s != now) break;
        TD.add(now, -1);
        ans[(*it).id] = t;
        cnt++;
        st_out.erase(it);
      }
    }
  }
  for (int i = 1; i <= n; i++) printf("%I64d\n", ans[i]);
  return 0;
}
