Lucky Sudoku

Time limit: 2s Memory limit: 256MB Input: Output:

Task

JellyTheOctopus is ready to play today's New York Times Sudoku! Just for him, the sudoku setters have added a new difficulty "Impossible", to the normal "Easy", "Medium" and "Hard".

The rules of normal Sudoku are as follows: you are given a 99 x 99 grid, split into 9 39 \ 3 x 33 boxes. Place the digits 11 through 99 each in every row, column, and box such that each digit appears only once. However, in today's new difficulty, there are a few minor changes. Instead of a 99 x 99 grid, there is a NN x NN grid, split into many 33 x 33 boxes (NN is a multiple of 33).

Due to the nature of these new grid sizes, some digits might already have repeats (in a column or row). However, any new digits added by Jelly must still follow normal sudoku rules (E.g. if there are three ones in a column, Jelly cannot place another one in that column). You can see here a picture of a normal sudoku grid.

As the name suggests, it is very likely that this puzzle is impossible to solve. Thus, Jelly has created a new challenge for himself. As we all know, 88 is the luckiest number, and Jelly wants to find the "luckiness" of the grid. Given an NN x NN grid, find all positions where Jelly can place a single 88.

Jelly will only write a single digit. If he places an 88 in another position, he will erase it off the grid before considering other positions that he can place an 88 at.

The luckiness of the grid is calculated as the number of positions he can place a single 88 at. Help Jelly find this number before NYT publishes their next Impossible Sudoku Puzzle!

Input data

In the first line you are given an integer NN and MM, the number of places already filled.

On the next line you are given the coordinates where we already placed digits, together with the digit we placed.

Output data

The output will contain the luckiness of the grid Jelly received as part of today's Impossible New York Times Sudoku Puzzle.

Constraints and clarifications

  • 3N21053 \leq N \leq 2 \cdot 10^5;
  • NN is a multiple of 33;
  • 0Mmin(2105,N2)0 \leq M \leq \min(2 \cdot 10^5, N^2);
  • 0x,y<N0 \leq x, y < N;
  • 1digit91 \leq digit \leq 9;
# Score Constraints
0 0 Example
1 6 M=0M = 0
2 22 1n2001 \leq n \leq 200
3 35 1n2 0001 \leq n \leq 2 \ 000
4 37 No additional constraints

Example

stdin

9 7
1 7 8
3 2 1
4 7 3
1 0 5
8 7 4
2 4 9
3 4 6

stdout

57

Explanation

In the example grid, he can't place a 88 on any of the squares already filled, as well as on any of the squares on line 11 and column 77.

Log in or sign up to be able to send submissions!