#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
int s;
int f;
int t;
int main() {
  int m, n;
  scanf("%d%d", &n, &m);
  int conta = 2 * (m - 1);
  for (int i = 0; i < (int)(n); i++) {
    scanf("%d%d%d", &s, &f, &t);
    int minus = s - 1;
    int newt = t - minus;
    int divi = newt / conta;
    int dsub = 0;
    dsub = divi * conta + minus;
    int baj = 2 * minus;
    int sub = conta - baj;
    int calcSub = ((f >= s) ? f - s : sub + s - f);
    int calcBaj = ((f >= s) ? baj + f - s : s - f);
    if (s == f)
      printf("%d\n", t);
    else if (dsub >= t)
      printf("%d\n", dsub + calcSub);
    else {
      dsub += sub;
      if (dsub >= t) {
        printf("%d\n", dsub + calcBaj);
      } else {
        dsub += baj;
        printf("%d\n", dsub + calcSub);
      }
    }
  }
  return 0;
}
