#include <bits/stdc++.h>
using namespace std;
struct shit {
  int fen[100500];
  int size;
  multiset<int> s;
  shit() {
    size = 0;
    memset(fen, 0, sizeof(fen));
  }
  void inc(int i, int d) {
    for (; i < 100500; i |= i + 1) fen[i] += d;
  }
  int sum(int i) {
    int r = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1) r += fen[i];
    return r;
  }
  void add(int floor) {
    inc(floor, 1);
    s.insert(floor);
  }
  void remove(int floor) {
    inc(floor, -1);
    s.erase(s.find(floor));
  }
  int getLess(int floor) { return sum(floor - 1); }
  int nearestUp(int floor) {
    multiset<int>::iterator it = s.lower_bound(floor);
    if (it == s.end())
      return 100500;
    else
      return *it;
  }
  int nearestDown(int floor) {
    multiset<int>::iterator it = s.upper_bound(floor);
    if (it == s.begin())
      return -1;
    else
      return *--it;
  }
};
int n, m;
int t[100500];
int s[100500], f[100500];
long long ans[100500];
shit inside;
shit outside;
priority_queue<long long, vector<long long>, greater<long long>> q;
map<long long, vector<int>> appear;
vector<int> waitInside[100500], waitOutside[100500];
int main() {
  cin >> n >> m;
  for (int i = 0; i < ((int)(n)); i++) {
    scanf("%d%d%d", &t[i], &s[i], &f[i]);
    q.push(t[i]);
    appear[t[i]].push_back(i);
  }
  long long prevT = 0;
  int v = 0;
  int curFloor = 1;
  while (!q.empty()) {
    long long t = q.top();
    curFloor += v * int(t - prevT);
    prevT = t;
    q.pop();
    vector<int> &ap = appear[t];
    for (int i = 0; i < ((int)(((int)((ap).size())))); i++) {
      int ind = ap[i];
      outside.add(s[ind]);
      waitOutside[s[ind]].push_back(ind);
    }
    ap.clear();
    while (!waitOutside[curFloor].empty()) {
      int ind = waitOutside[curFloor].back();
      waitOutside[curFloor].pop_back();
      waitInside[f[ind]].push_back(ind);
      inside.add(f[ind]);
      outside.remove(curFloor);
    }
    while (!waitInside[curFloor].empty()) {
      inside.remove(curFloor);
      int ind = waitInside[curFloor].back();
      waitInside[curFloor].pop_back();
      ans[ind] = t;
    }
    if (inside.s.empty() && outside.s.empty())
      v = 0;
    else {
      int pDown = inside.getLess(curFloor) + outside.getLess(curFloor);
      int pUp =
          ((int)((inside.s).size())) + ((int)((outside.s).size())) - pDown;
      if (pUp >= pDown)
        v = 1;
      else
        v = -1;
    }
    long long destTime;
    if (v == 1) {
      int dest = min(inside.nearestUp(curFloor), outside.nearestUp(curFloor));
      destTime = t + (dest - curFloor);
    } else if (v == -1) {
      int dest =
          max(inside.nearestDown(curFloor), outside.nearestDown(curFloor));
      destTime = t + (curFloor - dest);
    }
    if (v && (q.empty() || destTime < q.top())) q.push(destTime);
  }
  for (int i = 0; i < ((int)(n)); i++) cout << ans[i] << '\n';
}
