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