Moo Deng’s Bounces

Time limit: 0.5sMemory limit: 256MBInput: Output:

Task

Moo Deng, the famous pygmy hippo, wants to cross a track made of NN tiles, numbered from 00 to N−1N-1 from left to right. She starts on tile 00 and wants to reach tile N−1N-1.

Every tile ii has a jump power BiB_i: standing on tile ii, Moo Deng can jump to any tile jj such that 1≤j−i≤Bi1 \le j-i \le B_i, that is, forward by at least 11 and at most BiB_i tiles. Each jump counts as one move.

Some tiles are wet. If Moo Deng lands on a wet tile kk, she cannot stop there: she instantly slides forward to tile SkS_k, where Sk>kS_k > k. If SkS_k is wet as well, she keeps sliding, until she reaches a dry tile. Sliding is free: it does not count as a jump. Tiles 00 and N−1N-1 are always dry.

One way to cross the track of the first sample case. Wet tiles are blue, jumps are solid arrows, slides are dashed. Note that the figure is partially complete.

Help Moo Deng find the minimum number of jumps she needs to reach tile N−1N-1, or tell her that it is impossible.

Input data

The first line contains the only integer NN.

The second line contains NN integers B0,…,BN−1B_0, \ldots, B_{N-1}.

The third line contains NN integers S0,…,SN−1S_0, \ldots, S_{N-1}: Sk=−1S_k = -1 if tile kk is dry, otherwise tile kk is wet and Moo Deng slides from it to tile SkS_k.

Output data

You need to write a single line with an integer: the minimum number of jumps needed to reach tile N−1N-1, or −1-1 if tile N−1N-1 cannot be reached.

Constraints and clarifications

  • 1≤N≤200 0001 \le N \le 200\ 000.
  • 0≤Bi≤N−1−i0 \le B_i \le N-1-i for each i=0,…,N−1i=0,\ldots,N-1 (in particular, BN−1=0B_{N-1}=0).
  • Sk=−1S_k=-1 or k<Sk≤N−1k < S_k \le N-1 for each k=0,…,N−1k=0,\ldots,N-1.
  • S0=SN−1=−1S_0=S_{N-1}=-1.
# Score Constraints
0 0 Examples.
1 10 N≤8N \le 8.
2 15 N≤2 000N \le 2\ 000 and there are no wet tiles (Sk=−1S_k=-1 for each kk).
3 25 N≤2 000N \le 2\ 000 and every slide ends on a dry tile (SSk=−1S_{S_k}=-1 for each wet tile kk).
4 40 N≤2 000N \le 2\ 000.
5 10 No additional restrictions.

Example

stdin

8
3 1 1 1 2 0 1 0
-1 -1 4 5 6 -1 -1 -1

stdout

2

Explanation

In the first sample case tiles 22, 33 and 44 are wet.

The figure shows one way to reach tile 77: Moo Deng jumps from tile 00 to tile 11, then from tile 11 to tile 22. Tile 22 is wet, so she slides to tile 44; tile 44 is wet too, so she keeps sliding to tile 66. Finally she jumps from tile 66 to tile 77. This route uses 33 jumps, but it is not the best one.

From tile 00 Moo Deng can jump to tile 11, 22 or 33.

  • If she jumps to tile 22, she slides to tile 44, which is also wet, so she keeps sliding to tile 66. From there one more jump takes her to tile 77. This route uses only 22 jumps, which is the best possible: no single jump from tile 00 reaches tile 77.
  • Jumping to tile 33 looks tempting, because it is the farthest choice, but she slides to tile 55, where B5=0B_5=0: she is stuck forever.
  • Jumping to tile 11 is not useful: from there she can only jump to tile 22, reaching tile 66 with 22 jumps instead of 11.

Notice that B2B_2, B3B_3 and B4B_4 do not matter, because Moo Deng never stops on a wet tile.

Example

stdin

4
2 1 0 0
-1 2 -1 -1

stdout

-1

Explanation

In the second sample case jumping to tile 11 makes Moo Deng slide to tile 22, and jumping directly to tile 22 gets her there too. Since B2=0B_2=0, tile 33 cannot be reached, and the answer is −1-1.

Log In or Sign Up to be able to send submissions!