#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    int period = 2 * m - 2;
    for (int(i) = 0; (i) < (n); (i)++) {
      int s, f, t;
      cin >> s >> f >> t;
      s--;
      f--;
      int res = 0;
      if (s == f)
        res = t;
      else {
        int p = t / period;
        int offset = t % period;
        if (f > s) {
          bool before = false;
          if (offset <= s) before = true;
          if (before)
            res = p * period + f;
          else
            res = (p + 1) * period + f;
        } else if (s > f) {
          bool before = false;
          if (offset < m || (offset >= m && (period - offset) >= s))
            before = true;
          if (before)
            res = p * period + period - f;
          else
            res = (p + 1) * period + period - f;
        }
      }
      cout << res << endl;
    }
  }
}
