#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, s, f, t;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s >> f >> t;
    if (s == f) {
      cout << t << "\n";
      continue;
    }
    int trem = t % (2 * (m - 1));
    int tdiv = t / (2 * (m - 1));
    int geton;
    if (t - trem + s - 1 >= t)
      geton = t - trem + s - 1;
    else if (t - trem + 2 * (m - 1) - s + 1 >= t)
      geton = t - trem + 2 * (m - 1) - s + 1;
    else
      geton = t - trem + 2 * (m - 1) + s - 1;
    int getoff;
    if (t - trem + f - 1 >= geton)
      getoff = t - trem + f - 1;
    else if (t - trem + 2 * (m - 1) - f + 1 >= geton)
      getoff = t - trem + 2 * (m - 1) - f + 1;
    else if (t - trem + 2 * (m - 1) + f - 1 >= geton)
      getoff = t - trem + 2 * (m - 1) + f - 1;
    else
      getoff = t - trem + 4 * (m - 1) - f + 1;
    cout << getoff << "\n";
  }
  return 0;
}
