#include <bits/stdc++.h>
using namespace std;
const int INF = (int)2E9;
const long long LINF = (long long)9E18;
const long double PI = acos(-1.0);
const long double EPS = 1E-11;
int main() {
  bool ok;
  long long n, m, s, t, f, res;
  cin >> n >> m;
  for (int i = 0; i < (n); ++i) {
    cin >> s >> f >> t;
    if (s == f)
      cout << t << endl;
    else {
      res = 0;
      ok = true;
      if (t > (s - 1))
        while ((t - res) > (s - 1)) {
          res += (m - 1);
          if ((t - res) <= (m - s)) {
            if (s > f)
              res += m - f;
            else
              res += (m - 1) + (f - 1);
            ok = false;
          } else
            res += (m - 1);
        }
      if (ok)
        if (s < f)
          res += f - 1;
        else
          res += (s - 1) + (m - s) + (m - f);
      cout << res << endl;
    }
  }
}
