LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

First Crossing

MediumProblem #75
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Cells are only ever added, never removed - and the question is asked after every single one.

An n×n grid starts out full of zeros. Then ones are written into it, one cell at a time, in a given order.

You may walk across the grid by stepping between cells that hold a one, moving only up, down, left or right - never diagonally. Crossing the grid means starting on any cell of the top row and reaching any cell of the bottom row this way.

After each one is written, the grid may or may not be crossable. Find how many ones have to be written before it becomes crossable for the first time.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains two integers n and m - the side of the grid and how many ones will be written.
Each of the next m lines contains two integers r and c - the row and column of the next one, both counted from 0.

No cell is listed twice.

Output

For every testcase print a single line with the number of ones written when the grid first becomes crossable, or −1 if it never does.

Example

Input
3
4 9
0 0
0 1
1 1
3 3
1 3
2 0
3 0
2 1
2 2
1 1
0 0
3 2
0 0
2 2
Output
8
1
-1

After the eighth one the first grid looks like this, and the marked cells lead from the top row to the bottom:

1100
0101
1100
1001

In the second testcase the grid is a single cell, which is both the top row and the bottom row, so one write is enough. In the third the two ones sit in opposite corners and never touch.

Constraints

1≤t≤10
1≤n≤200
1≤m≤n2
0≤r,c≤n−1
The sum of m over all testcases does not exceed 105


This problem was adapted, with permission, from Prvi put kroz matricu, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit