#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;
int calc(int s, int f, int t, int m) {
  if (s == f) return 0;
  s--;
  f--;
  t %= (2 * (m - 1));
  int tos;
  int stof;
  bool up = (t < m - 1);
  int pos = (t < m - 1) ? t : (2 * m - 2) - t;
  if (up) {
    if (s >= pos) {
      tos = s - pos;
    } else {
      tos = (2 * m - 2) - pos - s;
      up = false;
    }
  } else {
    if (s <= pos) {
      tos = pos - s;
    } else {
      tos = pos + s;
      up = true;
    }
  }
  pos = s;
  if (up) {
    if (f >= pos) {
      stof = f - pos;
    } else {
      stof = (2 * m - 2) - pos - f;
    }
  } else {
    if (f <= pos) {
      stof = pos - f;
    } else {
      stof = pos + f;
    }
  }
  return tos + stof;
}
int main() {
  int n, m;
  int s, f, t;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s >> f >> t;
    cout << calc(s, f, t, m) + t << endl;
  }
  return 0;
}
