#include <bits/stdc++.h>
struct passenger {
  int64_t t, s, f;
  size_t id;
};
bool operator<(const passenger& lhs, const passenger& rhs) {
  return lhs.t < rhs.t;
}
std::istream& operator>>(std::istream& is, passenger& p) {
  return is >> p.t >> p.s >> p.f;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  size_t n, m;
  std::cin >> n >> m;
  std::vector<passenger> passengers(n);
  for (size_t i = 0; i < n; ++i) {
    std::cin >> passengers[i];
    passengers[i].id = i;
  }
  sort(passengers.begin(), passengers.end());
  passengers.push_back({1000000000000000000ll, 1, 1, n});
  int64_t floor = 1;
  int64_t last = 0;
  std::vector<int64_t> results(n);
  std::map<int64_t, std::vector<passenger>> wait_up;
  std::map<int64_t, std::vector<passenger>> wait_down;
  std::map<int64_t, std::vector<passenger>> go_up;
  std::map<int64_t, std::vector<passenger>> go_down;
  size_t p_up = 0;
  size_t p_down = 0;
  for (size_t i = 0; i <= n; ++i) {
    while (last != passengers[i].t && (p_up != 0 || p_down != 0)) {
      if (p_up >= p_down) {
        const int64_t nearest = [&]() {
          if (wait_up.empty()) {
            return go_up.begin()->first;
          }
          if (go_up.empty()) {
            return wait_up.begin()->first;
          }
          return std::min(wait_up.begin()->first, go_up.begin()->first);
        }();
        const int64_t need = nearest - floor;
        const int64_t take = std::min(need, passengers[i].t - last);
        floor += take;
        last += take;
        if (auto it = wait_up.find(floor); it != wait_up.end()) {
          for (const auto& waiting : it->second) {
            if (waiting.f < floor) {
              go_down[waiting.f].push_back(waiting);
              p_down += 1;
            } else {
              go_up[waiting.f].push_back(waiting);
              p_up += 1;
            }
          }
          p_up -= it->second.size();
          wait_up.erase(it);
        }
        if (auto it = go_up.find(floor); it != go_up.end()) {
          for (const auto& going : it->second) {
            results[going.id] = last;
          }
          p_up -= it->second.size();
          go_up.erase(it);
        }
      } else {
        const int64_t nearest = [&]() {
          if (wait_down.empty()) {
            return go_down.rbegin()->first;
          }
          if (go_down.empty()) {
            return wait_down.rbegin()->first;
          }
          return std::max(wait_down.rbegin()->first, go_down.rbegin()->first);
        }();
        const int64_t need = floor - nearest;
        const int64_t take = std::min(need, passengers[i].t - last);
        floor -= take;
        last += take;
        if (auto it = wait_down.find(floor); it != wait_down.end()) {
          for (const auto& waiting : it->second) {
            if (waiting.f < floor) {
              go_down[waiting.f].push_back(waiting);
              p_down += 1;
            } else {
              go_up[waiting.f].push_back(waiting);
              p_up += 1;
            }
          }
          p_down -= it->second.size();
          wait_down.erase(it);
        }
        if (auto it = go_down.find(floor); it != go_down.end()) {
          for (const auto& going : it->second) {
            results[going.id] = last;
          }
          p_down -= it->second.size();
          go_down.erase(it);
        }
      }
    }
    if (passengers[i].s == floor) {
      if (passengers[i].f > floor) {
        go_up[passengers[i].f].push_back(passengers[i]);
        p_up += 1;
      } else {
        go_down[passengers[i].f].push_back(passengers[i]);
        p_down += 1;
      }
    } else if (passengers[i].s > floor) {
      wait_up[passengers[i].s].push_back(passengers[i]);
      p_up += 1;
    } else {
      wait_down[passengers[i].s].push_back(passengers[i]);
      p_down += 1;
    }
    last = passengers[i].t;
  }
  for (int64_t x : results) {
    std::cout << x << '\n';
  }
}
