#include <bits/stdc++.h>
using namespace std;
struct person {
  int t, s, f, id;
  person() {}
  person(int t1, int s1, int f1, int id1) : t(t1), s(s1), f(f1), id(id1) {}
};
int N, M;
person P[(100005)];
long long sol[(100005)];
int bitin[(200005)], bitout[(200005)];
set<pair<int, int> > pin, pout;
bool cmpfp(person p1, person p2) { return p1.t < p2.t; }
void update_bit(int *b, int pos, int v) {
  while (pos < (200005)) {
    b[pos] += v;
    pos += pos & -pos;
  }
}
int read_bit(int *b, int pos) {
  int sum = 0;
  while (pos > 0) {
    sum += b[pos];
    pos -= pos & -pos;
  }
  return sum;
}
int main() {
  int i, j;
  scanf("%d %d", &N, &M);
  for (i = 0; i < N; ++i) {
    int t, s, f;
    scanf("%d %d %d", &t, &s, &f);
    P[i] = person(t, s, f, i);
  }
  sort(P, P + N, cmpfp);
  long long curt = 0;
  int floor = 1;
  int incount = 0;
  int outcount = 0;
  i = 0;
  j = 0;
  while (1) {
    while (i < N && curt == P[i].t) {
      update_bit(bitout, P[i].s, 1);
      pout.insert(pair<int, int>(P[i].s, i));
      ++outcount;
      ++i;
    }
    int k = 0;
    set<pair<int, int> >::iterator s, s1, s2, s3;
    s1 = pin.lower_bound(pair<int, int>(floor, 0));
    s2 = pin.lower_bound(pair<int, int>(floor + 1, 0));
    for (s = s1; s != pin.end() && s != s2;) {
      ++k;
      sol[P[s->second].id] = curt;
      s3 = s;
      ++s;
      pin.erase(s3);
    }
    update_bit(bitin, floor, -k);
    j += k;
    incount -= k;
    k = 0;
    s1 = pout.lower_bound(pair<int, int>(floor, 0));
    s2 = pout.lower_bound(pair<int, int>(floor + 1, 0));
    for (s = s1; s != s2;) {
      ++k;
      update_bit(bitin, P[s->second].f, 1);
      pin.insert(pair<int, int>(P[s->second].f, s->second));
      s3 = s;
      ++s;
      pout.erase(s3);
    }
    update_bit(bitout, floor, -k);
    outcount -= k;
    incount += k;
    if (j == N) break;
    int pd1 = read_bit(bitout, floor);
    int pd2 = read_bit(bitin, floor);
    int pd3 = outcount - pd1;
    int pd4 = incount - pd2;
    int pup = pd3 + pd4;
    int pdown = pd1 + pd2;
    if (pup == 0 && pdown == 0) {
      curt = P[i].t;
    } else if (pup >= pdown) {
      long long t = -1;
      s1 = pout.lower_bound(pair<int, int>(floor + 1, 0));
      s2 = pin.lower_bound(pair<int, int>(floor + 1, 0));
      if (s1 != pout.end()) t = s1->first - floor + curt;
      if (s2 != pin.end()) {
        long long t1 = s2->first - floor + curt;
        if (t == -1 || t > t1) t = t1;
      }
      if (i < N && t > P[i].t) t = P[i].t;
      floor += t - curt;
      curt = t;
    } else {
      long long t = -1;
      s1 = pout.lower_bound(pair<int, int>(floor, 0));
      s2 = pin.lower_bound(pair<int, int>(floor, 0));
      if (s1 != pout.begin()) {
        --s1;
        t = floor - s1->first + curt;
      }
      if (s2 != pin.begin()) {
        --s2;
        long long t1 = floor - s2->first + curt;
        if (t == -1 || t > t1) t = t1;
      }
      if (i < N && t > P[i].t) t = P[i].t;
      floor -= t - curt;
      curt = t;
    }
  }
  for (i = 0; i < N; ++i) printf("%I64d\n", sol[i]);
  return 0;
}
