AlgoUniversity
  • Go Back
Discussion
Book :

Author

Ujjwal Srivastava

Difficulty Level : Medium

Submissions : 312

Asked In : Jaguar

Marks :30

: 2 | : 0

You are given a book with $$$n$$$ chapters.

Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list.

Currently, you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter.

Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book.

Input

The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — number of chapters.

Then $$$n$$$ lines follow. The $$$i_{th}$$$ line begins with an integer $$$k_{i}$$$ $$$(0 \le k_{i} \le n−1)$$$ — number of chapters required to understand the $$$i_{th}$$$ chapter. Then $$$k_{i}$$$ integers $$$a_{i,1},a_{i,2},…,a_{i,ki}$$$ $$$(1 \le a_{i,j} \le n, a_{i,j} ≠ i, a_{i,j} ≠ a_{i,l} for j≠l)$$$ follow — the chapters required to understand the i-th chapter.

It is guaranteed that the sum of $$$k_{i}$$$ do not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, if the entire book can be understood, print how many times you will read it, otherwise print −1.

Examples

Input
5
1 2
1 3
1 4
1 5
0
Output
5
Input
5
1 5
1 1
1 2
1 3
1 4
Output
-1

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.