#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
fstream fin("collision.in");
int main() {
int n,g;
fin >> n;
while (n > 0) {
int tallies[4][2*n+1];
for (int j=0; j<4; j++)
for (int k=0; k <= 2*n; k++)
tallies[j][k] = 0;
fin >> g;
for (int i=0; i<g; i++) {
int k,x,y,s,t;
fin >> k >> x >> y >> s >> t;
for (int j=0; j<k; j++) {
tallies[0][x]++;
tallies[1][y]++;
tallies[2][x+y]++;
tallies[3][1+n+x-y]++;
x += s;
y += t;
}
}
long count = 0;
for (int j=0; j<4; j++)
for (int k=0; k <= 2*n; k++)
if (tallies[j][k] > 1)
count += (tallies[j][k] - 1);
cout << count << endl;
fin >> n;
}
}