#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 62;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= 1000000007;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
  }
  return res;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct person {
  long long t;
  int s, f;
  int id;
  bool operator<(const person &a) const { return t < a.t; }
} q[200005];
int main() {
  int n = read(), m = read();
  for (int i = 0; i < n; i++) {
    q[i].t = read(), q[i].s = read(), q[i].f = read();
    q[i].id = i;
  }
  sort(q, q + n);
  int an = 0;
  int now = 1, num = 0;
  long long curt = 0;
  set<pair<int, int> > s1, s2;
  static long long ans[200005];
  while (an < n || !s1.empty() || !s2.empty()) {
    int dt = m;
    if (an < n) chkmin(dt, (int)(q[an].t - curt));
    auto it1 = s1.lower_bound({now, 0});
    auto it2 = s2.lower_bound({now, 0});
    int d;
    if (s1.empty() && s2.empty()) {
      d = 0;
    } else if (num >= 0) {
      d = 1;
      if (it1 != s1.end()) chkmin(dt, it1->first - now);
      if (it2 != s2.end()) chkmin(dt, it2->first - now);
    } else {
      d = -1;
      if (it1 != s1.begin()) {
        auto it = it1;
        it--;
        chkmin(dt, now - it->first);
      }
      if (it2 != s2.begin()) {
        auto it = it2;
        it--;
        chkmin(dt, now - it->first);
      }
    }
    curt += dt, now += d * dt;
    while (an < n && q[an].t == curt) {
      s1.insert({q[an].s, an});
      if (q[an].s > now)
        num++;
      else if (q[an].s < now)
        num--;
      else
        num += d;
      an++;
    }
    while (1) {
      it2 = s2.lower_bound({now, 0});
      if (it2 == s2.end() || it2->first != now) break;
      num -= d;
      ans[q[it2->second].id] = curt;
      s2.erase(it2);
    }
    while (1) {
      it1 = s1.lower_bound({now, 0});
      if (it1 == s1.end() || it1->first != now) break;
      num -= d;
      s2.insert({q[it1->second].f, it1->second});
      if (q[it1->second].f > now)
        num++;
      else
        num--;
      s1.erase(it1);
    }
  }
  for (int i = 0; i < n; i++) printf("%lld\n", ans[i]);
  return 0;
}
