#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long n, m;
  cin >> n >> m;
  while (--n >= 0) {
    long s(0), f(0), t(0), output(0);
    cin >> s >> f >> t;
    if (s == f) {
      output = t;
    } else {
      long T = 2 * (m - 1);
      --s;
      --f;
      output = (t / T) * T;
      if (s < f && (output + s) < t) {
        output += T;
      } else if (s > f && (output + T - s) < t) {
        output += T;
      }
      output += (s < f) ? f : (T - f);
    }
    cout << output;
    ;
    cout << "\n";
  }
  return 0;
}
