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