#include <bits/stdc++.h>
const long double pi = 3.1415926535897932384626433832795;
const int INF = (int)1E9;
const int MAXN = (int)11000;
using namespace std;
int main() {
  int n, m, a, b, c, t, cur, ans;
  bool dir;
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < (int)(n); i++) {
    scanf("%d%d%d\n", &a, &b, &c);
    ans = c;
    t = c % (2 * m - 2);
    dir = (t < m - 1);
    if (dir)
      cur = t + 1;
    else
      cur = (2 * m - 2 - t + 1);
    if (a < b) {
      if (dir) {
        if (cur > a)
          ans += ((m - cur) + (m - 1) + (a - 1));
        else
          ans += (a - cur);
      } else {
        ans += ((cur - 1) + (a - 1));
      }
      ans += (b - a);
    }
    if (a > b) {
      if (!dir) {
        if (cur < a)
          ans += ((cur - 1) + (m - 1) + (m - a));
        else
          ans += (cur - a);
      } else {
        ans += ((m - cur) + (m - a));
      }
      ans += (a - b);
    }
    printf("%d\n", ans);
  }
  return 0;
}
