#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d %d\n", &n, &m);
  int s, f, t, tmp;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &s, &f, &t);
    if (f == s) {
      printf("%d\n", t);
      continue;
    }
    tmp = t / (m - 1);
    if (tmp & 1) {
      if (t - tmp * (m - 1) > m - s) ++tmp;
    } else if (t - tmp * (m - 1) >= s)
      ++tmp;
    if (tmp & 1) {
      if (f > s) {
        ++tmp;
        printf("%d\n", tmp * (m - 1) + f - 1);
        continue;
      } else {
        printf("%d\n", tmp * (m - 1) + m - f);
        continue;
      }
    } else {
      if (f < s) {
        ++tmp;
        printf("%d\n", tmp * (m - 1) + m - f);
        continue;
      } else {
        printf("%d\n", tmp * (m - 1) + f - 1);
        continue;
      }
    }
  }
  return 0;
}
