#include <bits/stdc++.h>
#define int long long// 保险起见开 long long
#define endl '\n'
using namespace std;
static const int maxn = 66666;
int n, k; // n and k 分别表示表格大小和移动次数
int x[maxn], r[maxn], c[maxn]; // 三个整数 x, r, z
struct tabulation{
int x, y;// x是行 y是列 表示当前位置数字的坐标
tabulation(){};// 初始化
tabulation(int data,int next) : x(data),y(next){};
}a[maxn]; // a数组表示每个数字的位置
signed main() {
// 输入输出优化
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n >> k; // 输入
for(int i = 1; i <= k; i ++) {
cin >> x[i] >> r[i] >> c[i];
a[i].x = (x[i] - 1) / n + 1;// 向上取整
a[i].y = (x[i] % n == 0) ? n : x[i] % n;
/* 如果 x[i] 是 n 的倍数 那么 x[i] 就在第n列
反之则在第 x[i] % n 列 */
}
for(int i = 1; i <= k; i++) {
int line = a[i].x, colu = a[i].y;// 行和列
// 处理列
colu = (c[i] >= colu) ? c[i] - colu : n - (colu - c[i]);
// 计算最少移动次数
for(int j = 1; j <= k; j ++) {
// 更新位置
if(a[i].x == a[j].x) {
a[j].y += colu;
if(a[j].y > n) a[j].y -= n;
}
}
// 处理行
line = (r[i] >= line) ? r[i] - line : n - (line - r[i]);
// 计算最少移动次数
for(int j = 1; j <= n; j ++) {
// 更新位置
if(a[i].y == a[j].y) {
a[j].x += line;
if(a[j].x > n) a[j].x -= n;
}
}
// 最终answer=移动行的次数加上移动列的次数
cout << colu + line << endl;
}
return 0;
}