#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000, maxm = 100000;
const long long inf = (long long)1e18;
struct guy {
  long long t;
  int s, f, id;
  inline bool operator<(const guy &g) const { return t < g.t; }
};
int n, m, fenw[maxm];
long long ans[maxn];
guy data[maxn];
set<pair<int, int> > dest, wait;
inline void inc(int x, int d) {
  for (; x < m; x |= x + 1) fenw[x] += d;
}
inline int sum(int r) {
  int ans = 0;
  for (; r != -1; r = (r & (r + 1)) - 1) ans += fenw[r];
  return ans;
}
inline int sum(int l, int r) {
  if (l > r) return 0;
  return sum(r) - sum(l - 1);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> data[i].t >> data[i].s >> data[i].f;
    data[i].s--, data[i].f--;
    data[i].id = i;
  }
  sort(data, data + n);
  int cur = 0, ptr = 0;
  long long curt = 0;
  memset(ans, -1, sizeof(ans));
  while (curt != inf) {
    while (ptr < n && data[ptr].t == curt) {
      inc(data[ptr].s, 1);
      wait.insert(make_pair(data[ptr].s, ptr));
      ptr++;
    }
    for (auto it = dest.lower_bound(make_pair(cur, -1));
         it != dest.end() && it->first == cur;
         it = dest.lower_bound(make_pair(cur, -1))) {
      inc(it->first, -1);
      ans[data[it->second].id] = curt;
      dest.erase(it);
    }
    for (auto it = wait.lower_bound(make_pair(cur, -1));
         it != wait.end() && it->first == cur;
         it = wait.lower_bound(make_pair(cur, -1))) {
      dest.insert(make_pair(data[it->second].f, it->second));
      inc(it->first, -1);
      inc(data[it->second].f, 1);
      wait.erase(it);
    }
    if (sum(0, m - 1) == 0) {
      if (ptr == n) break;
      curt = data[ptr].t;
      continue;
    }
    int dir = sum(cur + 1, m - 1) >= sum(0, cur - 1) ? 1 : -1;
    long long arrive = inf;
    if (dir == -1) {
      auto it = dest.lower_bound(make_pair(cur, -1));
      if (it != dest.begin()) {
        it--;
        arrive = min(arrive, curt + cur - it->first);
      }
      it = wait.lower_bound(make_pair(cur, -1));
      if (it != wait.begin()) {
        it--;
        arrive = min(arrive, curt + cur - it->first);
      }
    } else {
      auto it = dest.lower_bound(make_pair(cur, -1));
      if (it != dest.end()) arrive = min(arrive, curt + it->first - cur);
      it = wait.lower_bound(make_pair(cur, -1));
      if (it != wait.end()) arrive = min(arrive, curt + it->first - cur);
    }
    if (ptr < n) arrive = min(arrive, data[ptr].t);
    cur += (arrive - curt) * dir;
    curt = arrive;
  }
  for (int i = 0; i < n; i++) {
    assert(ans[i] != -1);
    cout << ans[i] << endl;
  }
  return 0;
}
