#include <bits/stdc++.h>
using namespace std;
int n, m;
int s, f, t;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> s >> f >> t;
    --s;
    --f;
    if (s == f) {
      cout << t << endl;
      continue;
    }
    if (s < f) {
      int num = s;
      while (num < t) num += 2 * (m - 1);
      while (f < num) f += 2 * (m - 1);
      cout << f << endl;
    } else {
      int num = (m - 1) + (m - 1 - s);
      while (num < t) num += 2 * (m - 1);
      f = (m - 1) + (m - 1 - f);
      while (f < num) f += 2 * (m - 1);
      cout << f << endl;
    }
  }
  return 0;
}
