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