#include <bits/stdc++.h>
using namespace std;
const int M = 210000;
struct Node {
  long long si, fi, ti;
} node[M];
int n, m;
long long get_num(long long x, long long y, long long z, int dir) {
  long long sum = 0;
  if (dir == 1) {
    if (x <= y) {
      sum += y - x;
      if (y <= z) {
        sum += z - y;
      } else {
        sum += (m - 1 - y) * 2 + y - z;
      }
    } else {
      sum += (m - 1 - x) * 2 + x - y;
      if (y >= z)
        sum += y - z;
      else
        sum += 2 * y + z - y;
    }
  } else {
    if (x >= y) {
      sum += x - y;
      if (y >= z)
        sum += y - z;
      else
        sum += 2 * y + z - y;
    } else {
      sum += 2 * x + y - x;
      if (y <= z)
        sum += z - y;
      else
        sum += (m - 1 - y) * 2 + y - z;
    }
  }
  return sum;
}
int main() {
  while (scanf("%d%d", &n, &m) == 2) {
    for (int i = 1; i <= n; i++) {
      scanf("%lld%lld%lld", &node[i].si, &node[i].fi, &node[i].ti);
      node[i].si--;
      node[i].fi--;
    }
    for (int i = 1; i <= n; i++) {
      if (node[i].si == node[i].fi) {
        printf("%lld\n", node[i].ti);
        continue;
      }
      if ((node[i].ti / (m - 1)) % 2 == 0) {
        long long cur = node[i].ti % (m - 1);
        printf("%lld\n", get_num(cur, node[i].si, node[i].fi, 1) + node[i].ti);
      } else {
        long long cur = m - 1 - node[i].ti % (m - 1);
        printf("%lld\n", get_num(cur, node[i].si, node[i].fi, -1) + node[i].ti);
      }
    }
  }
  return 0;
}
