#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
  int x, y, z;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    if (x == y) {
      cout << z << endl;
      continue;
    }
    long long int cnt = 0, l = x - 1, r = 1;
    while (l < z) {
      if (r == 1)
        l = l + (m - x) * 2;
      else
        l = l + (x - 1) * 2;
      r = 3 - r;
    }
    if (r == 1) {
      if (x < y) {
        cout << l + y - x << endl;
      } else {
        cout << l + (m - x) + (m - y) << endl;
      }
    } else {
      if (x > y) {
        cout << l + x - y << endl;
      } else {
        cout << l + (x - 1) * 2 + (y - x) << endl;
      }
    }
  }
  return 0;
}
