#include <bits/stdc++.h>
using namespace std;
bool down(int t, int m) { return (t - 1) / (m - 1) % 2; }
int main() {
  int n, m, s, f, t, h;
  while (cin >> n >> m) {
    while (n--) {
      cin >> s >> f >> t;
      int a = s - 1, b = f - 1;
      if (t == 0) {
        if (a < b)
          cout << b << endl;
        else if (a > b)
          cout << m - 1 + m - 1 - b << endl;
        else
          cout << 0 << endl;
        continue;
      }
      if (a == b) {
        cout << t << endl;
        continue;
      }
      if (!down(t, m)) {
        if (t % (m - 1) == 0)
          h = m - 1;
        else
          h = t % (m - 1);
      } else {
        if (t % (m - 1) == 0)
          h = 0;
        else
          h = m - 1 - (t % (m - 1));
      }
      if (a < b) {
        if (!down(t, m)) {
          if (h > a)
            cout << t + (m - 1) - h + (m - 1) + b << endl;
          else
            cout << t + b - h << endl;
        } else {
          cout << t + h + b << endl;
        }
      } else if (a > b) {
        if (down(t, m)) {
          if (h < a)
            cout << t + h + m - 1 + m - 1 - b << endl;
          else
            cout << t + h - b << endl;
        } else {
          cout << t + m - 1 - h + m - 1 - b << endl;
        }
      } else {
        if (!down(t, m)) {
          if (h > a)
            cout << t + (m - 1) - h + (m - 1) - a << endl;
          else {
            cout << t + a - h << endl;
          }
        } else {
          if (h < a)
            cout << t + h + a << endl;
          else
            cout << t + h - a << endl;
        }
      }
    }
  }
  return 0;
}
