Practice Topic 3 Game on Paper

Source: Internet
Author: User

Game on PaperTime limit:2000MS Memory Limit:262144KB 64bit IO Format:%i64 D &%i64u

Description

One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game.

He took a checkered white square piece of paper, consisting Of  n x n  cells. After this, he started to paint the white cells black one after the other. In total he painted  m  different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he Shou LD make till a black square with Side 3 can is found on the piece of paper. But Valera does isn't know the answer to this question, so he asks your to help him.

Your task is to find the minimum number of moves, till the checkered piece of paper have at least one black square with SID E of 3. Otherwise determine that such move does not exist.

Input

The first line contains the integers n and m(1≤ n ≤1000, 1 ≤ mmin(n· N,5)) -the size of the squared piece of paper and the number of moves, correspondingly.

Then,  m  lines contain the description of the moves. The  i -th line contains II Integers  x i ,   y i   ( 1≤ x i , y i n )-the number of row and column of the square that gets painted on THE   i -th move.

All numbers on the lines is separated by single spaces. It is guaranteed, all moves be different. The moves is numbered starting from 1 in the order, in which they is given in the input. The columns of the squared piece of paper is numbered starting from 1, from the left to the right. The rows of the squared piece of paper is numbered starting from 1, from top to bottom.

Output

On a single line print the answer to the problem-the minimum number of the move after which the piece of paper have a bla CK square with side 3. If No such move exists, print-1.

Sample Input

Input

1 1









4 1
Output
10
Input












3 1
Output
-1

Simple test Instructions:
   is in n * n, altogether can add M point,, ask at least fill a few points when can form 3*3 square ...
Thinking Analysis:
Can slowly fill, fill a point to judge, fill a point to judge. Solving
# include <iostream># include<fstream># include<cstdio># include<cstring>using namespacestd;Const intMAX =1001;intA[max][max];intm, N;intdx[]={1,0,-1,0,1,1,-1,-1,0};intdy[]={0,1,0,-1,1,-1,1,-1,0};BOOLFintXinty) {    intTX, ty, sum =0;  for(inti =0; I <9; i++) {TX= x +Dx[i]; Ty= y +Dy[i]; if(TX >=0&& TX < m && Ty >=0&& Ty <m) {if(A[tx][ty] = =9)            return true; A[tx][ty]++; if(A[tx][ty] = =9)            return true; }    }    return false;}intMain () {//fstream cin ("Aaa.txt");CIN >> M >>N; Memset (A,0,sizeof(a)); intx, y, flag =0;  for(inti =0; I < n; i++) {cin>> x >>y; if(f (x, y)) {cout<< i +1<<Endl; Flag=1;  Break; }    }    //cout << flag << Endl;    if(!flag) cout<<"-1"<<Endl;return 0;}

Practice Topic 3 Game on Paper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.