#include <bits/stdc++.h>
int main() {
  int c, tt, n, x, y, z, m;
  while (scanf("%d%d", &n, &m) == 2) {
    while (n--) {
      scanf("%d%d%d", &x, &y, &z);
      if (x == y) {
        printf("%d\n", z);
        continue;
      }
      int k = 2 * m - 2;
      int a = z / k;
      int b = z % k;
      if (b >= m - 1) {
        c = 2 * m - b - 1;
        if (x <= c) {
          tt = a * k + b + c - x;
          c = x;
          if (c >= y)
            tt += (x - y);
          else
            tt += (x + y - 2);
        } else {
          tt = a * k + b + x + c - 2;
          c = x;
          if (c <= y)
            tt += (y - c);
          else
            tt += (2 * (m - c) + c - y);
        }
      } else {
        c = b + 1;
        if (x >= c) {
          tt = a * k + b + x - c;
          c = x;
          if (c <= y)
            tt += (y - x);
          else
            tt += (2 * (m - c) + c - y);
        } else {
          tt = a * k + b + 2 * (m - c) + c - x;
          c = x;
          if (y <= c)
            tt += (c - y);
          else
            tt += (2 * (c - 1) + y - c);
        }
      }
      printf("%d\n", tt);
    }
  }
  return 0;
}
