#include <bits/stdc++.h>
using namespace std;
long long int s, f, t;
long long int n, m, d;
long long int mp(long long int a) {
  if (a < m)
    return a;
  else
    return 2 * m - 2 - a;
}
long long int dist(long long int t, long long int s) {
  long long int r = t % (2 * m - 2);
  long long int mpt = mp(r) + 1;
  if (mpt == s) return 0;
  if (r >= m - 1) {
    if (mpt < s)
      return mpt + s - 2;
    else
      return mpt - s;
  } else {
    if (mpt < s)
      return s - mpt;
    else
      return m - mpt + m - s;
  }
}
int main() {
  cin >> n >> m;
  for (long long int i = 0; i < n; i++) {
    cin >> s >> f >> t;
    if (s == f) {
      cout << t << endl;
      continue;
    }
    d = dist(t, s);
    cout << t + d + dist(t + d, f) << endl;
  }
  return 0;
}
