#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
struct man {
  int t;
  int s;
  int f;
  int id;
  void read(int ID) {
    id = ID;
    scanf("%d%d%d", &t, &s, &f);
    --s, --f;
  }
  bool operator<(const man &B) const { return t < B.t; }
};
int N, M;
vector<int> t2;
vector<int> t;
int n;
int n2;
man a[1 << 17];
long long ans[1 << 17];
vector<vector<int> > q, g;
void init(int nn) {
  n = nn;
  t.assign(n, 0);
}
int sum(int r) {
  int result = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];
  return result;
}
void inc(int i, int delta) {
  for (; i < n; i = (i | (i + 1))) t[i] += delta;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
void init2(int nn) {
  n2 = nn;
  t2.assign(n, 0);
}
int sum2(int r) {
  int result = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) result += t2[r];
  return result;
}
void inc2(int i, int delta) {
  for (; i < n2; i = (i | (i + 1))) t2[i] += delta;
}
int sum2(int l, int r) { return sum2(r) - sum2(l - 1); }
inline int getNextUp(int x) {
  int l = x + 1, r = n - 1;
  int res = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    int cur = sum(x + 1, mid);
    if (!cur) cur = sum2(x + 1, mid);
    if (cur) {
      res = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  return res;
}
inline int getNextDown(int x) {
  int l = 0, r = x - 1;
  int res = -1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    int cur = sum(mid, x - 1);
    if (!cur) cur = sum2(mid, x - 1);
    if (cur) {
      res = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return res;
}
int main() {
  g.clear();
  q.clear();
  scanf("%d%d", &N, &M);
  q.resize(M);
  g.resize(M);
  init(M);
  init2(M);
  for (int i = 0; i < N; ++i) {
    a[i].read(i);
  }
  sort(a, a + N);
  int left = N;
  long long t = a[0].t;
  int pos = 0;
  int cur = 0;
  int dir = 0;
  long long pt = 0;
  while (left) {
    long long npos = pos + (long long)dir * (t - pt);
    npos = max((long long)0, npos);
    npos = min((long long)(M - 1), npos);
    pos = (int)npos;
    while (cur < N && a[cur].t <= t) {
      inc(a[cur].s, 1);
      q[a[cur].s].push_back(cur);
      ++cur;
    }
    int K = (int)q[pos].size();
    for (int i = 0; i < K; ++i) {
      int y = q[pos][i];
      inc2(a[y].f, 1);
      g[a[y].f].push_back(y);
    }
    q[pos].clear();
    if (K) inc(pos, -K);
    K = (int)g[pos].size();
    for (int i = 0; i < K; ++i) {
      int y = g[pos][i];
      ans[a[y].id] = t;
      --left;
    }
    g[pos].clear();
    if (K) inc2(pos, -K);
    if (!left) break;
    int S1 = sum(0, pos - 1) + sum2(0, pos - 1);
    int S2 = sum(pos + 1, M - 1) + sum2(pos + 1, M - 1);
    if (!S1 && !S2) {
      dir = 0;
      pt = t;
      t = a[cur].t;
      continue;
    }
    if (S1 > S2) {
      dir = -1;
      int tar = getNextDown(pos);
      long long nt = t + (pos - tar);
      pt = t;
      t = nt;
      if (cur < N) t = min(t, (long long)a[cur].t);
    } else {
      dir = 1;
      int tar = getNextUp(pos);
      long long nt = t + (tar - pos);
      pt = t;
      t = nt;
      if (cur < N) t = min(t, (long long)a[cur].t);
    }
  }
  for (int i = 0; i < N; ++i) {
    printf("%I64d\n", ans[i]);
  }
  return 0;
}
