#include <bits/stdc++.h>
using namespace std;
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);
    s--;
    f--;
    if (s == f) {
      printf("%d\n", t);
      continue;
    }
    int k = t / (m - 1), pos;
    bool dir;
    if (k % 2) {
      pos = m - 1 - t % (m - 1);
      dir = 0;
    } else {
      pos = t % (m - 1);
      dir = 1;
    }
    if (s < pos) {
      if (!dir)
        t += pos - s;
      else {
        t += m - 1 - pos + m - 1 - s;
        dir = 0;
      }
    } else if (s > pos) {
      if (!dir) {
        t += pos + s;
        dir = 1;
      } else
        t += s - pos;
    }
    if (!s) dir = 1;
    if (s == m - 1) dir = 0;
    if (s < f)
      if (dir)
        t += f - s;
      else
        t += s + f;
    else if (dir)
      t += m - 1 - s + m - 1 - f;
    else
      t += s - f;
    printf("%d\n", t);
  }
  return 0;
}
