#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long> vs, vt, vf;
  long long n, m, i, j, a, b, ma = -1, c, k = 1, p;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a >> b >> c;
    if (a == b)
      cout << c << endl;
    else {
      long o = 2 * m - 2;
      j = c % o;
      if (j >= m) {
        k = 2 * m - 1 - j;
        p = -1;
      } else {
        k = j + 1;
        p = 1;
      }
      if (p == 1) {
        if (a >= k)
          if (a < b)
            cout << b - k + c << endl;
          else
            cout << a - k + m - a + m - b + c << endl;
        else if (a < b)
          cout << m - 1 - k + m - 1 + b + c << endl;
        else
          cout << m - b + m - k + c << endl;
      } else {
        if (a > k)
          if (a < b)
            cout << k - 1 + b - 1 + c << endl;
          else
            cout << m - b + m - a + k - 1 + a - 1 + c << endl;
        else if (a < b)
          cout << k - 1 + b - 1 + c << endl;
        else
          cout << k - b + c << endl;
      }
    }
  }
  return 0;
}
