//* Problem  : G - Whac-a-mole
//* Contest  : acm.tju.edu.cn
//* Origin   : Nordic CPC 2006
//* Type     : Solution
//* Date     : 2008.05.29
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 8.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <iostream>
using namespace std;
#define int64 long long

int n, d, m, res;

int pr[32][32][32];
int dp[32][32][32];

int c[32][32][32][32][16];

inline int len(int x0, int y0, int xn, int yn)
{
	int dx = x0 - xn;
	int dy = y0 - yn;
	return dx * dx + dy * dy;
}

int countcount(int x, int y, int t)
{
	if (t == 11)
		return 0;
	if (!dp[t][x][y])
	{
		int res = 0;
		for (int xn = 0; xn < n + 10; xn++)
			for (int yn = 0; yn < n + 10; yn++)
				if (len(x, y, xn, yn) <= d * d)
				{
					int tt = c[x][y][xn][yn][t] + countcount(xn, yn, t + 1);
					res = max(res, tt);
				}
		dp[t][x][y] = res + 1;
	}
	return dp[t][x][y] - 1;
}

inline int gcd(int x, int y)
{
	while (x && y)
		if (x > y)
			x %= y;
		else
			y %= x;
	return (x + y);
}

void fill()
{
	for (int x0 = 0; x0 < n + 10; x0++)
		for (int y0 = 0; y0 < n + 10; y0++)
			for (int xn = 0; xn < n + 10; xn++)
				for (int yn = 0; yn < n + 10; yn++)
					if (len(x0, y0, xn, yn) <= d * d)
					{
						int dx = xn - x0, dy = yn - y0;
						int d = gcd(abs(dx), abs(dy));
						if (d)
						{
							dx /= d; dy /= d;
						}
						for (int t = 1; t <= 10; t++)
						{
							int v = 0, xx = x0, yy = y0;
							v += pr[t][xx][yy];
							while (xx != xn || yy != yn)
							{
								xx += dx; yy += dy;
								v += pr[t][xx][yy];
							}
							c[x0][y0][xn][yn][t] = v;
						}
					}
}

int main()
{
#ifdef _DEBUG
 	freopen("g.in", "r", stdin);
 	freopen("g.out", "w", stdout);
#endif
	while (scanf("%d%d%d", &n, &d, &m) && n)
	{
		memset(pr, 0, sizeof(pr));
		memset(dp, 0, sizeof(dp));
		memset(c, 0, sizeof(c));
		for (int i = 0; i < m; i++)
		{
			int x, y, t;
			scanf("%d%d%d", &x, &y, &t); pr[t][x + 5][y + 5] = 1;
		}
		fill();
		int res = 0;
		for (int i = 0; i < n + 10; i++)
			for (int j = 0; j < n + 10; j++)
				res = max(res, countcount(i, j, 0));
		printf("%d\n", res);
	}	
	return 0;
}
