#include <bits/stdc++.h>
using namespace std;
const long long int inf = 5 * (1e+8);
const long long int mod = 1e+9 + 7;
vector<long long int> adj[500005];
vector<long long int> visited(500005, 0);
vector<long long int> t(500005, 0);
long long int flag = 1;
void nothing() {}
void solve() {
  long long int n, m;
  cin >> n >> m;
  while (n--) {
    long long int s, f, t;
    cin >> s >> f >> t;
    long long int ans;
    long long int k = 0;
    if (s == f)
      ans = t;
    else if (s < f) {
      k = t / (2 * m - 2) - 2;
      while (k * (2 * m - 2) + s - 1 < t) k++;
      ans = k * (2 * m - 2) + f - 1;
    } else {
      k = t / (2 * m - 2) - 2;
      while (k * (2 * m - 2) + (2 * m - s) - 1 < t) k++;
      ans = k * (2 * m - 2) + (2 * m - f) - 1;
    }
    cout << ans << '\n';
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  solve();
  return 0;
}
