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