#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
template <class _T>
inline string tostr(const _T& a) {
  ostringstream os("");
  os << a;
  return os.str();
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
void ASS(bool b) {
  if (!b) {
    ++*(int*)0;
  }
}
int n, m, x, y, z;
int main() {
  cout.flags(ios::fixed);
  cout.precision(2);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    if (x == y) {
      printf("%d\n", z);
      continue;
    }
    x--;
    y--;
    long long t = z / (m - 1);
    int now = z % (m - 1);
    if (t & 1) {
      now = m - 1 - now;
    }
    int mov;
    if ((t & 1) == 0)
      mov = 1;
    else
      mov = -1;
    if (now == 0) mov = 1;
    if (now == m - 1) mov = -1;
    long long ans = z;
    if (mov == 1 && now > x) {
      mov = -1;
      ans += m - 1 - now;
      now = m - 1;
    }
    if (mov == -1 && now < x) {
      mov = 1;
      ans += now;
      now = 0;
    }
    if (mov == 1) {
      ans += x - now;
      now = x;
    } else {
      ans += now - x;
      now = x;
    }
    if (now == 0) mov = 1;
    if (now == m - 1) mov = -1;
    if (mov == 1 && now > y) {
      mov = -1;
      ans += m - 1 - now;
      now = m - 1;
    }
    if (mov == -1 && now < y) {
      mov = 1;
      ans += now;
      now = 0;
    }
    if (mov == 1) {
      ans += y - now;
      now = y;
    } else {
      ans += now - y;
      now = y;
    }
    int q = ans;
    printf("%d\n", q);
  }
  return 0;
}
