#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9, MAXN = (int)1e6;
const double eps = 1e-9;
int n, q, a, b, c;
long long query(long long s, long long f, long long t) {
  if (s == f) return t;
  long long tt = t % (2 * (n - 1));
  if (s < f && tt <= s - 1) {
    return t + (s - 1) - tt + f - s;
  }
  if (s < f) {
    return t + 2 * (n - 1) - tt + f - 1;
  }
  if (n - 1 + n - s >= tt) {
    return t + n - 1 + n - f - tt;
  }
  return t + 2 * (n - 1) - tt + n - 1 + n - f;
}
long long s[MAXN], f[MAXN], t[MAXN];
int main() {
  cin >> q >> n;
  for (int i = 0; i < (q); i++) cin >> s[i] >> f[i] >> t[i];
  for (int i = 0; i < (q); i++) cout << query(s[i], f[i], t[i]) << endl;
  return 0;
}
