#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, n, m, d, m1, t, t1;
  vector<int> v;
  cin >> n >> m;
  int a[n], b[n], c[n];
  m1 = m - 1;
  for (i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
  for (i = 0; i < n; i++) {
    t1 = 0;
    if (b[i] == a[i])
      v.push_back(c[i]);
    else {
      d = 0;
      if (b[i] < a[i]) {
        t = a[i] - b[i];
        d = d + m - a[i] + m - 1;
        while (c[i] > d) d = d + 2 * m1;
        v.push_back(d + t);
      } else {
        t = b[i] - a[i];
        d = d + a[i] - 1;
        while (c[i] > d) d = d + 2 * m1;
        v.push_back(d + t);
      }
    }
  }
  for (i = 0; i < v.size(); i++) cout << v[i] << endl;
  return 0;
}
