#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int inf = 1 << 30;
const long long Inf = 1LL << 62;
inline int Rand() { return (rand() << 16) | rand(); }
inline int sign(int x) { return (x > 0) - (x < 0); }
inline int comp(int x, int y) { return (x > y) - (x < y); }
inline int flp2(int x) {
  x |= x >> 1;
  x |= x >> 2;
  x |= x >> 4;
  x |= x >> 8;
  x |= x >> 16;
  return x - (x >> 1);
}
inline int clp2(int x) {
  x--;
  x |= x >> 1;
  x |= x >> 2;
  x |= x >> 4;
  x |= x >> 8;
  x |= x >> 16;
  return x + 1;
}
inline bool deg2(int x) { return (x & (x - 1)) == 0; }
inline bool eq(double a, double b) { return fabs(a - b) <= eps; }
const int N = 101010;
int i, s[N], f[N], t[N], n, cur, dir, lvl = 1, ans[N], m;
bool ok, was[N];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d", s + i, f + i, t + i);
    if (s[i] == f[i]) {
      was[i] = 1;
      ans[i] = t[i];
    }
  }
  while (1) {
    ok = 1;
    int mini = inf;
    for (i = 1; i <= n; i++)
      if (!was[i]) {
        ok = 0;
        mini = min(mini, t[i]);
      }
    if (ok) break;
    int d = max(0, (mini - cur - m) / (m - 1));
    cur += d * (m - 1);
    dir = (dir + (d & 1)) & 1;
    if (!dir) {
      for (i = 1; i <= n; i++)
        if (!was[i] && cur + s[i] - 1 >= t[i]) {
          was[i] = 1;
          if (s[i] < f[i])
            ans[i] = cur + f[i] - 1;
          else
            ans[i] = cur + m + m - f[i] - 1;
        }
    } else {
      for (i = 1; i <= n; i++)
        if (!was[i] && t[i] <= cur + m - s[i]) {
          was[i] = 1;
          if (s[i] > f[i])
            ans[i] = cur + m - f[i];
          else
            ans[i] = cur + m - 2 + f[i];
        }
    }
    dir ^= 1;
    cur += m - 1;
  }
  for (i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
