[TASK]:
You will be given lines of pseudocode, your task is to write the corresponding C++ code. The pseudocode will provide detailed description of the c++ code line by line. The pseudocode is garanteed to be correct and complete.

[INSTRUCTION]:
The following libraries are already included in the code.
```cpp
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <fstream>
#include <climits>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <bitset>
using namespace std;
```
Do not include them in your code again. Please surround your code with ```cpp and ``` markers. Note that the code should correspond to the pseudocode line by line.

[PSEUDOCODE]:
define the main function
    n, m = integers
    read n and m
    i, x, y, t, up, down, ans, u_d, d_u, count, sum = integers with up set to 0, down set to 0, count set to 0, sum set to 0
    for 1 to n inclusive
        read x, y and t
        set up to 0
        set down to 0
        set count to 0
        set sum to 0
        set u_d to 2 * (m - x)
        set d_u to 2 * (x - 1)
        if x is equal to y
            print t
        else
            if t is less than or equal to x - 1
                if y is greater than x
                    print x - 1 + y - x
                else
                    print x - 1 + x - y + u_d
                end of the block
            else
                decrement t by x - 1
                set up to 1
                increment sum by x - 1
                while t is greater than 0
                    if count % 2 is 0
                        decrement t by u_d
                        set down to 1
                        set up to 1
                        increment sum by u_d
                    else
                        decrement t by d_u
                        set down to 0
                        set up to 1
                        increment sum by d_u
                    end of the block
                    increment count
                end of the block
                if y is greater than x
                    if up is equal to 1
                        print sum + y - x
                    else
                        print sum + d_u + y - x
                    end of the block
                else
                    if up is equal to 1
                        print sum + x - y + u_d
                    else
                        print sum + x - y
                    end of the block
                end of the block
            end of the block
        end of the block
    end of the block
end of the block

[CODE]:

int main() {
    int n, m;
    cin >> n >> m;
    int i, x, y, t, up = 0, down = 0, ans, u_d, d_u, count = 0, sum = 0;
    for (i = 1; i <= n; i++) {
        cin >> x >> y >> t;
        up = 0;
        down = 0;
        count = 0;
        sum = 0;
        u_d = 2 * (m - x);
        d_u = 2 * (x - 1);
        if (x == y)
            cout << t << endl;
        else {
            if (t <= x - 1) {
                if (y > x) {
                    cout << x - 1 + y - x << endl;
                } else {
                    cout << x - 1 + x - y + u_d << endl;
                }
            } else {
                t -= (x - 1);
                up = 1;
                sum += (x - 1);
                while (t > 0) {
                    if (count % 2 == 0) {
                        t -= u_d;
                        down = 1;
                        up = 0;
                        sum += u_d;
                    } else {
                        t -= d_u;
                        down = 0;
                        up = 1;
                        sum += d_u;
                    }
                    count++;
                }
                if (y > x) {
                    if (up == 1) {
                        cout << sum + y - x << endl;
                    } else {
                        cout << sum + d_u + y - x << endl;
                    }
                } else {
                    if (up == 1) {
                        cout << sum + x - y + u_d << endl;
                    } else {
                        cout << sum + x - y << endl;
                    }
                }
            }
        }
    }
}