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