#include <bits/stdc++.h>
using namespace std;
struct data {
  int t, s, f, id, ans;
  data(int a, int b, int c, int d) {
    t = a;
    s = b;
    f = c;
    id = d;
  }
  bool friend operator<(data A, data B) { return A.t < B.t; }
};
vector<data> v;
int n, m;
long long BIT[200005 + 5];
vector<long long> in[200005], out[200005];
set<long long> st;
long long read(int x) {
  long long ret = 0;
  for (int i = x; i > 0; i -= i & -i) ret += BIT[i];
  return ret;
}
void add(int x, long long v) {
  for (int i = x; i <= 200005; i += i & -i) BIT[i] += v;
}
long long res[200005 + 5];
int main() {
  int i, j, k, t, x, y;
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> t >> x >> y;
    v.push_back(data(t, x, y, i));
  }
  sort(v.begin(), v.end());
  st.insert(m + 2);
  st.insert(0);
  long long cnt = (3 * n);
  long long fn = 1;
  long long tm = 1;
  i = 0;
  while (cnt) {
    while (i < n && v[i].t == tm) {
      in[v[i].s].push_back((long long)i);
      add(v[i].s, 1);
      st.insert(v[i].s);
      i++;
      cnt--;
    }
    int sz = in[fn].size();
    for (int j = 0; j < sz; j++) {
      long long y = in[fn][j];
      out[v[y].f].push_back(y);
      add(v[y].s, -1);
      add(v[y].f, 1);
      st.insert(v[y].f);
      cnt--;
    }
    in[fn].clear();
    sz = out[fn].size();
    for (int j = 0; j < sz; j++) {
      long long y = out[fn][j];
      add(v[y].f, -1);
      res[v[y].id] = tm;
      cnt--;
    }
    out[fn].clear();
    st.erase(fn);
    long long up = read(200005) - read(fn);
    long long dn = read(fn - 1);
    long long dx, jmp = INT_MAX - 1;
    if (up == 0 && dn == 0)
      dx = 0;
    else
      dx = (up >= dn ? 1 : -1);
    if (dx) {
      set<long long>::iterator it;
      it = st.upper_bound(fn);
      if (dx < 0) it--;
      int idd = (*it);
      if (idd > m) idd = m;
      if (idd < 1) idd = 1;
      jmp = min(jmp, abs(idd - fn));
    }
    if (i < n) jmp = min(jmp, v[i].t - tm);
    fn += (dx * jmp);
    tm += jmp;
  }
  for (i = 1; i <= n; i++) {
    cout << res[i] << endl;
  }
  return 0;
}
