#include <bits/stdc++.h>
using namespace std;
struct man {
  int x, s, t;
  int num;
  void read(int _num) {
    scanf("%d%d%d", &x, &s, &t);
    --s, --t;
    num = _num;
  }
  inline bool operator<(const man &m) const { return x < m.x; }
};
const int maxn = (int)1e5;
man men[maxn];
long long ans[maxn];
long long x;
int iterMen;
multiset<pair<int, int> > outside;
int outlower, outupper;
int pos;
int speed;
multiset<pair<int, int> > inside;
int lower, upper;
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) >= 1) {
    for (int i = 0; i < n; i++) men[i].read(i);
    sort(men, men + n);
    x = -1;
    pos = 0;
    lower = upper = 0;
    outlower = outupper = 0;
    speed = 0;
    iterMen = 0;
    outside.clear(), inside.clear();
    while (1) {
      long long xout = ((long long)1e18);
      if (speed) {
        multiset<pair<int, int> >::iterator iter =
            inside.lower_bound(make_pair(pos, -1));
        if (speed == -1) {
          if (iter == inside.begin())
            iter = inside.end();
          else
            iter--;
        }
        if (iter != inside.end()) xout = abs(iter->first - pos);
      }
      long long xin = ((long long)1e18);
      if (speed) {
        multiset<pair<int, int> >::iterator iter =
            outside.lower_bound(make_pair(pos, -1));
        if (speed == -1) {
          if (iter == outside.begin())
            iter = outside.end();
          else
            iter--;
        }
        if (iter != outside.end()) xin = abs(iter->first - pos);
      }
      long long xnew = ((long long)1e18);
      if (iterMen != n) xnew = men[iterMen].x - x;
      long long xpl = min(xnew, min(xin, xout));
      if (xpl == ((long long)1e18)) break;
      assert(xpl > 0);
      x += xpl;
      pos += xpl * speed;
      if (xout == xpl) {
        while (1) {
          multiset<pair<int, int> >::iterator iter =
              inside.lower_bound(make_pair(pos, -1));
          if (iter == inside.end() || iter->first != pos) break;
          int cur = iter->second;
          inside.erase(iter);
          assert(speed);
          if (speed == 1)
            upper--;
          else
            lower--;
          ans[men[cur].num] = x;
        }
      }
      if (xnew == xpl) {
        while (iterMen < n && men[iterMen].x == x) {
          outside.insert(make_pair(men[iterMen].s, iterMen));
          if (men[iterMen].s < pos) outlower++;
          if (men[iterMen].s > pos) outupper++;
          if (men[iterMen].s == pos) xin = xpl;
          iterMen++;
        }
      }
      if (xin == xpl) {
        while (1) {
          multiset<pair<int, int> >::iterator iter =
              outside.lower_bound(make_pair(pos, -1));
          if (iter == outside.end() || iter->first != pos) break;
          int cur = iter->second;
          outside.erase(iter);
          if (men[cur].x < x) {
            assert(speed);
            if (speed == 1)
              outupper--;
            else
              outlower--;
          }
          inside.insert(make_pair(men[cur].t, cur));
          assert(men[cur].t != pos);
          if (men[cur].t > pos)
            upper++;
          else
            lower++;
        }
      }
      if (upper + outupper + lower + outlower) {
        if (upper + outupper >= lower + outlower)
          speed = 1;
        else
          speed = -1;
      } else
        speed = 0;
    }
    for (int i = 0; i < n; i++)
      printf(
          "%lld"
          "\n",
          ans[i]);
    break;
  }
  return 0;
}
