// Arup Guha
// 2/10/2013
// Solution to 2010 MCPC Regional Problem D: Queen Collisions

import java.util.*;

public class d {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Go till end of data.
		while (n != 0) {

			int g = stdin.nextInt();

			// Store the number of queens in each row, column, and both diagonals.
			int[] rows = new int[n+1];
			int[] cols = new int[n+1];
			int[] fDiag = new int[2*n+1];
			int[] bDiag = new int[2*n+1];

			// Loop through patterns.
			for (int i=0; i<g; i++) {

				int numQs = stdin.nextInt();
				int sx = stdin.nextInt();
				int sy = stdin.nextInt();
				int dx = stdin.nextInt();
				int dy = stdin.nextInt();

				// Place a queen in its row, column, and both diagonals.
				for (int j=0; j<numQs; j++) {
					int curx = sx + j*dx;
					int cury = sy + j*dy;
					rows[curx]++;
					cols[cury]++;
					fDiag[curx-cury+(n-1)]++;
					bDiag[curx+cury]++;
				}
			}

			int collisions = 0;

			// Add up all row, col collisions - we only count adjacent collisions.
			for (int j=0; j<rows.length; j++) {
				if (rows[j] > 1)
					collisions += (rows[j] - 1);
				if (cols[j] > 1)
					collisions += (cols[j] - 1);
			}

			// Add up both diagonal collisions - we only count adjacent collisions.
			for (int j=0; j<fDiag.length; j++) {
				if (fDiag[j] > 1)
					collisions += (fDiag[j] - 1);
				if (bDiag[j] > 1)
					collisions += (bDiag[j] - 1);
			}

			// Output result.
			System.out.println(collisions);

			// Go to next case.
			n = stdin.nextInt();

		}
	}
}