#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);
    if (s == f)
      printf("%d\n", t);
    else {
      s--;
      f--;
      int p = 2 * (m - 1);
      int delay = 0;
      int dr;
      int ep = t % p;
      int current;
      if (ep >= 0 && ep < m - 1) {
        dr = 1;
        current = ep;
      } else {
        dr = -1;
        current = p - ep;
      }
      if (dr == 1) {
        if (s >= current) {
          delay += s - current;
        } else {
          delay += m - 1 - s + (p - current) - (m - 1);
          dr = -1;
        }
      } else if (dr == -1) {
        if (s <= current) {
          delay += current - s;
        } else {
          delay += current + s;
          dr = 1;
        }
      }
      current = s;
      if (dr == 1) {
        if (f >= current) {
          delay += f - current;
        } else {
          delay += m - 1 - f + (p - current) - (m - 1);
          dr = -1;
        }
      } else if (dr == -1) {
        if (f <= current) {
          delay += current - f;
        } else {
          delay += current + f;
          dr = 1;
        }
      }
      printf("%d\n", t + delay);
    }
  }
}
