#include <bits/stdc++.h>
int solve(int s, int f, int t, int m) {
  if (s == f) {
    return t;
  }
  int begin;
  int ind = t % (2 * (m - 1));
  bool ishead;
  if (ind <= m - 1) {
    begin = ind + 1;
    ishead = true;
  } else {
    begin = m - (ind - (m - 1));
    ishead = false;
  }
  if (ishead) {
    if (s >= begin) {
      if (f > s) {
        return t + f - begin;
      } else {
        return t + (m - begin) + (m - f);
      }
    } else {
      if (f > s) {
        return t + (m - begin) + (m - 2) + f;
      } else {
        return t + (m - begin) + (m - f);
      }
    }
  } else {
    if (s > begin) {
      if (f > s) {
        return t + (begin - 1) + (f - 1);
      } else {
        return t + (begin - 1) + (m - 1) + (m - f);
      }
    } else {
      if (f > s) {
        return t + (begin - 1) + (f - 1);
      } else {
        return t + begin - f;
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int s, f, t;
  int ans;
  while (n--) {
    scanf("%d%d%d", &s, &f, &t);
    ans = solve(s, f, t, m);
    printf("%d\n", ans);
  }
  return 0;
}
