#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    long long s, f, t;
    cin >> s >> f >> t;
    if (s == f) {
      cout << t << endl;
    } else if (s < f) {
      long long t1 = s - 1;
      while (t1 < t) {
        t1 += (m - s) + (m - 1) + (s - 1);
      }
      t1 += (f - s);
      cout << t1 << endl;
    } else if (s > f) {
      long long t1 = (m - 1) + (m - s);
      while (t1 < t) {
        t1 += (m - 1) + (m - s) + (s - 1);
      }
      t1 += (s - f);
      cout << t1 << endl;
    }
  }
}
