#include <bits/stdc++.h>
using namespace std;
struct person {
  long s;
  long f;
  long t;
  long t_fin;
};
int main() {
  long n, m;
  cin >> n >> m;
  vector<person> people(n);
  for (long i = 0; i < n; i++) cin >> people[i].s >> people[i].f >> people[i].t;
  for (long i = 0; i < n; i++) {
    if (people[i].s == people[i].f)
      people[i].t_fin = people[i].t;
    else {
      if (people[i].s > people[i].f) {
        long k = ((people[i].s + people[i].t - 1) / (2 * m - 2));
        if ((people[i].s + people[i].t - 1) % (2 * m - 2) != 0) k++;
        people[i].t_fin = (2 * k - 1) * (m - 1) + (m - people[i].f);
      } else {
        long k = (max((people[i].t - people[i].s + 1), (long)0) / (2 * m - 2));
        if (max((people[i].t - people[i].s + 1), (long)0) % (2 * m - 2) != 0)
          k++;
        people[i].t_fin = 2 * k * (m - 1) + (people[i].f - 1);
      }
    }
    cout << people[i].t_fin << "\n";
  }
}
