Problem: The m-floor (m > 1) office of international corporation CodeForces has the advanced elevator control system established. It works as follows.

All office floors are sequentially numbered with integers from 1 to m. At time t = 0, the elevator is on the first floor, the elevator is empty and nobody is waiting for the elevator on other floors. Next, at times ti (ti > 0) people come to the elevator. For simplicity, we assume that one person uses the elevator only once during the reported interval. For every person we know three parameters: the time at which the person comes to the elevator, the floor on which the person is initially, and the floor to which he wants to go.

The movement of the elevator between the floors is as follows. At time t (t ≥ 0, t is an integer) the elevator is always at some floor. First the elevator releases all people who are in the elevator and want to get to the current floor. Then it lets in all the people waiting for the elevator on this floor. If a person comes to the elevator exactly at time t, then he has enough time to get into it. We can assume that all of these actions (going in or out from the elevator) are made instantly. After that the elevator decides, which way to move and at time (t + 1) the elevator gets to the selected floor.

The elevator selects the direction of moving by the following algorithm. 

  * If the elevator is empty and at the current time no one is waiting for the elevator on any floor, then the elevator remains at the current floor. 
  * Otherwise, let's assume that the elevator is on the floor number x (1 ≤ x ≤ m). Then elevator calculates the directions' "priorities" pup and pdown: pup is the sum of the number of people waiting for the elevator on the floors with numbers greater than x, and the number of people in the elevator, who want to get to the floors with the numbers greater than x; pdown is the sum of the number of people waiting for the elevator on the floors with numbers less than x, and the number of people in the elevator, who want to get to the floors with the numbers less than x. If pup ≥ pdown, then the elevator goes one floor above the current one (that is, from floor x to floor x + 1), otherwise the elevator goes one floor below the current one (that is, from floor x to floor x - 1). 



Your task is to simulate the work of the elevator and for each person to tell the time when the elevator will get to the floor this person needs. Please note that the elevator is large enough to accommodate all the people at once.

Input

The first line contains two space-separated integers: n, m (1 ≤ n ≤ 105, 2 ≤ m ≤ 105) — the number of people and floors in the building, correspondingly.

Next n lines each contain three space-separated integers: ti, si, fi (1 ≤ ti ≤ 109, 1 ≤ si, fi ≤ m, si ≠ fi) — the time when the i-th person begins waiting for the elevator, the floor number, where the i-th person was initially located, and the number of the floor, where he wants to go.

Output

Print n lines. In the i-th line print a single number — the moment of time, when the i-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input. 

Please don't use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

3 10
1 2 7
3 6 5
3 4 8


Output

7
11
8


Input

2 10
1 2 5
7 4 5


Output

5
9

Note

In the first sample the elevator worked as follows: 

  * t = 1. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. pup = 1 + 0 = 1, pdown = 0 + 0 = 0, pup ≥ pdown. So the elevator goes to the floor number 2. 
  * t = 2. The elevator is on the floor number 2. One person enters the elevator, he wants to go to the floor number 7. pup = 0 + 1 = 1, pdown = 0 + 0 = 0, pup ≥ pdown. So the elevator goes to the floor number 3. 
  * t = 3. The elevator is on the floor number 3. There is one person in the elevator, he wants to go to floor 7. The floors number 4 and 6 have two people waiting for the elevator. pup = 2 + 1 = 3, pdown = 0 + 0 = 0, pup ≥ pdown. So the elevator goes to the floor number 4. 
  * t = 4. The elevator is on the floor number 4. There is one person in the elevator who wants to go to the floor number 7. One person goes into the elevator, he wants to get to the floor number 8. The floor number 6 has one man waiting. pup = 1 + 2 = 3, pdown = 0 + 0 = 0, pup ≥ pdown. So the elevator goes to the floor number 5. 
  * t = 5. The elevator is on the floor number 5. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. There is one person waiting for the elevator on the floor number 6. pup = 1 + 2 = 3, pdown = 0 + 0 = 0, pup ≥ pdown. So the elevator goes to the floor number 6. 
  * t = 6. The elevator is on the floor number 6. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. One man enters the elevator, he wants to get to the floor number 5. pup = 0 + 2 = 2, pdown = 0 + 1 = 1, pup ≥ pdown. So the elevator goes to the floor number 7. 
  * t = 7. The elevator is on the floor number 7. One person leaves the elevator, this person wanted to get to the floor number 7. There are two people in the elevator, they want to get to the floors with numbers 8 and 5, correspondingly. pup = 0 + 1 = 1, pdown = 0 + 1 = 1, pup ≥ pdown. So the elevator goes to the floor number 8. 
  * t = 8. The elevator is on the floor number 8. One person leaves the elevator, this person wanted to go to the floor number 8. There is one person in the elevator, he wants to go to the floor number 5. pup = 0 + 0 = 0, pdown = 0 + 1 = 1, pup < pdown. So the elevator goes to the floor number 7. 
  * t = 9. The elevator is on the floor number 7. There is one person in the elevator, this person wants to get to the floor number 5. pup = 0 + 0 = 0, pdown = 0 + 1 = 1, pup < pdown. So the elevator goes to the floor number 6. 
  * t = 10. The elevator is on the floor number 6. There is one person in the elevator, he wants to get to the floor number 5. pup = 0 + 0 = 0, pdown = 0 + 1 = 1, pup < pdown. So the elevator goes to the floor number 5. 
  * t = 11. The elevator is on the floor number 5. One person leaves the elevator, this person initially wanted to get to the floor number 5. The elevator is empty and nobody needs it, so the elevator remains at the floor number 5. 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct person {
 public:
  int t, s, f, id;
  person(){};
  person(int t, int s, int f, int id) : t(t), s(s), f(f), id(id){};
};
const long long INF = 2 * (long long)1e17;
vector<int> exit_floor, input_floor;
int nn = 0;
void init(vector<int>& t, int sz) {
  nn = sz + 5;
  t.assign(nn, 0);
}
int sum(vector<int>& t, int r) {
  int res = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) res += t[r];
  return res;
}
void add(vector<int>& t, int i, int delta) {
  for (; i < nn; i = (i | (i + 1))) t[i] += delta;
}
int sum(vector<int>& t, int l, int r) { return sum(t, r) - sum(t, l - 1); }
struct cmp_by_floor {
  bool operator()(const person& a, const person& b) { return a.f < b.f; }
};
bool cmp_by_time(const person& a, const person& b) { return a.t < b.t; }
int main() {
  int n, m;
  cin >> n >> m;
  init(exit_floor, m);
  init(input_floor, m);
  vector<person> all;
  for (int i = 0; i < n; i++) {
    int ti, si, fi;
    cin >> ti >> si >> fi;
    all.push_back(person(ti, si, fi, i));
  }
  vector<long long> ans(n, -1);
  vector<vector<person> > in(m + 1);
  vector<vector<person> > waits(m + 1);
  long long curt = 1, x = 1;
  sort(all.begin(), all.end(), cmp_by_time);
  set<int> floors;
  set<int>::iterator it;
  int pos = 0;
  while (true) {
    while (pos < n && curt >= all[pos].t) {
      person it = all[pos];
      waits[it.s].push_back(all[pos]);
      floors.insert(all[pos].s);
      add(input_floor, all[pos].s, 1);
      pos++;
    }
    while (waits[x].size() > 0) {
      person it = waits[x].back();
      waits[x].pop_back();
      in[it.f].push_back(it);
      add(exit_floor, it.f, 1);
      floors.insert(it.f);
      add(input_floor, x, -1);
    }
    while (in[x].size() > 0) {
      person it = in[x].back();
      in[x].pop_back();
      ans[it.id] = curt;
      add(exit_floor, x, -1);
    }
    int p_up = 0, p_down = 0;
    p_up = sum(exit_floor, x + 1, m) + sum(input_floor, x + 1, m);
    p_down = sum(exit_floor, 1, x - 1) + sum(input_floor, 1, x - 1);
    if (p_up == 0 && p_down == 0) {
      if (pos >= n) break;
      curt = all[pos].t;
      continue;
    }
    long long nextt = INF, nextx = -1;
    if (pos < n) nextt = all[pos].t;
    floors.insert(x);
    if (p_up >= p_down) {
      nextx = INF;
      it = floors.find(x);
      it++;
      if (it != floors.end()) {
        nextx = *it;
      }
      if (nextt == INF && nextx == INF) break;
      if (nextt - curt < nextx - x) {
        nextx = x + nextt - curt;
      } else {
        nextt = curt + nextx - x;
      }
    } else {
      nextx = -INF;
      it = floors.find(x);
      if (it != floors.begin()) {
        it--;
        nextx = *it;
      }
      if (nextt == INF && nextx == -INF) break;
      if (nextt - curt < x - nextx) {
        nextx = x - (nextt - curt);
      } else {
        nextt = curt + x - nextx;
      }
    }
    floors.erase(x);
    curt = nextt;
    x = nextx;
  }
  for (int i = 0; i < n; i++) cout << ans[i] << endl;
  return 0;
}