#include <bits/stdc++.h>
using namespace std;
int waitt[100001], want[100001];
void add(int l, int k, int num) {
  if (l <= 0) return;
  if (num == 0) return;
  int* arr = (k ? waitt : want);
  for (; l <= 100000; l += (l & (-l))) arr[l] += num;
}
int que(int l) {
  int r(0);
  for (; l > 0; l -= (l & (-l))) r += (waitt[l] + want[l]);
  return r;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > info;
  info.resize(n);
  for (int i = 0; i < n; ++i) info[i].resize(4);
  for (int i = 0; i < n; ++i) {
    cin >> info[i][0] >> info[i][1] >> info[i][2];
    info[i][3] = i;
  }
  sort(info.begin(), info.end());
  long long time = info[0][0];
  int lev = 1;
  int await = 0, awant = 0;
  multimap<int, int> inele;
  multimap<int, int> waiting;
  vector<long long> ans;
  ans.resize(n);
  for (int i = 0, c = 0; i < n || waiting.size() > 0 || inele.size() > 0;) {
    pair<multimap<int, int>::iterator, multimap<int, int>::iterator> pp =
        inele.equal_range(lev);
    int num = 0;
    for (; pp.first != pp.second; ++pp.first, ++num)
      ans[info[(pp.first)->second][3]] = time;
    inele.erase(lev);
    add(lev, 0, -num);
    num = 0;
    for (; i < n && info[i][0] == time; ++i, ++num) {
      waiting.insert(pair<int, int>(info[i][1], i));
      add(info[i][1], 1, 1);
    }
    num = 0;
    pp = waiting.equal_range(lev);
    for (; pp.first != pp.second; ++pp.first, ++num) {
      inele.insert(
          pair<int, int>(info[(pp.first)->second][2], (pp.first)->second));
      add(info[pp.first->second][2], 0, 1);
    }
    waiting.erase(lev);
    add(lev, 1, -num);
    int down = que(lev - 1);
    int up = (int)(waiting.size() + inele.size()) - down;
    if (up == 0 && down == 0) {
      if (i < n) time = info[i][0];
    } else if (up >= down) {
      long long mtime = 100001;
      if (i < n) mtime = min(mtime, (long long)info[i][0] - time);
      multimap<int, int>::iterator ii = waiting.upper_bound(lev);
      if (ii != waiting.end()) mtime = min(mtime, (long long)ii->first - lev);
      ii = inele.upper_bound(lev);
      if (ii != inele.end()) mtime = min(mtime, (long long)ii->first - lev);
      time += mtime, lev += mtime;
    } else {
      long long mtime = 100001;
      if (i < n) mtime = min(mtime, (long long)info[i][0] - time);
      multimap<int, int>::iterator ii = waiting.upper_bound(lev);
      if (ii != waiting.begin()) {
        --ii;
        mtime = min(mtime, lev - (long long)ii->first);
      }
      ii = inele.upper_bound(lev);
      if (ii != inele.begin()) {
        --ii;
        mtime = min(mtime, lev - (long long)ii->first);
      }
      time += mtime, lev -= mtime;
    }
  }
  for (int i = 0; i < n; ++i) cout << ans[i] << endl;
  return 0;
}
