Under construction

Still being built. Things may change

Toy Vending Machine

Time limit1sMemory limit1024 MB

Summary
Coins travel a tree of tubes, choosing the wider tube (left on ties) and reducing its width by 1; count the coins needed for a coin to first reach node v.
Level

Hard8 of 10

Topics
Tree, Simulation, Greedy, Dynamic programming
Solved
No attempts yet

Problem

An arcade in city E installed a new generation game machine. You can drop a coin into the machine and watch it travel from top to bottom through a branching maze of tubes. The maze has nn nodes, numbered from 1 to nn. When a coin is dropped, it enters the first node. Every node except the first has exactly one tube entering from above, through which a coin can reach it. Each node has at most two tubes going down, one leading left and the other leading right. Each tube has a width. A coin falls into the wider tube, and if the two tubes have equal width, it falls into the left one.

After a coin passes through a tube, that tube's width decreases by 1. A coin cannot pass through a tube of width 0. If a coin reaches a node from which it cannot move further down, the machine stops and waits for the next coin to be dropped.

Initially, each node of the maze holds one toy. When a coin enters a node for the first time, the toy in that node is taken by the player who dropped that coin.

Pankrat likes the toy in the node numbered vv.

Write a program that determines how many coins Pankrat must drop into the machine to get the toy from node vv.

Input

The first line contains the number nn, the number of nodes in the maze. The next nn lines describe all the nodes; the kk-th of these lines describes the node numbered kk.

The description of node kk consists of four integers: aka_k, uku_k, bkb_k, wkw_k. If node kk has a left tube, then aka_k is the number of the node it leads to (k<ak⩽nk < a_k \leqslant n) and uku_k is its width. If there is no left tube, then ak=uk=0a_k = u_k = 0. If node kk has a right tube, then bkb_k is the number of the node it leads to (k<bk⩽nk < b_k \leqslant n) and wkw_k is its width. If there is no right tube, then bk=wk=0b_k = w_k = 0.

The last line contains the integer vv (1⩽v⩽n1 \leqslant v \leqslant n), the number of the node holding the toy Pankrat likes.

Every node except the first has exactly one tube entering it.

Output

Print one number: the number of coins Pankrat must drop into the machine to get the toy in node vv. If the chosen toy cannot be obtained, print −1-1.

Constraints

  • 1≤n⩽1051 \le n \leqslant 10^5
  • 1≤uk,wk⩽1091 \le u_k, w_k \leqslant 10^9

Notes

In the first example, the first coin travels the maze along the following path, and the player gets the toys from nodes 1, 3, and 4:

The second coin travels the maze along the following path, and the player gets the toys from nodes 2 and 6:

The third coin travels the maze along the following path, and the player gets the toys from nodes 5 and 7:

Examples2

  1. Example 1

    Input
    7
    2 1 3 2
    0 0 6 3
    4 1 5 1
    0 0 0 0
    7 2 0 0
    0 0 0 0
    0 0 0 0
    5
    
    Expected output
    3
    
  2. Example 2

    Input
    4
    0 0 2 1
    4 1 3 1
    0 0 0 0
    0 0 0 0
    3
    
    Expected output
    -1