#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 5;
long long ans[N];
struct person {
  int t, s, f, num;
  void read() { scanf("%d%d%d", &t, &s, &f); }
  bool operator<(const person &b) const { return t < b.t; }
} a[N];
int n, m, X, D, sl, sr;
long long T;
multiset<pair<long long, int> > s;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) a[i].num = i, a[i].read();
  sort(a + 1, a + n + 1);
  T = 0;
  X = 1;
  D = 0;
  int i = 1;
  while (i <= n || !s.empty()) {
    multiset<pair<long long, int> >::iterator tmp;
    if (D == 1)
      tmp = s.lower_bound(pair<long long, int>(X, -N));
    else if (D == -1) {
      tmp = s.upper_bound(pair<long long, int>(X, +N));
      if (tmp == s.begin())
        tmp = s.end();
      else
        --tmp;
    } else
      tmp = s.end();
    if (i > n || tmp != s.end() && (tmp->first - X) * D <= a[i].t - T) {
      T += (tmp->first - X) * D;
      int tx = X;
      X = tmp->first;
      if (X < tx) {
        multiset<pair<long long, int> >::iterator
            beg = s.lower_bound(pair<long long, int>(X, -N)),
            end = s.lower_bound(pair<long long, int>(X, +N));
        for (; beg != end; beg++) sl--, sr++;
      }
      if (tmp->second > 0) {
        s.insert(pair<long long, int>(a[tmp->second].f, -tmp->second));
        if (a[tmp->second].f >= X)
          sr++;
        else
          sl++;
      } else {
        ans[a[-tmp->second].num] = T;
      }
      if (tmp->first >= X)
        sr--;
      else
        sl--;
      s.erase(tmp);
    } else {
      X += (a[i].t - T) * D;
      T = a[i].t;
      s.insert(pair<long long, int>(a[i].s, i));
      if (a[i].s >= X)
        sr++;
      else
        sl++;
      i++;
    }
    if (sr == 0 && sl == 0)
      D = 0;
    else if (sr >= sl)
      D = 1;
    else
      D = -1;
  }
  for (int i = 1; i <= n; i++) printf("%I64d\n", ans[i]);
}
