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