#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxN = 1e5 + 10;
int n, m;
int t[maxN], s[maxN], f[maxN], tree[maxN];
long long res[maxN];
vector<int> wait[maxN], lift[maxN];
pair<int, int> e[maxN];
inline int get(int x) {
  x++;
  int res = 0;
  for (; x > 0; x -= x & -x) res += tree[x];
  return res;
}
inline void upd(int x, int val) {
  x++;
  for (; x < 1e5 + 10; x += x & -x) tree[x] += val;
}
inline int getU(int x) {
  int cur = get(x);
  int l = x, r = 1e5 + 9;
  while (l < r) {
    int m = (l + r + 1) >> 1;
    if (get(m) == cur)
      l = m;
    else
      r = m - 1;
  }
  return l - x + 1;
}
inline int getD(int x) {
  int cur = get(x);
  int l = 0, r = x;
  while (l < r) {
    int m = (l + r) >> 1;
    if (get(m) == cur)
      r = m;
    else
      l = m + 1;
  }
  return x - r;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = (0); i < (n); i++) cin >> t[i] >> s[i] >> f[i];
  for (int i = (0); i < (n); i++) e[i] = make_pair(t[i], i);
  sort(e, e + n);
  int cur = 1;
  long long T = 0;
  int waiting = n;
  int lifted = 0;
  int L = 0;
  while (waiting + lifted) {
    while (L < n && e[L].first <= T) {
      wait[s[e[L].second]].push_back(e[L].second);
      upd(s[e[L].second], 1);
      L++;
    }
    for (int i = (0); i < (lift[cur].size()); i++) {
      res[lift[cur][i]] = T;
      upd(cur, -1);
      lifted--;
    }
    lift[cur].clear();
    for (int i = (0); i < (wait[cur].size()); i++) {
      lift[f[wait[cur][i]]].push_back(wait[cur][i]);
      upd(cur, -1);
      upd(f[wait[cur][i]], 1);
      waiting--;
      lifted++;
    }
    wait[cur].clear();
    int sum1 = get(cur);
    int sum2 = get(m) - sum1;
    long long dT = 1e9;
    if (L < n) dT = min(dT, e[L].first - T);
    if (sum1 + sum2 > 0) {
      if (sum1 > sum2) {
        dT = min(dT, (long long)getD(cur));
        cur -= dT;
      } else {
        dT = min(dT, (long long)getU(cur));
        cur += dT;
      }
    }
    T += dT;
  }
  for (int i = (0); i < (n); i++) cout << res[i] << '\n';
  return 0;
}
