#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const long long INF64 = 7e18;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979310000;
const long long MD = 535355424;
const int TT = 1e5 * 3;
const int N = 100100;
const int M = 51;
struct st {
  long long t, s, f, num;
};
bool operator<(st a, st b) { return (a.t < b.t); }
int n, m;
st a[N];
long long ex[N], t, f = 1, ok, up, down;
vector<int> in[N], out[N];
set<long long> ins, outs;
set<long long>::iterator first, second;
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n); i++) {
    scanf("%I64d %I64d %I64d", &a[i].t, &a[i].s, &a[i].f);
    a[i].num = i;
  }
  sort(a, a + n);
  int j = 0;
  while (ok < n) {
    while (j < n && t >= a[j].t) {
      in[a[j].s].push_back(j);
      if (a[j].s > f)
        up++;
      else if (a[j].s < f)
        down++;
      ins.insert(a[j].s);
      j++;
    }
    if (ok == j) {
      t = a[j].t;
      continue;
    }
    for (int i = 0; i < int(int((out[f]).size())); i++) {
      int v = out[f][i];
      ex[a[v].num] = t;
      ok++;
    }
    out[f].clear();
    outs.erase(f);
    for (int i = 0; i < int(int((in[f]).size())); i++) {
      int v = in[f][i];
      if (a[v].f > f)
        up++;
      else
        down++;
      outs.insert(a[v].f);
      out[a[v].f].push_back(v);
    }
    in[f].clear();
    ins.erase(f);
    if (ok == j) {
      t = a[j].t;
      continue;
    }
    first = ins.lower_bound(f);
    second = outs.lower_bound(f);
    if (up >= down) {
      long long z = INF;
      if (first != ins.end()) z = min(z, *first);
      if (second != outs.end()) z = min(z, *second);
      if (j < n && t + abs(f - z) > a[j].t) z = f + (a[j].t - t);
      t += abs(f - z);
      f = z;
      up -= int((out[f]).size()) + int((in[f]).size());
    } else {
      long long z = 1;
      if (first != ins.begin()) {
        first--;
        z = max(z, *first);
      }
      if (second != outs.begin()) {
        second--;
        z = max(z, *second);
      }
      if (j < n && t + abs(f - z) > a[j].t) z = f - (a[j].t - t);
      t += abs(f - z);
      f = z;
      down -= int((out[f]).size()) + int((in[f]).size());
    }
  }
  for (int i = 0; i < int(n); i++) printf("%I64d\n", ex[i]);
  return 0;
}
