Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- /*ifstream f("test.in");
- ofstream g("test.out");*/
- int main()
- {
- int n,a,v[100001];
- cin>> n >> a;
- for(int i=1; i<=n; i++)
- {
- cin>> v[i];
- }
- if(a==1)
- {
- cout<<0;
- return 0;
- }
- int poz = 1,nrpasi=0;
- do
- {
- poz = v[poz];
- nrpasi++;
- }
- while(poz != a && poz != 1);
- if(poz==1)
- {
- cout << -1;
- }
- else
- {
- cout<<nrpasi;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement