#include <bits/stdc++.h>
using namespace std;
int n, m, t, x, y, z, num;
int get(int x, int t) {
  int t1 = t - x + 1;
  int num1 = t1 / (m * 2 - 2) + (t1 % (m * 2 - 2) != 0);
  if (t1 <= 0) num1 = 0;
  int t2 = t - (m - 1 + m - x);
  int num2 = t2 / (m * 2 - 2) + (t2 % (m * 2 - 2) != 0);
  if (t2 <= 0) num2 = 0;
  return min(num2 * (m * 2 - 2) + (m - 1 + m - x), num1 * (m * 2 - 2) + x - 1);
}
int main() {
  cin >> n >> m;
  while (n--) {
    scanf("%d%d%d", &x, &y, &z);
    if (x == y)
      cout << z << endl;
    else {
      int tt = get(x, z);
      printf("%d\n", get(y, tt));
    }
  }
}
