URI Online Judge | 3202
# Whac-a-Mole

**Timelimit: 1**

By Kristoffer Arnsfelt Hansen Norway

While visiting a traveling funfair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the Whac-a-Mole game is to... well... whack moles. With a hammer. To make the job easier you have first consulted the fortune teller and now you know the exact appearance patterns of the moles.

The moles appear out of holes occupying the **n ^{2}** integer points (

The input consists of several test cases. Each test case starts with a line containing three integers **n**, **d** and **m**, where **n** and **d** are as described above, and **m** is the total number of moles that will appear (1 ≤ **n** ≤ 20, 1 ≤ **d** ≤ 5, and 1 ≤ **m** ≤ 1000). Then follow **m** lines, each containing three integers **x, y** and **t** giving the position and time of the appearance of a mole (0 ≤ **x, y** < **n** and 1 ≤ **t** ≤ 10). No two moles will appear at the same place at the same time. The input is ended with a test case where **n** = **d** = **m** = 0. This case should not be processed.

For each test case output a single line containing a single integer, the maximum possible score achievable.

Input Sample | Output Sample |

4 2 6 |
4 2 |