#include <bits/stdc++.h>
using namespace std;
using PII = pair<int, int>;
using TPL = pair<PII, PII>;
using LL = long long;
const int INF = 1000000007;
using namespace std;
class BITree {
 public:
  static const int SIZE = 100010, BIAS = 5;
  long long u[SIZE];
  void clear() { memset(u, 0, sizeof(u)); }
  void modify(int x, long long v) {
    for (x += BIAS; x < SIZE; x += x & -x) u[x] += v;
  }
  long long getsum(int x) {
    long long s = 0;
    for (x += BIAS; x; x -= x & -x) s += u[x];
    return s;
  }
  long long getrev(int x) { return getsum(SIZE - BIAS - 1) - getsum(x - 1); }
} sumw, sumr;
long long ans[100000];
int T[100000], S[100000], F[100000];
int main() {
  set<PII> ride, wait, next;
  int n, m, pos = 1;
  long long now = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", T + i, S + i, F + i);
    next.insert({T[i], i});
  }
  while (1) {
    do {
      auto t = ride.lower_bound({pos, 0});
      if (t == ride.end() || t->first != pos) break;
      ans[t->second] = now;
      sumr.modify(t->first, -1);
      ride.erase(t);
    } while (1);
    do {
      auto t = wait.lower_bound({pos, 0});
      if (t == wait.end() || t->first != pos) break;
      sumr.modify(F[t->second], +1);
      ride.insert({F[t->second], t->second});
      sumw.modify(t->first, -1);
      wait.erase(t);
    } while (1);
    if (wait.empty() && ride.empty()) {
      if (next.empty()) break;
      now = next.begin()->first;
    } else {
      int up = sumr.getrev(pos + 1) + sumw.getrev(pos + 1);
      int dn = sumr.getsum(pos - 1) + sumw.getsum(pos - 1), to;
      if (up >= dn) {
        to = +INF;
        auto w = wait.lower_bound({pos + 1, 0});
        auto r = ride.lower_bound({pos + 1, 0});
        if (w != wait.end()) to = min(to, w->first);
        if (r != ride.end()) to = min(to, r->first);
        if (next.size() && abs(to - pos) + now > next.begin()->first)
          to = pos + (next.begin()->first - now);
      } else {
        to = -INF;
        auto w = wait.upper_bound({pos - 1, INF});
        auto r = ride.upper_bound({pos - 1, INF});
        if (w != wait.begin())
          --w;
        else
          w = wait.end();
        if (r != ride.begin())
          --r;
        else
          r = ride.end();
        if (w != wait.end()) to = max(to, w->first);
        if (r != ride.end()) to = max(to, r->first);
        if (next.size() && abs(to - pos) + now > next.begin()->first)
          to = pos - (next.begin()->first - now);
      }
      now += abs(pos - to);
      pos = to;
    }
    while (next.size() && next.begin()->first == now) {
      auto t = next.begin();
      sumw.modify(S[t->second], +1);
      wait.insert({S[t->second], t->second});
      next.erase(t);
    }
  }
  for (int i = 0; i < n; i++) printf("%I64d\n", ans[i]);
}
