#include <bits/stdc++.h>
using namespace std;
int n, m;
int fd(int tt, int k) {
  int t = tt;
  t %= m + m - 2;
  if (t < m) {
    if (k >= t) {
      return tt + k - t;
    } else {
      return tt + m * 2 - t - k - 2;
    }
  } else {
    t -= m;
    t = m - t - 2;
    if (k <= t) {
      return tt + t - k;
    } else
      return tt + t + k;
  }
}
int main() {
  while (cin >> n >> m) {
    int s, f, t;
    while (n--) {
      cin >> s >> f >> t;
      if (s == f) {
        cout << t << endl;
        continue;
      }
      s--, f--;
      int t1, t2;
      t1 = fd(t, s);
      t2 = fd(t1, f);
      cout << t2 << endl;
    }
  }
}
