#include <bits/stdc++.h>
using namespace std;
long long const M = 5e5 + 10, M2 = 1e7 + 10, mod = 1e9 + 7, inf = 1e18 + 10;
long long n, m, a[M], siz;
long long fans(long long x, long long t) {
  long long res = t / siz;
  long long hlp = siz * res;
  long long rim = t % siz;
  if (x - 1 >= rim) {
    return x - 1 + hlp;
  } else if (n - 1 + n - x >= rim)
    return hlp + (n - 1 + n - x);
  else
    return fans(x + siz, t);
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n;
  siz = 2 * n - 2;
  while (m--) {
    long long s, f, t;
    cin >> s >> f >> t;
    if (s == f) {
      cout << t << endl;
      continue;
    }
    t = fans(s, t);
    t = fans(f, t);
    cout << t << endl;
  }
}
