#include<bits/stdc++.h>
using namespace std;
int n,k,val,r,c,l[10010],h[10010],ans=0;//l记录该列移动次数,h记录该行移动次数
int main()
{scanf("%d%d",&n,&k);
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<n*(i-1)+j<<" ";
}
cout<<endl;
}*/
for(int i=1;i<=k;i++)
{
ans=0;
scanf("%d%d%d",&val,&r,&c);
int x,y,nowx,nowy;
x=(val-1)/n+1;//原来的x坐标
y=(val-1)%n+1;//原来的y坐标
nowy=(y+h[x]-1)%n+1//随该行变动后的坐标
nowx=(x+l[nowy]-1)%n+1;//随该列变动后的坐标
//cout<<nowx<<" "<<nowy<<endl;
if(nowy>c)h[nowx]=(h[nowx]+n-nowy+c)%n,ans+=n-nowy+c;//要转到头
else h[nowx]=(h[nowx]+c-nowy)%n,ans+=c-nowy;//直接转
if(nowx>r)l[c]=(l[c]+n-nowx+r)%n,ans+=n-nowx+r;
else l[c]=(l[c]+r-nowx)%n,ans+=r-nowx;
//cout<<h[nowx]<<" "<<l[c]<<endl;
printf("%d\n",ans);
}
}