#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = (0); i < ((int)(n)); i++) {
    int s, f, t;
    scanf("%d%d%d", &s, &f, &t);
    if (s == f) {
      cout << t << endl;
      continue;
    }
    s--;
    f--;
    int v = t % (m + m - 2);
    int S = s;
    if (S < v) S = (m - 1) + (m - 1) - s;
    if (S < v) S = m + m - 2 + s;
    int res = t + S - v;
    v = S;
    int F = f;
    if (F < v) F = (m - 1) + (m - 1) - f;
    if (F < v) F = m + m - 2 + f;
    if (F < v) F = F + (m - 1) - f + (m - 1) - f;
    if (F < v) F = 2 * (m + m - 2 + f);
    cout << res + F - v << endl;
  }
}
