#include <bits/stdc++.h>
using namespace std;
struct node {
  int st, en, id;
  long long T;
  int operator<(const node &o) const { return T < o.T; }
};
int n, m;
node a[201010];
int total, Pup, Pdown;
int id = 0, pos = 1;
long long Time = 0;
vector<int> Go_in[201010], Go_out[201010];
int C[201010];
long long ans[101010];
int ask(int x) {
  if (!x) return 0;
  int ans = 0;
  for (int i = x; i; i -= ((i) & (-i))) ans += C[i];
  return ans;
}
void ins(int x, int value) {
  for (int i = x; i <= m; i += ((i) & (-i))) C[i] += value;
}
int query(int L, int R) {
  if (L > R) return 0;
  return ask(R) - ask(L - 1);
}
void newGuest(int id) {
  if (a[id].st == pos) {
    ins(a[id].en, 1);
    Go_out[a[id].en].push_back(a[id].id);
  } else {
    ins(a[id].st, 1);
    Go_in[a[id].st].push_back(id);
  }
}
void process() {
  for (; Go_out[pos].size();) {
    int cur = Go_out[pos].back();
    ans[cur] = Time;
    ins(pos, -1);
    Go_out[pos].pop_back();
  }
  for (; Go_in[pos].size();) {
    int cur = Go_in[pos].back();
    ins(pos, -1);
    Go_in[pos].pop_back();
    ins(a[cur].en, 1);
    Go_out[a[cur].en].push_back(a[cur].id);
  }
}
int findLower(int L, int R) {
  int ans;
  for (int lo = L, hi = R; lo <= hi;) {
    int mid = (lo + hi) / 2;
    if (query(L, mid))
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  return ans;
}
int findUpper(int L, int R) {
  int ans;
  for (int lo = L, hi = R; lo <= hi;) {
    int mid = (lo + hi) / 2;
    if (query(mid, R))
      ans = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%I64d%d%d", &a[i].T, &a[i].st, &a[i].en), a[i].id = i;
  sort(a + 1, a + 1 + n);
  memset(C, 0, sizeof(C));
  for (int i = 1; i <= m; i++) Go_in[i].clear(), Go_out[i].clear();
  for (;;) {
    Pup = query(pos + 1, m);
    Pdown = query(1, pos - 1);
    if (!Pup && !Pdown && id == n) break;
    if (!Pup && !Pdown) {
      Time = a[++id].T;
      for (;;) {
        newGuest(id);
        if (id == n || a[id + 1].T != Time) break;
        id++;
      }
      continue;
    }
    if (Pup >= Pdown) {
      int tmp = findLower(pos + 1, m);
      if (id < n && (long long)tmp - pos > a[id + 1].T - Time) {
        pos += (long long)a[++id].T - Time;
        Time = a[id].T;
        for (;;) {
          newGuest(id);
          if (id == n || a[id + 1].T != Time) break;
          id++;
        }
      } else {
        Time += (long long)(tmp - pos);
        pos = tmp;
      }
      process();
    } else {
      int tmp = findUpper(1, pos - 1);
      if (id < n && (long long)pos - tmp > a[id + 1].T - Time) {
        pos -= (long long)a[++id].T - Time;
        Time = a[id].T;
        for (;;) {
          newGuest(id);
          if (id == n || a[id + 1].T != Time) break;
          id++;
        }
      } else {
        Time += (long long)(pos - tmp);
        pos = tmp;
      }
      process();
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << endl;
  return 0;
}
