#include <bits/stdc++.h>
using namespace std;
int n, m;
int s, f, t;
int ret(int s, int t) {
  int tt = s - 1;
  while (tt < t) tt += 2 * (m - 1);
  return tt;
}
int ret2(int s, int t) {
  int tt = -s + 1;
  while (tt < t) tt += 2 * (m - 1);
  return tt;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &s, &f, &t);
    if (s == f) printf("%d\n", t);
    if (f > s) printf("%d\n", (f - s) + ret(s, t));
    if (f < s) printf("%d\n", (s - f) + ret2(s, t));
  }
  return 0;
}
