#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = acos(-1.);
template <class T>
inline T sq(T x) {
  return x * x;
}
template <class T>
inline void upd_min(T &lhs, T rhs) {
  if (lhs > rhs) lhs = rhs;
}
template <class T>
inline void upd_max(T &lhs, T rhs) {
  if (lhs < rhs) lhs = rhs;
}
void solve() {
  int n, m;
  cin >> n >> m;
  int T = m - 1 << 1;
  for (int i = (0); i < (n); ++i) {
    int s, f, t;
    cin >> s >> f >> t;
    if (s == f) {
      cout << t << endl;
    }
    if (s < f) {
      --s;
      --f;
      int k = (t - s + T - 1) / T;
      cout << (k * T + f) << endl;
    }
    if (s > f) {
      s = m - s;
      f = m - f;
      t -= m - 1;
      int k = (t - s + T - 1) / T;
      cout << (k * T + f + m - 1) << endl;
    }
  }
}
int main() {
  solve();
  return 0;
}
