Time limit
2s
Memory limit
128 MB
There are N stones placed at equal intervals. Number them 1, 2, ..., N from left to right. You start on stone 1 and want to reach stone N while following these rules.
x, the next jump length may be x-1, x, or x+1. Every jump length must be at least 1.Find the minimum number of jumps needed to reach stone N from stone 1 while satisfying all rules. If it is impossible, output -1.
The first line contains two integers N and M. N is the number of stones, and M is the number of small stones that cannot be used.
2 <= N <= 10,000, and 0 <= M <= N-2.
Each of the next M lines contains the number of one small stone. Stones 1 and N are always usable.
Output the minimum number of jumps needed to reach stone N. If it is impossible, output -1.