#abc355c. C - Bingo 2

C - Bingo 2

Score : 300300 points

问题陈述

有一个 N×NN \times N 的网格,其中从顶部第 ii 行和从左侧第 jj 列的单元格包含整数 N×(i1)+jN \times (i-1) + j

TT 轮中,会宣布整数。在第 ii 轮,宣布整数 AiA_i,并且包含 AiA_i 的单元格被标记。确定首次实现 Bingo 的轮次。如果在 TT 轮内没有实现 Bingo,则打印 -1

在这里,实现 Bingo 意味着满足以下条件之一:

  • 存在一行,其中所有 NN 个单元格都被标记。
  • 存在一列,其中所有 NN 个单元格都被标记。
  • 存在一条对角线(从左上到右下或从右上到左下),其中所有 NN 个单元格都被标记。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is an N×NN \times N grid, where the cell at the ii-th row from the top and the jj-th column from the left contains the integer N×(i1)+jN \times (i-1) + j.

Over TT turns, integers will be announced. On Turn ii, the integer AiA_i is announced, and the cell containing AiA_i is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within TT turns, print -1.

Here, achieving Bingo means satisfying at least one of the following conditions:

  • There exists a row in which all NN cells are marked.
  • There exists a column in which all NN cells are marked.
  • There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all NN cells are marked.

Constraints

  • 2N2×1032 \leq N \leq 2 \times 10^3
  • 1Tmin(N2,2×105)1 \leq T \leq \min(N^2, 2 \times 10^5)
  • 1AiN21 \leq A_i \leq N^2
  • AiAjA_i \neq A_j if iji \neq j.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN TT

A1A_1 A2A_2 \ldots ATA_T

Output

If Bingo is achieved within TT turns, print the turn number on which Bingo is achieved for the first time; otherwise, print -1.

Sample Input 1

3 5
5 1 8 9 7

Sample Output 1

4

The state of the grid changes as follows. Bingo is achieved for the first time on Turn 44.

Sample Input 2

3 5
4 2 9 7 5

Sample Output 2

-1

Bingo is not achieved within five turns, so print -1.

Sample Input 3

4 12
13 9 6 5 2 7 16 14 8 3 10 11

Sample Output 3

9