#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3fffffff;
const int mod = 1000000007;
template <class T>
inline void read(T &first) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (first = 0; c > 32; c = getchar()) first = first * 10 + c - '0';
  if (fu) first = -first;
};
template <class T>
inline void read(T &first, T &second) {
  read(first);
  read(second);
}
template <class T>
inline void read(T &first, T &second, T &z) {
  read(first);
  read(second);
  read(z);
}
template <class T>
inline void read(T &first, T &second, T &z, T &q) {
  read(first);
  read(second);
  read(z);
  read(q);
}
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long powmod(long long a, long long b, long long mod) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 111111, M = 111111;
int l, m, n, C, c[2][2], fl = 1;
long long t;
struct zcc {
  int first, second, id;
  long long t, ans;
} a[N];
inline bool cmp(const zcc &a, const zcc &b) { return a.t < b.t; }
inline bool cmpx(const zcc &a, const zcc &b) { return a.id < b.id; }
multiset<pair<int, int> > Si, So;
void add(int first, int second) {
  if (first == 0) {
    Si.insert(make_pair(a[second].second, second));
    c[0][a[second].second > fl ? 1 : 0]++;
  } else {
    So.insert(make_pair(a[second].first, second));
    c[1][a[second].first > fl ? 1 : 0]++;
  }
}
int main() {
  scanf("%d%*d", &n);
  for (int i = (1); i <= (n); ++i)
    scanf("%I64d%d%d", &a[i].t, &a[i].first, &a[i].second), a[i].id = i;
  sort(a + 1, a + 1 + n, cmp);
  Si.insert(make_pair(-inf, 0));
  Si.insert(make_pair(inf, 0));
  So.insert(make_pair(-inf, 0));
  So.insert(make_pair(inf, 0));
  a[n + 1].t = (1ll << 60);
  int pt = 1;
  while (1) {
    long long tmp = 0;
    if (c[0][0] + c[1][0] > c[0][1] + c[1][1])
      tmp = fl - max((--Si.lower_bound(make_pair(fl, 0)))->first,
                     (--So.lower_bound(make_pair(fl, 0)))->first);
    else
      tmp = min((Si.lower_bound(make_pair(fl, inf)))->first,
                (So.lower_bound(make_pair(fl, inf)))->first) -
            fl;
    if (Si.size() == 2 && So.size() == 2) {
      if (pt > n) break;
      if (a[pt].first == fl)
        add(0, pt);
      else
        add(1, pt);
      t = a[pt].t;
      ++pt;
      continue;
    }
    if (tmp + t > a[pt].t) {
      if (c[0][0] + c[1][0] > c[0][1] + c[1][1])
        fl -= a[pt].t - t;
      else
        fl += a[pt].t - t;
      if (a[pt].first == fl)
        add(0, pt);
      else
        add(1, pt);
      t = a[pt].t;
      ++pt;
    } else {
      t += tmp;
      multiset<pair<int, int> >::iterator it;
      if (c[0][0] + c[1][0] > c[0][1] + c[1][1]) {
        fl -= tmp;
        for (multiset<pair<int, int> >::iterator it =
                 Si.lower_bound(make_pair(fl, 0));
             it->first == fl; ++it) {
          c[0][0]--;
          a[it->second].ans = t;
        }
        for (multiset<pair<int, int> >::iterator it =
                 So.lower_bound(make_pair(fl, 0));
             it->first == fl; ++it) {
          c[1][0]--;
          Si.insert(make_pair(a[it->second].second, it->second));
          c[0][a[it->second].second > fl ? 1 : 0]++;
        }
        while (it = Si.lower_bound(make_pair(fl, 0)), it->first == fl)
          Si.erase(it);
        while (it = So.lower_bound(make_pair(fl, 0)), it->first == fl)
          So.erase(it);
      } else {
        fl += tmp;
        for (multiset<pair<int, int> >::iterator it =
                 Si.lower_bound(make_pair(fl, 0));
             it->first == fl; ++it) {
          c[0][1]--;
          a[it->second].ans = t;
        }
        for (multiset<pair<int, int> >::iterator it =
                 So.lower_bound(make_pair(fl, 0));
             it->first == fl; ++it) {
          c[1][1]--;
          Si.insert(make_pair(a[it->second].second, it->second));
          c[0][a[it->second].second > fl ? 1 : 0]++;
        }
        while (it = Si.lower_bound(make_pair(fl, 0)), it->first == fl)
          Si.erase(it);
        while (it = So.lower_bound(make_pair(fl, 0)), it->first == fl)
          So.erase(it);
      }
    }
  }
  sort(a + 1, a + 1 + n, cmpx);
  for (int i = (1); i <= (n); ++i) printf("%I64d\n", a[i].ans);
  return 0;
}
