#include <bits/stdc++.h>
int main() {
  long int n, m, add, s, f, t, s1, s2, choice;
  scanf("%ld %ld", &n, &m);
  add = (m - 1) << 1;
  while (n--) {
    scanf("%ld%ld%ld", &s, &f, &t);
    if (f == s) {
      printf("%d\n", t);
      continue;
    }
    s1 = s - 1;
    s2 = 2 * m - 1 - s;
    while (1) {
      if (s1 >= t) {
        choice = 1;
        break;
      }
      s1 += add;
      if (s2 >= t) {
        choice = 2;
        break;
      }
      s2 += add;
    }
    if (s1 == s2) choice = 2;
    if (choice == 1)
      t = s1;
    else
      t = s2;
    if (choice == 1 && f > s || choice == 2 && f < s)
      printf("%ld\n", t + (f - s < 0 ? s - f : f - s));
    else if (choice == 2)
      printf("%ld\n", t + f + s - 2);
    else
      printf("%ld\n", t + 2 * m - s - f);
  }
  return 0;
}
