#include <bits/stdc++.h>
bool DEBUG = false;
using namespace std;
int n, m, s[100000], f[100000], t[100000];
int next(int start_time, int level) {
  int current_level = start_time % (2 * m - 2) + 1;
  if (current_level <= m) {
    if (current_level <= level)
      return start_time + level - current_level;
    else
      return start_time + (m - current_level) + (m - level);
  } else {
    current_level = m - (current_level - m);
    if (current_level >= level)
      return start_time + current_level - level;
    else
      return start_time + (current_level - 1) + (level - 1);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < (n); ++i) {
    scanf("%d%d%d", &s[i], &f[i], &t[i]);
    if (s[i] == f[i]) {
      printf("%d\n", t[i]);
      continue;
    }
    int enter_time = next(t[i], s[i]);
    int exit_time = next(enter_time, f[i]);
    printf("%d\n", exit_time);
  }
  return 0;
}
