Under construction

Still being built. Things may change

Magic Compass

Interview

Time limit1sMemory limit1024 MB

Summary
Given M compass readings that each give the direction from a cell to a hidden treasure on an N x N grid, find the treasure's cell.
Level

Easy2 of 10

Topics
Brute force, Implementation
Solved
No attempts yet

Problem

Use a magic compass to find a treasure hidden in an N×NN \times N area.

The figure above shows the first sample. When the compass is activated, its red needle points toward the treasure in one of eight directions: up, down, left, right, or one of the four diagonals.

The directions are numbered 1 to 8 clockwise, starting from the top. The coordinates (x,y)(x, y) denote the cell in the xx-th row from the top and the yy-th column from the left. (xx and yy are integers from 1 to NN.)

  • Direction 1: the treasure has a smaller xx coordinate and the same yy coordinate.
  • Direction 2: the treasure has a smaller xx coordinate and a larger yy coordinate.
  • Direction 3: the treasure has the same xx coordinate and a larger yy coordinate.
  • Direction 4: the treasure has a larger xx coordinate and a larger yy coordinate.
  • Direction 5: the treasure has a larger xx coordinate and the same yy coordinate.
  • Direction 6: the treasure has a larger xx coordinate and a smaller yy coordinate.
  • Direction 7: the treasure has the same xx coordinate and a smaller yy coordinate.
  • Direction 8: the treasure has a smaller xx coordinate and a smaller yy coordinate.

Using the records of MM compass uses, find the location of the treasure. The compass position and the treasure position are always different, and the answer is guaranteed to be unique.

Input

The first line contains two integers NN and MM, separated by a space. (2≤N≤102 \le N \le 10, 1≤M<N21 \le M < N^2)

Each of the next MM lines contains XiX_i, YiY_i, and KiK_i, separated by spaces. These mean that the compass was used at (Xi,Yi)(X_i, Y_i) and the treasure was in direction KiK_i from that position. (1≤Xi,Yi≤N1 \le X_i, Y_i \le N, 1≤Ki≤81 \le K_i \le 8, all integers)

Output

Print the coordinates of the cell where the treasure is hidden.

Examples2

  1. Example 1

    Input
    5 10
    1 1 4
    1 2 4
    1 5 6
    2 3 5
    3 2 3
    3 5 7
    4 5 8
    5 1 2
    5 3 1
    5 4 8
    
    Expected output
    3 3
    
  2. Example 2

    Input
    3 2
    1 2 6
    3 3 8
    
    Expected output
    2 1