H[4][501][501],n,m,a,b,c,i;main(h){for(;scanf("%d%d",&n,&h)*n;printf("%d\n",n*n*n-m))for(memset(H,m=0,4e6);h--;)for(scanf("%s%d%d",&c,&a,&b),c%=3,c>1?i=a,a=b,b=i:0,i=!H[c][a][b]++*n;i;i--)m+=!H[(c+2)%3][b][i]&!H[-~c%3][i][a];}