stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>



using namespace std;



const int N = 1e5+10;



int a[N];

bool q;



int main()

{

    q = false;



    int n;

    cin >> n;



    for (int i = 1; i <= n; i ++ ) cin >> a[i];



    int cnt = 0;

    int index = 1;

    for (int i = 0; i < n; i ++ )

    {

        index = a[index];

        cnt ++ ;

        if(index == 2)

        {

            q = true;

            break;

        }

    }



    if(q) cout << cnt << endl;

    else cout << "-1" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.273274632740497x):
#include<stdio.h>

int n,a[100005];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		scanf("%d",&a[i]);

	}

	int w=1,j=a[1];

		do{

		if(j==2)

		break;

		else{

			j=a[j];

			w++;

		}

	}while(w<=n);

	if(w>n)

	printf("-1");

	else

	printf("%d",w);

	return 0;	

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include <vector>

using namespace std;

int main()

{

	

	int n;

	vector<int>res;

	cin>>n;

	res.push_back(0);

	int num;

	for(int i=0;i<n;i++)

	{

		cin>>num;

		res.push_back(num);

	}

	int cnt = 1,index =res[1];

	while(1)

	{

		if(index ==2)

		{

			break;

		}

		index = res[index];

		cnt++;

		if(cnt>n)

		{

			cout<<-1<<endl;

			return 0;

		}

	}

	



	cout<<cnt<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.332407377033799x):
#include<stdio.h>

int n,a[100005];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		scanf("%d",&a[i]);

	}

	int w=1,j=a[1];

		do{

		if(j==2)

		break;

		else{

			j=a[j];

			w++;

		}

	}while(w<=n);

	if(w>n)

	printf("-1");

	else

	printf("%d",w);

	return 0;	

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<string>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<iomanip>

#include<vector>



#define vint vector<int>

#define vstr vector<string>

#define vll vector<long long>

#define ll long long

#define pf printf

#define pfd(n) printf("%d", n)

#define pft	printf("\t")

#define pfn printf("\n")

#define pfk printf(" ")



#define PI 3.1415926 



using namespace std;



int main() {

	int n;

	cin >> n;

	int a[n+1];

	int f[n+1];

	a[0] = 0;

	f[0] = 0;

	for( int i=1; i<=n; i++ ) {

		cin >> a[i];

		f[i] = 0;

	}

	f[1] = 1;

	int l = 1, d = 1, re = 0;

	while( 1 ) {

		if( l==2 ) {

			break;

		}

		l = a[l];

		f[l]++;

		re++;

//		l = a[d];

		if( f[l]>1 ) {

			re = -1;

			break;

		}

	}

	cout << re << endl;

	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.278772756709927x):
#include<stdio.h>

int n,a[100005];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		scanf("%d",&a[i]);

	}

	int w=1,j=a[1];

		do{

		if(j==2)

		break;

		else{

			j=a[j];

			w++;

		}

	}while(w<=n);

	if(w>n)

	printf("-1");

	else

	printf("%d",w);

	return 0;	

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 7.401861390756547x):
#include<stdio.h>

int n,a[100005];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		scanf("%d",&a[i]);

	}

	int w=1,j=a[1];

		do{

		if(j==2)

		break;

		else{

			j=a[j];

			w++;

		}

	}while(w<=n);

	if(w>n)

	printf("-1");

	else

	printf("%d",w);

	return 0;	

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3974823955748983x):


#include <algorithm>

#include <iostream>

#include <iomanip>

#include <cstring>

#include <string>

#include <cstdio>

#include <cmath>

#include <queue>

#include<set>

using namespace std;

typedef long long ll;

const int maxn=int(1e5 + 5);

int arr[maxn];

set<int>S;

int main()

{

	S.clear(); int sizes = 0;

	int n; cin >> n;

	for (int i = 1; i <= n; i++)scanf("%d", &arr[i]);

	int tmp = 1;

	int ans = 0,k=1;

	while (1) {

		 tmp = arr[tmp];

		 if (tmp == 2) { ans = k; break; }

		 S.insert(tmp); sizes++;

		 if (S.size() < sizes) { ans = -1; break; }

		 k++;

	}

	cout << ans;

	return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;



const int N = 1e5+10;



int a[N];

bool q;



int main()

{

    q = false;



    int n;

    cin >> n;



    for (int i = 1; i <= n; i ++ ) cin >> a[i];



    int cnt = 0;

    int index = 1;

    for (int i = 0; i < n; i ++ )

    {

        index = a[index];

        cnt ++ ;

        if(index == 2)

        {

            q = true;

            break;

        }

    }



    if(q) cout << cnt << endl;

    else cout << "-1" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.073743541070331x):
#include<stdio.h>

#include<stdlib.h>

int main(){

	int sj[100005];

	int a,b,c,i=1,d=0;

	scanf("%d",&c);

	for(i=1;i<=c;i++)

	{scanf("%d",&sj[i]);}

	a=1;b=sj[1];

	while(a!=2)

	{

	a=b;

	b=sj[a];

	d++;

	if(d>c)

	{printf("-1");

	return 0;}

	}

	printf("%d",d);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include <vector>

using namespace std;

int main()

{

	

	int n;

	vector<int>res;

	cin>>n;

	res.push_back(0);

	int num;

	for(int i=0;i<n;i++)

	{

		cin>>num;

		res.push_back(num);

	}

	int cnt = 1,index =res[1];

	while(1)

	{

		if(index ==2)

		{

			break;

		}

		index = res[index];

		cnt++;

		if(cnt>n)

		{

			cout<<-1<<endl;

			return 0;

		}

	}

	



	cout<<cnt<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.130638810199901x):
#include<stdio.h>

#include<stdlib.h>

int main(){

	int sj[100005];

	int a,b,c,i=1,d=0;

	scanf("%d",&c);

	for(i=1;i<=c;i++)

	{scanf("%d",&sj[i]);}

	a=1;b=sj[1];

	while(a!=2)

	{

	a=b;

	b=sj[a];

	d++;

	if(d>c)

	{printf("-1");

	return 0;}

	}

	printf("%d",d);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<string>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<iomanip>

#include<vector>



#define vint vector<int>

#define vstr vector<string>

#define vll vector<long long>

#define ll long long

#define pf printf

#define pfd(n) printf("%d", n)

#define pft	printf("\t")

#define pfn printf("\n")

#define pfk printf(" ")



#define PI 3.1415926 



using namespace std;



int main() {

	int n;

	cin >> n;

	int a[n+1];

	int f[n+1];

	a[0] = 0;

	f[0] = 0;

	for( int i=1; i<=n; i++ ) {

		cin >> a[i];

		f[i] = 0;

	}

	f[1] = 1;

	int l = 1, d = 1, re = 0;

	while( 1 ) {

		if( l==2 ) {

			break;

		}

		l = a[l];

		f[l]++;

		re++;

//		l = a[d];

		if( f[l]>1 ) {

			re = -1;

			break;

		}

	}

	cout << re << endl;

	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.0790336260594x):
#include<stdio.h>

#include<stdlib.h>

int main(){

	int sj[100005];

	int a,b,c,i=1,d=0;

	scanf("%d",&c);

	for(i=1;i<=c;i++)

	{scanf("%d",&sj[i]);}

	a=1;b=sj[1];

	while(a!=2)

	{

	a=b;

	b=sj[a];

	d++;

	if(d>c)

	{printf("-1");

	return 0;}

	}

	printf("%d",d);

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 7.12178845950446x):
#include<stdio.h>

#include<stdlib.h>

int main(){

	int sj[100005];

	int a,b,c,i=1,d=0;

	scanf("%d",&c);

	for(i=1;i<=c;i++)

	{scanf("%d",&sj[i]);}

	a=1;b=sj[1];

	while(a!=2)

	{

	a=b;

	b=sj[a];

	d++;

	if(d>c)

	{printf("-1");

	return 0;}

	}

	printf("%d",d);

	return 0;

}
// Slower C++ solution:


#include <algorithm>

#include <iostream>

#include <iomanip>

#include <cstring>

#include <string>

#include <cstdio>

#include <cmath>

#include <queue>

#include<set>

using namespace std;

typedef long long ll;

const int maxn=int(1e5 + 5);

int arr[maxn];

set<int>S;

int main()

{

	S.clear(); int sizes = 0;

	int n; cin >> n;

	for (int i = 1; i <= n; i++)scanf("%d", &arr[i]);

	int tmp = 1;

	int ans = 0,k=1;

	while (1) {

		 tmp = arr[tmp];

		 if (tmp == 2) { ans = k; break; }

		 S.insert(tmp); sizes++;

		 if (S.size() < sizes) { ans = -1; break; }

		 k++;

	}

	cout << ans;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.096156117640888x):
#include<stdio.h>

#include<stdlib.h>

int main(){

	int sj[100005];

	int a,b,c,i=1,d=0;

	scanf("%d",&c);

	for(i=1;i<=c;i++)

	{scanf("%d",&sj[i]);}

	a=1;b=sj[1];

	while(a!=2)

	{

	a=b;

	b=sj[a];

	d++;

	if(d>c)

	{printf("-1");

	return 0;}

	}

	printf("%d",d);

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4032433741971704x):
using namespace std;

#include <iostream>



int main()

{

	long int n;cin>>n;

	long int a[n];

	long int a1=1;

	long int sum=0;

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

	}

	for(int i=0;i<n;i++)

	{

		sum++;

		a1=a[a1-1];

		if(a1==2) break;

	}

	if(a1==2) cout<<sum<<endl;

	else cout<<-1<<endl; 

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3889575514036476x):
#define _USE_MATH_DEFINES

#include <algorithm>

#include <array>

#include <bitset>

#include <cassert>

#include <cctype>

#include <climits>

#include <clocale>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include <deque>

#include <fstream>

#include <functional>

#include <iomanip>

#include <iostream>

#include <iterator>

#include <list>

#include <map>

#include <numeric>

#include <queue>

#include <regex>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <tuple>

#include <unordered_map>

#include <unordered_set>

#include <utility>

#include <vector>



const int MOD = 1000000007; 

const int INF = 1000000000; //1e9

const int NIL = -1;

const long long LINF = 1000000000000000000; // 1e18

const double EPS = 1E-10;



template<class T, class S>

bool chmax(T &a, const S &b){

    if(a < b){

        a = b; return true;

    }

    return false;

}

template<class T, class S>

bool chmin(T &a, const S &b){

    if(b < a){

        a = b; return true;

    }

    return false;

}







 

 

int main(){

    int N; std::cin >> N;

    std::vector<int> a(N);

    for(int i(0); i < N; ++i){

        std::cin >> a[i];

        --a[i];

    }

    for(int cnt(0), cur(0); cnt <= N; ++cnt){

        if(cur == 1){

            std::cout << cnt << std::endl;

            return 0;

        }

        cur = a[cur];

    }

    std::cout << -1 << std::endl;

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4029395103536406x):
#include <iostream>

using namespace std;

int main(void){

    // Your code here!

    int n; cin >>  n;

    int a = 1;

    int v[n];

    for(int i=0  ; i<n ;  i++){

    //入力をします

    	cin >> v[i];

    }

    for(int i =0; i<n; i++){

        if(a != 2){

            a = v[a-1];

        }else{

            cout << i <<endl;

            break;

        }

        if(a !=2 && i ==n-1){

            cout << -1 << endl;

        }

    }

    

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

#include<iostream>

#include<cmath>

#include<algorithm>

#define debug(x) cout<<"***"<<x<<"***"<<endl;

using namespace std;

int main()

{

	int n,a[100005],light[100005],tmp=1,cnt=0;

	memset(light,0,sizeof(light));

	cin>>n;light[1]=1;

	for (int i=1;i<=n;i++)

		cin>>a[i];

	while (light[2]==0 && cnt<=n)//3 3 1 2

	{

		light[tmp]=0;

		tmp=a[tmp];

		light[tmp]=1;

		cnt++;

	}

	if (light[2]==1) cout<<cnt;

	else cout<<-1;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3909215008691718x):
#include<bits/stdc++.h>

using namespace std;

int main()

{

    int n;

    cin>>n;

    vector<int>vec;

    for(int i=0;i<n;i++)

    {

        int value;

        cin>>value;

        --value;

        vec.push_back(value);

    }

    int now=0,counter=0 ;

    for(;;)

    {

        if(now==1)

        {

            cout<<counter<<endl;

            break ;

        }

        if(counter>=n)

        {

            cout<<"-1\n";

            break ;

        }

        counter++;

        now=vec[now];

    }

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

using namespace std;

int a[100005];

int main()

{

	int n;

	cin >> n;

	for (int i = 1; i <= n; i++)

		cin >> a[i];

	int now = 1, cnt = 0;

	for (int i = 1; i <= n; i++)

	{

		now = a[now]; cnt++;

		if (now == 2)break;

	}

	if (now == 2)cout << cnt << endl;

	else cout << -1 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.280761463848987x):
#include<stdio.h>

int a[100005];

bool b[100005]={0};//deng 

int main()

{

	int n,tmp=0;

	scanf("%d",&n);

	for(int i=1;i<=n;++i){

		scanf("%d",&a[i]);

	}

	b[1]=1;

	int j=a[1];

	while(1){

		if(b[j]==1){

			printf("-1");

			break;

		}

		b[j]=1;

		j=a[j];

		++tmp;

		if(b[2]==1){

			printf("%d",tmp);

			break;

		}

	}

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

using namespace std;

int a[100005];

int main()

{

	int n;

	cin >> n;

	for (int i = 1; i <= n; i++)

		cin >> a[i];

	int now = 1, cnt = 0;

	for (int i = 1; i <= n; i++)

	{

		now = a[now]; cnt++;

		if (now == 2)break;

	}

	if (now == 2)cout << cnt << endl;

	else cout << -1 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.286920837378311x):
#include<stdio.h>

int n,a[200000],flg=0,ans,j=1;

int main(){

	scanf("%d",&n);

	for(int i=1;i<=n;++i){

		scanf("%d",&a[i]);

		}

	for(int i=1;i<=n;++i){

		j=a[j];

		ans++;

		if(j==2) {

			flg=1;

			break;

		}

	}

	if(flg==1)

	printf("%d",ans);

	else printf("-1");

	return 0;

}
// Slower C++ solution:
#include<stdio.h>

#include<iostream>

#include<string>

using namespace std;

long long int n,a[100005],x,y,sum;

int main(){

	cin>>n;

	for(long long int i=1;i<=n;i++)cin>>a[i];

	x=1;y=a[1];

	long long int j=0;

	while(y!=2){

		x=y;

		y=a[x];

		j++;

		if(j>n){ printf("-1");return 0;};

     };

	printf("%lld",j+1);

	return 0;}

// Faster C++ solution for the same problem (measured speedup: 5.257247219113448x):
#include<stdio.h>

int n,a[200000],flg=0,ans,j=1;

int main(){

	scanf("%d",&n);

	for(int i=1;i<=n;++i){

		scanf("%d",&a[i]);

		}

	for(int i=1;i<=n;++i){

		j=a[j];

		ans++;

		if(j==2) {

			flg=1;

			break;

		}

	}

	if(flg==1)

	printf("%d",ans);

	else printf("-1");

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

using namespace std;

int a[100005];

int main()

{

	int n;

	cin >> n;

	for (int i = 1; i <= n; i++)

		cin >> a[i];

	int now = 1, cnt = 0;

	for (int i = 1; i <= n; i++)

	{

		now = a[now]; cnt++;

		if (now == 2)break;

	}

	if (now == 2)cout << cnt << endl;

	else cout << -1 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.0815006335747075x):
#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#pragma warning(disable:4996)

#define max 100005

int main()

{

	int a[max], i, j, n, p = 0, sum=0,x,y;

	scanf("%d", &n);

	for (i = 1; i <= n; i++)

	{

		scanf("%d", &a[i]);

		if (a[i] == 2)p = i;

	}

	if (p == 0) { printf("-1"); return 0; }

	x = 1; y = a[x];

	while (y!=2)

	{

		y = a[y];

		sum++;

		if(sum>n-1) { printf("-1"); return 0; }

	}

	sum++;

	printf("%d", sum);

	return 0;

}
// Slower C++ solution:
#include<stdio.h>

#include<iostream>

#include<string>

using namespace std;

long long int n,a[100005],x,y,sum;

int main(){

	cin>>n;

	for(long long int i=1;i<=n;i++)cin>>a[i];

	x=1;y=a[1];

	long long int j=0;

	while(y!=2){

		x=y;

		y=a[x];

		j++;

		if(j>n){ printf("-1");return 0;};

     };

	printf("%lld",j+1);

	return 0;}

// Faster C++ solution for the same problem (measured speedup: 5.05297996631839x):
#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#pragma warning(disable:4996)

#define max 100005

int main()

{

	int a[max], i, j, n, p = 0, sum=0,x,y;

	scanf("%d", &n);

	for (i = 1; i <= n; i++)

	{

		scanf("%d", &a[i]);

		if (a[i] == 2)p = i;

	}

	if (p == 0) { printf("-1"); return 0; }

	x = 1; y = a[x];

	while (y!=2)

	{

		y = a[y];

		sum++;

		if(sum>n-1) { printf("-1"); return 0; }

	}

	sum++;

	printf("%d", sum);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

using namespace std;

int a[100005];

int main()

{

	int n;

	cin >> n;

	for (int i = 1; i <= n; i++)

		cin >> a[i];

	int now = 1, cnt = 0;

	for (int i = 1; i <= n; i++)

	{

		now = a[now]; cnt++;

		if (now == 2)break;

	}

	if (now == 2)cout << cnt << endl;

	else cout << -1 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.284474736295874x):
#include <stdio.h>

const int maxn=1e5+5;

int a[maxn];

int main()

{

	int n;

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&a[i]);

	}

	int x=1,ans=0;

	for(int i=0;i<=n;i++)

	{

		x=a[x];

		ans++;

		if(x==2)break;

	}

	if(x==2)printf("%d",ans);

	else printf("-1");

	return 0;

}
// Slower C++ solution:
#include<stdio.h>

#include<iostream>

#include<string>

using namespace std;

long long int n,a[100005],x,y,sum;

int main(){

	cin>>n;

	for(long long int i=1;i<=n;i++)cin>>a[i];

	x=1;y=a[1];

	long long int j=0;

	while(y!=2){

		x=y;

		y=a[x];

		j++;

		if(j>n){ printf("-1");return 0;};

     };

	printf("%lld",j+1);

	return 0;}

// Faster C++ solution for the same problem (measured speedup: 5.254814847131937x):
#include <stdio.h>

const int maxn=1e5+5;

int a[maxn];

int main()

{

	int n;

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&a[i]);

	}

	int x=1,ans=0;

	for(int i=0;i<=n;i++)

	{

		x=a[x];

		ans++;

		if(x==2)break;

	}

	if(x==2)printf("%d",ans);

	else printf("-1");

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <iostream>

using namespace std;



int main()

{

    int n;

    scanf("%d", &n);



    int a[n + 1] = {0};

    for (int i = 1; i <= n; i++) {

        cin >> a[i];

    }

    

    

    

    int idx = 1;

    int cnt = 1;

    for (int i = 0; i < n; i++) {

        if (a[idx] == 2) {

            printf("%d\n", cnt);

            break;

        } else {

            idx = a[idx];

            cnt++;

        }

    }

    if (cnt == n + 1) printf("%d\n", -1);

    

}


// Faster C++ solution for the same problem (measured speedup: 5.200713077525746x):
#include <cstdio>



int main()

{

    int n;

    scanf("%d", &n);



    int a[n + 1] = {0};

    for (int i = 1; i <= n; i++) {

        scanf("%d", &a[i]);

    }

    

    int idx = 1;

    int cnt = 1;

    for (int i = 0; i < n; i++) {

        if (a[idx] == 2) {

            printf("%d\n", cnt);

            break;

        } else {

            idx = a[idx];

            cnt++;

        }

    }

    if (cnt == n + 1) printf("%d\n", -1);

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <algorithm>



#define rep(i, n) for (int i = 0; i < n; i++)



using namespace std;



int main() {

  int N;

  int arr[100001] = {0};

  int tmp[100001] = {0};

  cin >> N;

  rep(i, N) {

    int a;

    cin >> a;

    arr[i] = a;

  }

  int cnt = 1;

  tmp[0] = 1;

  int next = arr[0] - 1;

  while (true) {

    if (next + 1 == 2) {

      cout << cnt << endl;

      break;

    }

    if (tmp[next] == 1) {

      cout << -1 << endl;

      break;

    }

    cnt++;

    tmp[next] = 1;

    next = arr[next] - 1;

  }



  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.793935815927513x):
#include <bits/stdc++.h>

#include <algorithm>



#define rep(i, n) for (int i = 0; i < n; i++)



using namespace std;



int main() {

  int N;

  cin >> N;

  vector<int> arr(N, 0);

  rep(i, N) { cin >> arr[i]; }

  int idx = 0;

  rep(i, N) {

    if (arr[idx] == 2) {

      cout << i + 1 << endl;

      return 0;

    } else {

      idx = arr[idx] - 1;

    }

  }

  cout << -1 << endl;

  return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;

int main()

{

	long long n;

	cin>>n;

	int a[n];

	for(long long i=0;i<n;i++)

	{

		cin>>a[i];

	}

	int b[n]={0};

	int count=1;

	int p=a[0];

	b[0]=1;

	while(p!=2)

	{

		b[p]=1;

		p=a[p-1];

		count++;

		if(b[p]==1)

		{

			cout<<"-1";

			return 0;

		}

	}

	cout<<count;

}

// Faster C++ solution for the same problem (measured speedup: 5.273189279056581x):
#include <stdio.h>

#pragma warning(disable:4996)



int a[100001], stts[100001];

int main() {

	int n;

	scanf("%d", &n);



	for (int i = 1; i <= n; i++) {

		scanf("%d", &a[i]);

	}



	int cnt = 0, pt = 1;

	while(pt!=2 && stts[pt]!=1){

		stts[pt] = 1;

		pt = a[pt];

		cnt++;

	}



	if (pt==2) {

		printf("%d\n", cnt);

	}

	else {

		printf("-1\n");

	}



	return 0;

}