Scientific Project#155

Scientific Project

1 second
64 megabytes
Easy

The scientific project consists of tasks, each executed on one of processors of a supercomputer. Tasks may run concurrently, but some can only start after certain other tasks are completed. For each task, its execution time and the list of prerequisite tasks (which may be empty) are given.

Find the minimal possible time required to complete all tasks. Output -1 if it is impossible to complete the project. All input numbers are positive integers not exceeding .

Input

The first line contains the integer .

Each of the following lines describes a task: the execution time and the list of prerequisite tasks (if any).

Output

Output a single number — the minimum time to complete all tasks, or -1 if it is impossible.

Examples

Example 1

Loading
Loading