#include <bits/stdc++.h>
using namespace std;
set<int> in;
set<int> wait;
vector<pair<pair<int, int>, pair<int, int> > > a;
vector<vector<int> > g1, g2;
long long ans[100100];
int t[2][100100];
int N;
int in1[100100];
int out[100100];
int getsum(int v, int p) {
  int res = 0;
  for (int i = p; i > 0; i -= (i & (-i))) res += t[v][i];
  return res;
}
void add(int v, int p, int val) {
  for (int i = p; i <= N; i += (i & (-i))) t[v][i] += val;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  a.resize(n);
  g1.resize(m + 1);
  g2.resize(m + 1);
  N = m;
  for (int i = (0); i < (n); ++i) {
    scanf("%d%d%d", &a[i].first.first, &a[i].second.first, &a[i].second.second);
    in1[i] = a[i].second.first;
    out[i] = a[i].second.second;
    a[i].first.second = i;
  }
  sort(a.begin(), a.end());
  int p = 0;
  int cur = 1;
  int dir = 0;
  long long now = 0;
  while (1) {
    if ((in.size() == 0) && (wait.size() == 0) && (p == a.size())) break;
    long long t1, t2, t3;
    t1 = t2 = t3 = 1000000000000000000ll;
    if (p < a.size()) t1 = a[p].first.first - now;
    int to1 = -1;
    if (dir == 1) {
      set<int>::iterator it = wait.lower_bound(cur);
      if (it != wait.end()) {
        to1 = *it;
        t2 = *it - cur;
      }
    } else if (dir == -1) {
      set<int>::iterator it = wait.upper_bound(cur);
      if (it != wait.begin()) {
        it--;
        to1 = *it;
        t2 = cur - *it;
      }
    }
    int to2 = -1;
    if (dir == 1) {
      set<int>::iterator it = in.lower_bound(cur);
      if (it != in.end()) {
        to2 = *it;
        t3 = *it - cur;
      }
    } else if (dir == -1) {
      set<int>::iterator it = in.upper_bound(cur);
      if (it != in.begin()) {
        it--;
        to2 = *it;
        t3 = cur - *it;
      }
    }
    if ((t1 < t2) && (t1 < t3)) {
      now += t1;
      if (dir == 1) {
        cur += t1;
        if (cur > m) cur = m;
      }
      if (dir == -1) {
        cur -= t1;
        if (cur < 1) cur = 1;
      }
      wait.insert(a[p].second.first);
      g1[a[p].second.first].push_back(a[p].first.second);
      add(0, a[p].second.first, 1);
      p++;
    } else if ((to1 != -1) || (to2 != -1)) {
      int to = to1;
      if (t2 > t3) to = to2;
      now += ((t2 < t3) ? (t2) : (t3));
      cur = to;
      for (int i = (0); i < (g2[to].size()); ++i) {
        ans[g2[to][i]] = now;
        add(1, to, -1);
      }
      in.erase(to);
      g2[to].clear();
      for (int i = (0); i < (g1[to].size()); ++i) {
        int k = out[g1[to][i]];
        g2[k].push_back(g1[to][i]);
        if (g2[k].size() == 1) in.insert(k);
        add(0, to, -1);
        add(1, k, 1);
      }
      wait.erase(to);
      g1[to].clear();
    }
    int sum1 =
        getsum(0, N) - getsum(0, cur - 1) + getsum(1, N) - getsum(1, cur - 1);
    int sum2 = getsum(0, cur - 1) + getsum(1, cur - 1);
    if ((sum1 == 0) && (sum2 == 0))
      dir = 0;
    else if (sum1 >= sum2)
      dir = 1;
    else
      dir = -1;
  }
  for (int i = (0); i < (n); ++i) printf("%I64d\n", ans[i]);
  return 0;
}
