#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i;
  cin >> n >> m;
  int s[n], f[n], t[n];
  int k, r;
  for (i = 0; i < n; i++) {
    cin >> s[i] >> f[i] >> t[i];
    k = (t[i] - 1) / (m - 1);
    if (t[i] == 0) k = 0;
    if (f[i] == s[i]) {
      cout << t[i] << endl;
      continue;
    }
    if (k % 2 != 0) {
      if (f[i] > s[i]) {
        t[i] = (k + 1) * (m - 1) + f[i] - 1;
      } else {
        if (t[i] > (k * (m - 1) + (m - s[i])))
          t[i] = (k + 2) * (m - 1) + (m - f[i]);
        else
          t[i] = (k) * (m - 1) + (m - f[i]);
      }
    } else {
      if (f[i] > s[i]) {
        if (t[i] > k * (m - 1) + s[i] - 1)
          t[i] = (k + 2) * (m - 1) + f[i] - 1;
        else
          t[i] = k * (m - 1) + f[i] - 1;
      } else {
        t[i] = (k + 1) * (m - 1) + m - f[i];
      }
    }
    cout << t[i] << endl;
  }
}
