#include <bits/stdc++.h>
using namespace std;
const long long MaxN = 1e5 + 10;
const long long MaxNode = 4e5 + 10;
const long long inf = 2e9;
long long sum[MaxNode], minn[MaxNode], maxx[MaxNode];
inline void update(long long now) {
  sum[now] = sum[now << 1] + sum[(now << 1) + 1];
  minn[now] = min(minn[now << 1], minn[(now << 1) + 1]);
  maxx[now] = max(maxx[now << 1], maxx[(now << 1) + 1]);
}
void build(long long now, long long l, long long r) {
  if (l == r) {
    minn[now] = inf;
    maxx[now] = -inf;
    return;
  }
  long long mid = (l + r) >> 1;
  build(now << 1, l, mid);
  build((now << 1) + 1, mid + 1, r);
  update(now);
}
void change(long long now, long long l, long long r, long long x, long long v) {
  if (l == r) {
    sum[now] += v;
    if (sum[now])
      minn[now] = maxx[now] = l;
    else
      minn[now] = inf, maxx[now] = -inf;
    return;
  }
  long long mid = (l + r) >> 1;
  if (x <= mid)
    change(now << 1, l, mid, x, v);
  else
    change((now << 1) + 1, mid + 1, r, x, v);
  update(now);
}
long long query(long long now, long long l, long long r, long long Left,
                long long Right, long long v) {
  if (Left <= l && r <= Right) {
    switch (v) {
      case 0:
        return sum[now];
      case 1:
        return minn[now];
      case 2:
        return maxx[now];
    }
    return -1;
  }
  long long mid = (l + r) >> 1;
  long long curSum = 0, curMin = inf, curMax = -inf;
  if (Left <= mid) {
    long long x = query(now << 1, l, mid, Left, Right, v);
    switch (v) {
      case 0:
        curSum += x;
        break;
      case 1:
        curMin = min(curMin, x);
        break;
      case 2:
        curMax = max(curMax, x);
        break;
    }
  }
  if (Right > mid) {
    long long x = query((now << 1) + 1, mid + 1, r, Left, Right, v);
    switch (v) {
      case 0:
        curSum += x;
        break;
      case 1:
        curMin = min(curMin, x);
        break;
      case 2:
        curMax = max(curMax, x);
        break;
    }
  }
  switch (v) {
    case 0:
      return curSum;
    case 1:
      return curMin;
    case 2:
      return curMax;
  }
  return -1;
}
long long ans[MaxN], n, m, sa[MaxN];
struct person {
  long long t, s, f;
} a[MaxN];
vector<long long> wait[MaxN];
inline long long getnum() {
  long long ans = 0;
  char c;
  bool flag = false;
  while ((c = getchar()) == ' ' || c == '\n' || c == '\r')
    ;
  if (c == '-')
    flag = true;
  else
    ans = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') ans = ans * 10 + c - '0';
  return ans * (flag ? -1 : 1);
}
bool cmp(const long long &aa, const long long &bb) { return a[aa].t < a[bb].t; }
int main() {
  n = getnum();
  m = getnum();
  for (long long i = 1; i <= n; i++) {
    a[i].t = getnum();
    a[i].s = getnum();
    a[i].f = getnum();
    sa[i] = i;
  }
  sort(sa + 1, sa + 1 + n, cmp);
  build(1, 1, m);
  long long curT = 0, curF = 1, cntGo = 0, p = 1;
  while (cntGo < n) {
    while (p <= n && a[sa[p]].t == curT) {
      long long cur = sa[p];
      change(1, 1, m, a[cur].s, 1);
      wait[a[cur].s].push_back(cur);
      p++;
    }
    for (long long j = 0; j < (long long)wait[curF].size(); j++) {
      long long ps = wait[curF][j];
      if (!ans[ps]) {
        change(1, 1, m, a[ps].s, -1);
        change(1, 1, m, a[ps].f, 1);
        wait[a[ps].f].push_back(ps);
        ans[ps] = -1;
      } else {
        change(1, 1, m, a[ps].f, -1);
        ans[ps] = curT;
        cntGo++;
      }
    }
    wait[curF].clear();
    long long pUp = query(1, 1, m, curF, m, 0);
    long long pDown = query(1, 1, m, 1, curF, 0);
    if (!pUp && !pDown)
      curT = a[sa[p]].t;
    else {
      if (pUp >= pDown) {
        long long nxt = query(1, 1, m, curF, m, 1);
        long long expT = curT + nxt - curF;
        if (p <= n && a[sa[p]].t < expT) expT = a[sa[p]].t;
        curF += expT - curT;
        curT = expT;
      } else {
        long long nxt = query(1, 1, m, 1, curF, 2);
        long long expT = curT + curF - nxt;
        if (p <= n && a[sa[p]].t < expT) expT = a[sa[p]].t;
        curF -= expT - curT;
        curT = expT;
      }
    }
  }
  for (long long i = 1; i <= n; i++) printf("%I64d\n", ans[i]);
}
