CF285B.Find Marble

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Petya and Vasya are playing a game. Petya's got nn non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 11 to nn from left to right. Note that the positions are indexed but the glasses are not.

First Petya puts a marble under the glass in position ss . Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1p_{1} , the glass from the second position to position p2p_{2} and so on. That is, a glass goes from position ii to position pip_{i} . Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.

After all shuffling operations Petya shows Vasya that the ball has moved to position tt . Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position ss to position tt .

输入格式

The first line contains three integers: n,s,t (1<=n<=105; 1<=s,t<=n)n,s,t\ (1<=n<=10^{5}; 1<=s,t<=n) — the number of glasses, the ball's initial and final position. The second line contains nn space-separated integers: p1,p2,...,pn (1<=pi<=n)p_{1},p_{2},...,p_{n}\ (1<=p_{i}<=n) — the shuffling operation parameters. It is guaranteed that all pip_{i} 's are distinct.

Note that ss can equal tt .

输出格式

If the marble can move from position ss to position tt , then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position tt . If it is impossible, print number -1.

输入输出样例

  • 输入#1

    4 2 1
    2 3 4 1
    

    输出#1

    3
    
  • 输入#2

    4 3 3
    4 1 3 2
    

    输出#2

    0
    
  • 输入#3

    4 3 4
    1 2 3 4
    

    输出#3

    -1
    
  • 输入#4

    3 1 3
    2 1 3
    

    输出#4

    -1
    
首页