#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
using namespace std;
double const PI = acos(-1.0);
double const EPS = 1e-7;
struct RMQ {
  int a[100100];
  int n;
  RMQ() {
    memset(a, 0, sizeof(a));
    n = 100100 - 1;
  }
  void set(int N) {
    n = N;
    memset(a, 0, sizeof(a));
  }
  void add(int x, int v) {
    for (; x <= n; x = x | (x + 1)) {
      a[x] += v;
    }
  }
  int get(int x) {
    int res = 0;
    for (; x >= 0; x = (x & (x + 1)) - 1) {
      res += a[x];
    }
    return res;
  }
  int get(int l, int r) {
    int res = get(r);
    if (l != 0) res -= get(l - 1);
    return res;
  }
  int findUp(int x) {
    int l = x, r = n;
    while (r - l > 1) {
      int m = (r + l) / 2;
      if (get(x, m) == 0)
        l = m;
      else
        r = m;
    }
    return r;
  }
  int findDown(int x) {
    int l = 0, r = x;
    while (r - l > 1) {
      int m = (r + l) / 2;
      if (get(m, x) == 0)
        r = m;
      else
        l = m;
    }
    return l;
  }
};
struct Q {
  int from;
  int to;
  int num;
  Q(int _from = 0, int _to = 0, int _num = 0)
      : from(_from), to(_to), num(_num) {}
};
map<int, vector<Q> > m;
RMQ rmq;
vector<Q> wait[100100];
vector<int> go[100100];
long long res[100100];
bool fictive;
int main() {
  int n, M;
  scanf("%d%d", &n, &M);
  rmq.set(M);
  int minT = M;
  for (int i = 0; i < n; i++) {
    int t, s, f;
    scanf("%d%d%d", &t, &s, &f);
    m[t].push_back(Q(s, f, i));
    minT = min(minT, t);
  }
  int floor = 1;
  long long time = minT;
  map<int, vector<Q> >::iterator it = m.begin();
  int CNT = n;
  int diff = 0;
  long long oldTime = 0;
  while (1) {
    if (CNT == 0) break;
    if (it != m.end() && it->first <= time) {
      for (int i = 0; i < it->second.size(); i++) {
        wait[it->second[i].from].push_back(it->second[i]);
        rmq.add(it->second[i].from, 1);
      }
      time = it->first;
      it++;
    }
    floor += diff * (time - oldTime);
    int cnt = go[floor].size();
    rmq.add(floor, -cnt);
    for (int i = 0; i < go[floor].size(); i++) {
      res[go[floor][i]] = time;
      CNT--;
    }
    go[floor].clear();
    cnt = wait[floor].size();
    rmq.add(floor, -cnt);
    for (int i = 0; i < wait[floor].size(); i++) {
      rmq.add(wait[floor][i].to, 1);
      go[wait[floor][i].to].push_back(wait[floor][i].num);
    }
    wait[floor].clear();
    int up = rmq.get(floor, M);
    int down = rmq.get(0, floor);
    if (up == down && up == 0) {
      diff = 0;
      oldTime = time;
      time = 1000000000;
    } else if (up >= down) {
      int x = rmq.findUp(floor);
      oldTime = time;
      time += x - floor;
      diff = 1;
    } else {
      int x = rmq.findDown(floor);
      oldTime = time;
      time += floor - x;
      diff = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%I64d\n", res[i]);
  }
}
