Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/ochkoz-eng-kam-sakrashlar

Fewest jumps

Medium 1000 ms 128 MB 64%
Share

There are $n$ cells in a row, and the $i$-th cell holds the number $a_{i}$: from this cell you may jump forward by at most $a_{i}$ cells (that is, to one of the cells $i + 1, \dots, i + a_{i}$). What is the smallest number of jumps needed to get from the first cell to the last one? If it cannot be reached, print $-1$.

A row of cells; the jump length from each cell, the path with the fewest jumps highlighted with arcs

Input

The first line contains a positive integer $n$.
The second line contains $n$ integers $a_{i}$.

Output

One integer — the smallest number of jumps or $-1$.

Note

$1 \le n \le 10^{5}$
$0 \le a_{i} \le 10^{5}$

Examples

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

Sign in to submit a solution

Signing up is free and takes a minute.