#include <bits/stdc++.h>
using namespace std;
const double dinf = 1e200;
void mytimer(string task) {}
void ext(int c) {}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (n); ++i) {
    int a, b, t;
    cin >> a >> b >> t;
    if (a == b) {
      cout << t << '\n';
      continue;
    }
    int res = abs(a - b);
    int t0 = (a < b) ? (a - 1) : (m - 1 + m - a);
    res += t + (2 * m - 2 - (t - t0 + 2 * m - 2) % (2 * m - 2)) % (2 * m - 2);
    cout << res << '\n';
  }
  return 0;
}
