#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  for (int x = 0; x < n; x++) {
    int s, f, t;
    int k = 0;
    cin >> s >> f >> t;
    if (f > s) {
      while (t > (k * (2 * m - 2) + (s - 1))) k++;
      cout << (k * (2 * m - 2) + (s - 1)) + (f - s) << "\n";
    } else if (f < s) {
      while (t > (k * (2 * m - 2) + (2 * m - 1 - s))) k++;
      cout << (k * (2 * m - 2) + (2 * m - 1 - s)) + (s - f) << "\n";
    } else {
      cout << t << "\n";
    }
  }
}
