#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
struct node {
  long long t;
  int from, to, id;
};
bool cmp(const node &a, const node &b) { return a.t < b.t; }
node waits[MAXN];
vector<node> numin[MAXN], numout[MAXN];
struct SegmentTree {
  struct Tree {
    int l, r, sum;
  };
  Tree seg[MAXN * 4];
  void init(int id, int l, int r) {
    seg[id].l = l;
    seg[id].r = r;
    seg[id].sum = 0;
    if (l != r) {
      int mid = (l + r) >> 1;
      init(id * 2, l, mid);
      init(id * 2 + 1, mid + 1, r);
    }
  }
  int size() { return seg[1].sum; }
  int query(int id, int pos, int ope) {
    if (ope == 1) {
      if (seg[id].l >= pos) return seg[id].sum;
      if (seg[id].r < pos) return 0;
      return query(id * 2, pos, ope) + query(id * 2 + 1, pos, ope);
    } else {
      if (seg[id].l > pos) return 0;
      if (seg[id].r <= pos) return seg[id].sum;
      return query(id * 2, pos, ope) + query(id * 2 + 1, pos, ope);
    }
  }
  int getpos(int id, int pos, int ope) {
    if (seg[id].sum == 0) return (ope == 1 ? MAXN : -MAXN);
    if (seg[id].l == seg[id].r) return seg[id].l;
    int mid = (seg[id].l + seg[id].r) / 2;
    if (ope == 1) {
      if (mid < pos) return getpos(id * 2 + 1, pos, ope);
      if (seg[id].l >= pos) {
        if (seg[id * 2].sum != 0)
          return getpos(id * 2, pos, ope);
        else
          return getpos(id * 2 + 1, pos, ope);
      } else
        return min(getpos(id * 2, pos, ope), getpos(id * 2 + 1, pos, ope));
    } else {
      if (mid >= pos) return getpos(id * 2, pos, ope);
      if (seg[id].r <= pos) {
        if (seg[id * 2 + 1].sum != 0)
          return getpos(id * 2 + 1, pos, ope);
        else
          return getpos(id * 2, pos, ope);
      } else
        return max(getpos(id * 2, pos, ope), getpos(id * 2 + 1, pos, ope));
    }
  }
  void Up(int id) { seg[id].sum = seg[id * 2].sum + seg[id * 2 + 1].sum; }
  void update(int id, int pos, int val) {
    if (seg[id].l == seg[id].r)
      seg[id].sum = val;
    else {
      int mid = (seg[id].l + seg[id].r) / 2;
      if (pos <= mid)
        update(id * 2, pos, val);
      else
        update(id * 2 + 1, pos, val);
      Up(id);
    }
  }
  void add(int id, int pos, int val) {
    if (seg[id].l == seg[id].r)
      seg[id].sum += val;
    else {
      int mid = (seg[id].l + seg[id].r) / 2;
      if (pos <= mid)
        add(id * 2, pos, val);
      else
        add(id * 2 + 1, pos, val);
      Up(id);
    }
  }
};
SegmentTree in, out;
long long ans[MAXN];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    waits[i].id = i;
    scanf("%I64d%d%d", &waits[i].t, &waits[i].from, &waits[i].to);
  }
  sort(waits, waits + n, cmp);
  in.init(1, 1, m);
  out.init(1, 1, m);
  int pos = 0;
  node now;
  now.t = 0;
  now.from = 1;
  while (true) {
    long long nextT = 0;
    int step, target;
    if (in.size() + out.size() + n - pos == 0) break;
    if (in.size() + out.size() != 0) {
      int pup = in.query(1, now.from, 1) + out.query(1, now.from, 1);
      int pdown = in.query(1, now.from, 0) + out.query(1, now.from, 0);
      if (pup >= pdown) {
        step = 1;
        target = min(in.getpos(1, now.from, 1), out.getpos(1, now.from, 1));
        nextT = now.t + abs(target - now.from);
      } else {
        step = -1;
        target = max(in.getpos(1, now.from, 0), out.getpos(1, now.from, 0));
        nextT = now.t + abs(target - now.from);
      }
      if (pos < n && nextT >= waits[pos].t) {
        nextT = waits[pos].t;
        target = now.from + step * (nextT - now.t);
      }
    } else {
      nextT = waits[pos].t;
      target = now.from;
    }
    while (pos < n && waits[pos].t <= nextT) {
      node x = waits[pos++];
      numout[x.from].push_back(x);
      out.add(1, x.from, 1);
    }
    in.update(1, target, 0);
    for (int i = 0; i < numin[target].size(); i++) {
      ans[numin[target][i].id] = nextT;
    }
    numin[target].clear();
    out.update(1, target, 0);
    for (int i = 0; i < numout[target].size(); i++) {
      node x = numout[target][i];
      numin[x.to].push_back(x);
      in.add(1, x.to, 1);
    }
    numout[target].clear();
    now.t = nextT;
    now.from = target;
  }
  for (int i = 0; i < n; i++) printf("%I64d\n", ans[i]);
  return 0;
}
