#include <bits/stdc++.h>
using namespace std;
struct person {
  int id, s, f;
  long long t;
  bool operator<(const person &b) const { return t < b.t; }
} a[100000 + 5];
int n, m, f[100000 + 5], x = 1, pu = 0, pd = 0;
long long ans[100000 + 5], now = 0;
set<int> e, w;
vector<int> el[100000 + 5], wt[100000 + 5];
void getin(int x) {
  for (int j = 0; j < wt[x].size(); j++) {
    e.insert(f[wt[x][j]]), el[f[wt[x][j]]].push_back(wt[x][j]),
        pu += f[wt[x][j]] > x, pd += f[wt[x][j]] < x;
  }
}
void getout(int x) {
  for (int j = 0; j < el[x].size(); j++) ans[el[x][j]] = now;
}
int main() {
  scanf("%d%d", &n, &m);
  int i;
  for (i = 1; i <= n; i++) {
    scanf("%lld%d%d", &a[i].t, &a[i].s, &a[i].f);
    a[i].id = i, f[i] = a[i].f;
  }
  sort(a + 1, a + 1 + n);
  a[n + 1].t = 1e15;
  i = 1;
  while (pu + pd || i <= n) {
    set<int>::iterator t1 = e.lower_bound(x), t2 = w.lower_bound(x);
    int delta = 1e9;
    int k = 0;
    bool flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0;
    if (i <= n) delta = a[i].t - now;
    if (pu || pd)
      if (pu >= pd) {
        if (t1 != e.end()) delta = min(delta, *t1 - x), flag1 = 1;
        if (t2 != w.end()) delta = min(delta, *t2 - x), flag2 = 1;
        k = 1;
      } else {
        if (t1 != e.begin()) t1--, delta = min(delta, x - *t1), flag3 = 1;
        if (t2 != w.begin()) t2--, delta = min(delta, x - *t2), flag4 = 1;
        k = -1;
      }
    now += delta, x += k * delta;
    while (i <= n && a[i].t == now) {
      int nxt;
      if (a[i].s == x)
        e.insert(a[i].f), el[a[i].f].push_back(a[i].id), nxt = a[i].f;
      else
        w.insert(a[i].s), wt[a[i].s].push_back(a[i].id), nxt = a[i].s;
      pu += nxt > x, pd += nxt < x, i++;
    }
    t1 = e.lower_bound(x), t2 = w.lower_bound(x);
    if (t1 != e.end() && *t1 == x) e.erase(t1);
    if (t2 != w.end() && *t2 == x) w.erase(t2);
    if (k) {
      getin(x), getout(x);
      if (k > 0) pu -= el[x].size() + wt[x].size();
      if (k < 0) pd -= el[x].size() + wt[x].size();
      el[x].clear(), wt[x].clear();
    }
  }
  for (int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
  return 0;
}
