#include <bits/stdc++.h>
using namespace std;
const int N = 202020;
const long long inf = 4e18;
int n, m;
long long ans[N];
pair<int, int> xy[N];
int main() {
  while (~scanf("%d%d", &n, &m)) {
    priority_queue<pair<int, int> > a, b, c, d;
    priority_queue<pair<pair<long long, int>, pair<int, int> > > e;
    for (int i = (1); i < (n + 1); i++) {
      int t, x, y;
      scanf("%d%d%d", &t, &x, &y);
      e.push(make_pair(make_pair(-t, i), make_pair(x, y)));
      xy[i] = make_pair(x, y);
    }
    ++n;
    e.push(make_pair(make_pair(-inf, n), make_pair(-1, -1)));
    long long tim = 0;
    int now = 1;
    for (int _ = (1); _ < (n + 1); _++) {
      pair<pair<long long, int>, pair<int, int> > u = e.top();
      e.pop();
      long long t = -u.first.first;
      int i = u.first.second, x = u.second.first, y = u.second.second;
      while (tim < t) {
        int up = (int)a.size() + (int)c.size(),
            down = (int)b.size() + (int)d.size();
        if (up + down == 0) {
          tim = t;
          break;
        }
        if (up >= down) {
          int to = m;
          if ((int)a.size()) to = min(to, -a.top().first);
          if ((int)c.size()) to = min(to, -c.top().first);
          int go = min((long long)(to - now), t - tim);
          tim = tim + go;
          to = now + go;
          now = to;
          while (!a.empty()) {
            pair<int, int> v = a.top();
            if (-v.first != to) break;
            a.pop();
            ans[v.second] = tim;
          }
          while (!c.empty()) {
            pair<int, int> v = c.top();
            if (-v.first != to) break;
            c.pop();
            int _ = xy[v.second].second;
            if (_ > to) {
              a.push(make_pair(-_, v.second));
            } else {
              b.push(make_pair(_, v.second));
            }
          }
        } else {
          int to = 1;
          if ((int)b.size()) to = max(to, b.top().first);
          if ((int)d.size()) to = max(to, d.top().first);
          int go = min((long long)(now - to), t - tim);
          tim = tim + go;
          to = now - go;
          now = to;
          while (!b.empty()) {
            pair<int, int> v = b.top();
            if (v.first != to) break;
            b.pop();
            ans[v.second] = tim;
          }
          while (!d.empty()) {
            pair<int, int> v = d.top();
            if (v.first != to) break;
            d.pop();
            int _ = xy[v.second].second;
            if (_ > to) {
              a.push(make_pair(-_, v.second));
            } else {
              b.push(make_pair(_, v.second));
            }
          }
        }
      }
      if (i != n) {
        if (x > now) {
          c.push(make_pair(-x, i));
        } else if (x < now) {
          d.push(make_pair(x, i));
        } else if (y > now) {
          a.push(make_pair(-y, i));
        } else {
          b.push(make_pair(y, i));
        }
      }
    }
    for (int i = (1); i < (n); i++) printf("%lld\n", ans[i]);
  }
  return 0;
}
