#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int s, f, t;
  for (int i = 0; i < n; i++) {
    cin >> s >> f >> t;
    int flag = t / (m - 1);
    int yushu = t % (m - 1);
    int floor;
    if (flag & 1) {
      floor = m - yushu;
    } else
      floor = 1 + yushu;
    int totaltime = 0;
    if (s > floor) {
      if (flag % 2 == 0)
        totaltime = s - floor;
      else
        totaltime = floor - 1 + s - 1, flag = 0;
      floor = s;
    } else if (s < floor) {
      if (flag % 2 == 0) {
        totaltime = (m - floor) + (m - s);
        flag = 1;
      } else
        totaltime = floor - s;
      floor = s;
    }
    if (s == f) {
      totaltime = t;
    } else {
      if (floor > f) {
        if (flag % 2 == 0) {
          totaltime += (m - floor + m - f);
        } else
          totaltime += floor - f;
      } else if (floor < f) {
        if (flag % 2 == 0)
          totaltime += f - floor;
        else
          totaltime += floor - 1 + f - 1;
      }
      totaltime += t;
    }
    cout << totaltime << endl;
  }
  return 0;
}
