#include <bits/stdc++.h>
static int where_is_the_lift(int t, int m) {
  int s = t % (2 * m - 2);
  if (s < m) return s + 1;
  return 2 * m - 1 - s;
}
static int when_is_the_lift_at_floor_x(int t, int l, int m) {
  int s = t % (2 * m - 2);
  if (s < m) {
    if (s + 1 <= l) return t + l - (s + 1);
    return t + (m - s) + (m - l) - 1;
  }
  if (l <= 2 * m - 1 - s) return t + (2 * m - 1 - s - l);
  return t + (2 * m - 2 - s) + l - 1;
}
int main() {
  int n, m, i;
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; i++) {
    int s, f, t, to;
    scanf("%d %d %d", &s, &f, &t);
    if (s == f) {
      printf("%d\n", t);
      continue;
    }
    to = t;
    t = when_is_the_lift_at_floor_x(t, s, m);
    assert(t >= to);
    assert(where_is_the_lift(t, m) == s);
    to = t;
    t = when_is_the_lift_at_floor_x(t, f, m);
    assert(where_is_the_lift(t, m) == f);
    assert(t >= to);
    printf("%d\n", t);
  }
  return 0;
}
