#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const int Size = 1000 * 1000 + 1;
char buffer[Size];
int solution(int nTest) {
  int m, n;
  scanf("%d%d", &n, &m);
  m--;
  int p = 2 * m;
  for (int i = 0; i < n; i++) {
    int s, f, t;
    scanf("%d%d%d", &s, &f, &t);
    f--;
    s--;
    if (s == f) {
      printf("%d\n", t);
      continue;
    }
    int l = t % p;
    int g = t / p;
    if (s < f) {
      if (l <= s) {
        printf("%d\n", g * p + f);
      } else {
        printf("%d\n", g * p + f + p);
      }
    } else {
      if (l <= p - s) {
        printf("%d\n", g * p + p - f);
      } else {
        printf("%d\n", g * p + 2 * p - f);
      }
    }
  }
  return 0;
}
int main() {
  if (0) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  int i = 0, n = 99999;
  while (i < n && solution(i)) {
    i++;
  }
  return 0;
}
