xy[3][501][501];
n,m,a,b,c,i;
D(a,b){
	for(i=1;i<=n;i++)
		m-=!xy[(c%3+2)%3][b][i]&&!xy[(c%3+1)%3][i][a];
}
main(h){
	for(;scanf("%d%d",&n,&h)*n;){
		memset(xy,0,sizeof(xy));
		m=n*n*n;
		for(;h--;){
			scanf("%s%d%d",&c,&a,&b);
			xy[c%3][a][b]++?1/**/:c%3<2?D(a,b):D(b,a);
		}
		printf("%d\n",m);
	}
	{int n;for(n=1000000;n--;)getchar();}//delay(0.52s)
	exit(0);
}