And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All *n* participants who have made it to the finals found themselves in a huge *m*-floored 10<sup class="upper-index">8</sup>-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?".

The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then — to the 3-rd floor and so on until it reaches the *m*-th floor. After that the elevator moves to floor *m* - 1, then to floor *m* - 2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.

For each of the *n* participant you are given *s*<sub class="lower-index">*i*</sub>, which represents the floor where the *i*-th participant starts, *f*<sub class="lower-index">*i*</sub>, which represents the floor the *i*-th participant wants to reach, and *t*<sub class="lower-index">*i*</sub>, which represents the time when the *i*-th participant starts on the floor *s*<sub class="lower-index">*i*</sub>.

For each participant print the minimum time of his/her arrival to the floor *f*<sub class="lower-index">*i*</sub>. 

If the elevator stops on the floor *s*<sub class="lower-index">*i*</sub> at the time *t*<sub class="lower-index">*i*</sub>, then the *i*-th participant can enter the elevator immediately. If the participant starts on the floor *s*<sub class="lower-index">*i*</sub> and that's the floor he wanted to reach initially (*s*<sub class="lower-index">*i*</sub> = *f*<sub class="lower-index">*i*</sub>), then the time of arrival to the floor *f*<sub class="lower-index">*i*</sub> for this participant is considered equal to *t*<sub class="lower-index">*i*</sub>.
The first line contains two space-separated integers *n* and *m* (1 ≤ *n* ≤ 10<sup class="upper-index">5</sup>, 2 ≤ *m* ≤ 10<sup class="upper-index">8</sup>). 

Next *n* lines contain information about the participants in the form of three space-separated integers *s*<sub class="lower-index">*i*</sub> *f*<sub class="lower-index">*i*</sub> *t*<sub class="lower-index">*i*</sub> (1 ≤ *s*<sub class="lower-index">*i*</sub>, *f*<sub class="lower-index">*i*</sub> ≤ *m*, 0 ≤ *t*<sub class="lower-index">*i*</sub> ≤ 10<sup class="upper-index">8</sup>), described in the problem statement.
7 4
2 4 3
1 2 0
2 2 0
1 2 1
4 3 5
1 2 2
4 2 0
9
1
0
7
10
7
5
5 5
1 5 4
1 3 1
1 3 4
3 1 5
4 2 5
12
10
10
8
7
5 5
1 3 4
4 4 2
3 2 1
2 4 0
1 5 3
10
2
7
3
12
8 5
2 2 91
5 1 97
5 1 36
5 4 19
2 5 50
4 2 1
1 4 9
3 2 32
91
104
40
21
60
7
19
39
7 5
2 2 85
4 3 76
4 1 17
5 4 70
2 5 3
3 3 14
3 1 51
85
78
24
77
12
14
56
5 2
1 2 0
2 1 1
2 2 0
2 2 5
2 1 4
1
2
0
5
6
1 2
1 1 3
3
2 2
2 2 3
2 1 5
3
6
3 4
2 4 7
3 3 1
2 2 9
9
1
9
2 100000000
2 1 3
99999999 100000000 100000000
199999998
299999997
n, m = map(int, input().split())
k = 2 * (m - 1)
ans = []
for _ in range(n):
    s, f, t = map(int, input().split())
    d = t % k
    if s < f:
        ad = (k if s <= d else 0) + f - 1 + t - d
    elif f < s:
        ad = (k if d + s > k + 1 else 0) + k + 1 - f + t - d
    else:
        ad = t
    ans.append(ad)
print("\n".join(map(str, ans)))