problems / 200

Luggage Screening

easy brute-force arrays 1000 ms · 256 MB

Luggage Screening

At an airport security checkpoint, a screener needs to find a specific bag from a conveyor belt of identical-looking luggage. Each bag has 5 property values that can be inspected: properties 0 through 4. The screener has a checklist of up to 5 property requirements that the target bag must satisfy.

Your task is to find which bag (by position on the belt) matches all the requirements, checking each bag in order from the front of the conveyor.

Input

The first line contains n and k (1 ≤ n ≤ 10^5, 1 ≤ k ≤ 5), the number of bags and the number of requirements.

The next n lines each contain 5 integers, the property values (0 to 10^9) of each bag in order.

The next k lines each contain two integers p and v (0 ≤ p ≤ 4, 0 ≤ v ≤ 10^9), specifying that property p must equal v.

Output

Print the 0-based index of the first bag that satisfies all k requirements. If no bag matches, print -1.

Notes

The solution checks each bag in order against all requirements, stopping at the first match. This is a straightforward linear scan where constraints help prune which bags to accept.

Example 1

Input

3 2
10 20 30 40 50
10 25 30 40 50
10 20 30 40 55
0 10
2 30

Output

0

Example 2

Input

4 2
6 10 15 20 25
5 10 14 20 26
7 10 15 20 25
5 10 15 20 25
0 5
2 15

Output

3

Hints · reveal one at a time if you're stuck

Hint 1

Each bag only needs its requested properties compared, and the bags are in order. How quickly can you reject a single bag that fails a requirement?

Hint 2

The answer is the first bag in order that satisfies every requirement, so you can stop as soon as one is found. What should be printed if no bag qualifies?

report an issue Prefer your own editor? sooks prep 200 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.