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

#include <algorithm>

#include <array>

using namespace std;



int main() {

    int N;

    cin >> N;



    int a[100000] = {0,};



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

        cin >> a[i];



    int counter = 1;

    int ai = a[0];

    while (0 <= N && ai != 2 && ai != -1) {

        int n = ai-1;

        ai = a[n];

        a[n] = -1;

        counter++;

        N--;

    }



    if (ai == 2) {

        cout << counter << endl;

    } else {

        cout << -1 << endl;

    }

}

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

#include <algorithm>

#include <vector>

using namespace std;



int main() {

    int N;

    cin >> N;



    vector<int> v;



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

        int n;

        cin >> n;

        v.push_back(--n);

    }



    int now = 0, c = 0;

    for (;;) {

        if (now == 1) {

            cout << c << endl;

            break;

        }

        if (c >= N) {

            cout << -1 << endl;

            break;

        }

        c++;

        now = v[now];

    }

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

#include <string>

#include <vector>

#include <algorithm>

#include <queue>

#include <map>

#include <utility>

#include <functional>



using namespace std;



int main() {

	int n, a[100001];



	cin >> n;

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

		cin >> a[i];

	}



	int s = 1, ans = 0;

	bool used[100001] = { false };

	while (1) {

		ans++;

		used[s] = true;

		s = a[s];

		if (s == 2)

			break;

		if (used[s]) {

			cout << -1 << endl;

			return 0;

		}

	}



	cout << ans << endl;



	return 0;

}

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

#include <set>

#include <list>

#include <cmath>

#include <deque>

#include <queue>

#include <stack>

#include <cstdio>

#include <string>

#include <vector>

#include <complex>

#include <cstdlib>

#include <cstring>

#include <iomanip>

#include <numeric>

#include <utility>

#include <iostream>

#include <algorithm>

#include <functional>



using namespace std;



#define INF (1 << 30)

#define INFL (1LL << 62)

#define MOD7 1000000007

#define MOD9 1000000009

#define EPS 1e-10



#define ll long long

#define ull unsigned long long

#define pii pair<int, int>

#define pll pair<ll, ll>

#define pul pair<ull, ull>

#define all(a) (a).begin(), (a).end()

#define FOR(i,a,b) for (int i=(a);i<(b);++i)

#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)

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

#define RREP(i,n) for (int i=(n)-1;i>=0;--i)



void YES() { cout << "YES" << endl; }

void NO() { cout << "NO" << endl; }

void Yes() { cout << "Yes" << endl; }

void No() { cout << "No" << endl; }

void yes() { cout << "yes" << endl; }

void no() { cout << "no" << endl; }

void Taka() { cout << "Takahashi" << endl; }

void Aoki() { cout << "Aoki" << endl; }

void digit(int d) { cout << fixed << setprecision(d); }

void IOup() { cin.tie(0); ios::sync_with_stdio(false); }



int a[100001];

bool used[100001];



int main() {

	int n;

	cin >> n;

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

		cin >> a[i];



	int s = 1, ans = 0;

	while (1) {

		ans++;

		used[s] = true;

		s = a[s];

		if (used[s]) {

			ans = -1;

			break;

		}

		if (s == 2)

			break;

	}



	cout << ans << endl;



	return 0;

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

using namespace std;

typedef long long ll;

#define INF (1<<30)

#define INFLL (1ll<<60)

typedef pair<int, int> P;

typedef pair<ll, P> E;

#define cost first

#define from second.first

#define to second.second

#define MOD (1000000007ll)

#define l_ength size



void mul_mod(ll& a, ll b){

	a *= b;

	a %= MOD;

}



int main(void){

	int n,a[123456],i,t[123456],now=0;

	fill(t,t+123456,-1);

	t[0] = 0;

	cin >> n;

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

		cin >> a[i];

		a[i]--;

	}

	while(true){

		if(t[a[now]]>0){

			break;

		}

		t[a[now]] = t[now] + 1;

		now = a[now];

	}

	cout << t[1] << endl;

	return 0;

}

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

using namespace std;

typedef long long ll;

#define INF (1<<30)

#define INFLL (1ll<<60)

typedef pair<int, int> P;

typedef pair<int, P> E;

#define MOD (1000000007ll)

#define l_ength size



void mul_mod(ll& a, ll b){

	a *= b;

	a %= MOD;

}



void add_mod(ll& a, ll b){

	b += MOD;

	a += b;

	a %= MOD;

}



int main(void){

	int n,i,a[123456],ans=0,p=0;

	bool done[123456];

	fill(done,done+123456,false);

	cin >> n;

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

		cin >> a[i];

		--a[i];

	}

	while(p != 1){

		if(done[p]){

			ans = -1;

			break;

		}

		done[p] = true;

		++ans;

		p = a[p];

	}

	cout << ans << endl;

	return 0;

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



using namespace std;



int main()

{

	int n;

	cin >> n;

	int count = 1;

	array<int, 100000> a = {};

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

	

	array<bool, 100000> check = {};

	for (int i = 0; a[i] != 2; i = a[i]-1)

	{

		if (check[i])

		{

			count = -1;

			break;

		}

		count++;

		check[i] = true;

	}



	cout << count << endl;

	return 0;

}


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



using namespace std;



int main()

{

	int n;

	cin >> n;

	int count = 1;

	vector<int> a(n);

	for (auto&& ai : a) cin >> ai;

	

	vector<bool> check(n, false);

	for (int i = 0; a[i] != 2; i = a[i]-1)

	{

		if (check[i])

		{

			count = -1;

			break;

		}

		count++;

		check[i] = true;

	}



	cout << count << endl;

	return 0;

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



using namespace std;



int main()

{

	int n;

	cin >> n;

	int count = 1;

	array<int, 100000> a = {};

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

	

	array<bool, 100000> check = {};

	for (int i = 0; a[i] != 2; i = a[i]-1)

	{

		if (check[i])

		{

			count = -1;

			break;

		}

		count++;

		check[i] = true;

	}



	cout << count << endl;

	return 0;

}


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



using namespace std;



int main()

{

    int N;

    array<int, 100000> a;

    array<bool, 100000> b;



    b.fill(false);



    cin >> N;



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

    {

        cin >> a[i];

    }



    int x = 1;

    int cnt = 1;



    int nxt;

    while (true)

    {

        nxt = a[x - 1];

        if (nxt == 2) break;



        if (b[nxt - 1]) break;

        b[nxt - 1] = true;



        x = nxt;

        cnt++;

    }



    printf("%d\n", nxt == 2 ? cnt : -1);

    return 0;

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



using namespace std;



int main()

{

	int n;

	cin >> n;

	int count = 1;

	array<int, 100000> a = {};

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

	

	array<bool, 100000> check = {};

	for (int i = 0; a[i] != 2; i = a[i]-1)

	{

		if (check[i])

		{

			count = -1;

			break;

		}

		count++;

		check[i] = true;

	}



	cout << count << endl;

	return 0;

}


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



using namespace std;



template<class T> void gc(T &x)

{

    char c;

    int s = 0;

    x = 0;



    c = getchar_unlocked();



    if (c == '-') s = 1;

    else if ('0' <= c && c <= '9')  x = c - '0';



    while (true)

    {

        c = getchar_unlocked();



        if (c < '0' || c > '9') break;

        x = x * 10 + (c - '0');

    }



    if (s) x = -x;

}



int main()

{

    int N;

    array<int, 100000> a;

    array<bool, 100000> b;



    b.fill(false);



    gc(N);



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

    {

        gc(a[i]);

    }



    int x = 1;

    int cnt = 1;



    int nxt;

    while (true)

    {

        nxt = a[x - 1];

        if (nxt == 2) break;



        if (b[nxt - 1]) break;

        b[nxt - 1] = true;



        x = nxt;

        cnt++;

    }



    printf("%d\n", nxt == 2 ? cnt : -1);

    return 0;

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

#include <vector>



using namespace std;



int main()

{

    int n;

    cin >> n;



    vector<int> v(100001);

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

        cin >> v[i];

    }



    int cnt = 1;

    int i = 1;

    while (n--) {

        if (v[i] == 2) {

            cout << cnt << endl;

            return 0;

        }

        i = v[i];

        ++cnt;

    }

    cout << -1 << endl;

}

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



int main()

{

    int n;

    scanf("%d", &n);



    int a[100005] = {};

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

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

    }



    int cnt = 1;

    int i = 1;

    while (n--) {

        if (a[i] == 2) {

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

            return 0;

        }

        i = a[i];

        ++cnt;

    }

    printf("-1\n");

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

#include <vector>



using namespace std;



int main()

{

    int n;

    cin >> n;



    vector<int> v(100001);

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

        cin >> v[i];

    }



    int cnt = 1;

    int i = 1;

    while (n--) {

        if (v[i] == 2) {

            cout << cnt << endl;

            return 0;

        }

        i = v[i];

        ++cnt;

    }

    cout << -1 << endl;

}

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



int main()

{

    int n;

    scanf("%d", &n);



    int a[100005] = {};

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

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

    }



    int cnt = 1;

    int i = 1;

    while (n--) {

        if (a[i] == 2) {

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

            return 0;

        } else if (i == a[a[i]]) {

            break;

        }

        i = a[i];

        ++cnt;

    }

    printf("-1\n");

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

#include<cstdio>

#define mod 1000000007

using namespace std;

int main() {

	int n;

	int a[100500];

	scanf("%d",&n);

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

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

	}

	int cnt=1;

	bool b[100500]= {0};

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

		if(a[i]!=2) {

			if(b[i]==1) {

				printf("-1");

				break;

			} else {

				cnt++;

				b[i]=1;

				i=a[i];

			}

		}else{

			printf("%d",cnt);

			break;

		}

	}

	return 0;

}

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

#include <cstdio>

#include <cmath>

#include <string>

#include <cstring>

#include <algorithm>

#include <iomanip>

#include <cstdlib>

#include <cctype>

#define ll long long

#define PI 3.14159265358979323846

using namespace std;

const int maxn = 1e6+7;



int main(){

	int n;

	cin>>n;

	int *a=new int[n];

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

		cin>>a[i];

	}

	int j=0;

	int num=0;

	int flag=0;

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

		if(a[j]==2){

			flag=1;

			break;

		}

		num++;

		j=a[j]-1;

	}

	if(flag){

		cout<<num+1<<endl;

	}

	else{

		cout<<-1<<endl;

	}

	return 0;

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

#include<cstdio>

#define mod 1000000007

using namespace std;

int main() {

	int n;

	int a[100500];

	scanf("%d",&n);

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

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

	}

	int cnt=1;

	bool b[100500]= {0};

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

		if(a[i]!=2) {

			if(b[i]==1) {

				printf("-1");

				break;

			} else {

				cnt++;

				b[i]=1;

				i=a[i];

			}

		}else{

			printf("%d",cnt);

			break;

		}

	}

	return 0;

}

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

#define N 100005

using namespace std;

int a[N];

int main()

{

    int n;

    while (~scanf("%d", &n)) {

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

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

        int x = 1,ans = 1, flag = 0;

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

            x = a[x];

            ans++;

            if (x == 2) {

                flag = 1;

                break;

            }

        }

        if (flag)

            printf("%d\n", ans - 1);

        else

            printf("-1\n");

    }



    return 0;

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

#include<stack>

#include <algorithm>

#include <cstdio>



using namespace std;



int main(){

    int n;

    int ans[100005];

    cin>>n;

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

        cin>>ans[i];

    }

    int l = 0;

    int k = 1;

    while(ans[k]!=2&&l<=n+1){

        k = ans[k];

        l++;

    }

    if (l == n+2){

        cout<<-1<<endl;

    }

    else{

        cout<<l+1<<endl;

    }

}


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

const int maxn=1e5+6;

int a[maxn],b[maxn],c[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

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

	{

		a[i]=i;

	}

	int k=1,sum=1,flag=0;

	c[1]=1;

	while(n--)

	{

		c[b[k]]=1;

		k=b[a[k]];

		sum++;

		if(k==2)

		{

			printf("%d",sum-1);

			flag=1;

			break;

		}

	}

	if(flag==0) printf("-1");

	return 0;

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

#include<cstdio>

#define mod 1000000007

using namespace std;

int main() {

	int n;

	int a[100500];

	scanf("%d",&n);

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

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

	}

	int cnt=1;

	bool b[100500]= {0};

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

		if(a[i]!=2) {

			if(b[i]==1) {

				printf("-1");

				break;

			} else {

				cnt++;

				b[i]=1;

				i=a[i];

			}

		}else{

			printf("%d",cnt);

			break;

		}

	}

	return 0;

}

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

const int maxn=1e5+6;

int a[maxn],b[maxn],c[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

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

	{

		a[i]=i;

	}

	int k=1,sum=1,flag=0;

	c[1]=1;

	while(n--)

	{

		c[b[k]]=1;

		k=b[a[k]];

		sum++;

		if(k==2)

		{

			printf("%d",sum-1);

			flag=1;

			break;

		}

	}

	if(flag==0) printf("-1");

	return 0;

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

#include <cstdio>

#include <cmath>

#include <string>

#include <cstring>

#include <algorithm>

#include <iomanip>

#include <cstdlib>

#include <cctype>

#define ll long long

#define PI 3.14159265358979323846

using namespace std;

const int maxn = 1e6+7;



int main(){

	int n;

	cin>>n;

	int *a=new int[n];

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

		cin>>a[i];

	}

	int j=0;

	int num=0;

	int flag=0;

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

		if(a[j]==2){

			flag=1;

			break;

		}

		num++;

		j=a[j]-1;

	}

	if(flag){

		cout<<num+1<<endl;

	}

	else{

		cout<<-1<<endl;

	}

	return 0;

}


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

const int maxn=1e5+6;

int a[maxn],b[maxn],c[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

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

	{

		a[i]=i;

	}

	int k=1,sum=1,flag=0;

	c[1]=1;

	while(n--)

	{

		c[b[k]]=1;

		k=b[a[k]];

		sum++;

		if(k==2)

		{

			printf("%d",sum-1);

			flag=1;

			break;

		}

	}

	if(flag==0) printf("-1");

	return 0;

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

#define N 100005

using namespace std;

int a[N];

int main()

{

    int n;

    while (~scanf("%d", &n)) {

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

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

        int x = 1,ans = 1, flag = 0;

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

            x = a[x];

            ans++;

            if (x == 2) {

                flag = 1;

                break;

            }

        }

        if (flag)

            printf("%d\n", ans - 1);

        else

            printf("-1\n");

    }



    return 0;

}


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

const int maxn=1e5+6;

int a[maxn],b[maxn],c[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

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

	{

		a[i]=i;

	}

	int k=1,sum=1,flag=0;

	c[1]=1;

	while(n--)

	{

		c[b[k]]=1;

		k=b[a[k]];

		sum++;

		if(k==2)

		{

			printf("%d",sum-1);

			flag=1;

			break;

		}

	}

	if(flag==0) printf("-1");

	return 0;

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

#include<stack>

#include <algorithm>

#include <cstdio>



using namespace std;



int main(){

    int n;

    int ans[100005];

    cin>>n;

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

        cin>>ans[i];

    }

    int l = 0;

    int k = 1;

    while(ans[k]!=2&&l<=n+1){

        k = ans[k];

        l++;

    }

    if (l == n+2){

        cout<<-1<<endl;

    }

    else{

        cout<<l+1<<endl;

    }

}


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

const int maxn=1e5+6;

int a[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

	int x=1,cnt=0;

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

	{

		x=a[x];

		cnt++;

		if(x==2)break;

	}

	if(x==2)printf("%d\n",cnt);

	else printf("-1\n");

	return 0;

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

#include<cstdio>

#define mod 1000000007

using namespace std;

int main() {

	int n;

	int a[100500];

	scanf("%d",&n);

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

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

	}

	int cnt=1;

	bool b[100500]= {0};

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

		if(a[i]!=2) {

			if(b[i]==1) {

				printf("-1");

				break;

			} else {

				cnt++;

				b[i]=1;

				i=a[i];

			}

		}else{

			printf("%d",cnt);

			break;

		}

	}

	return 0;

}

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

const int maxn=1e5+6;

int a[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

	int x=1,cnt=0;

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

	{

		x=a[x];

		cnt++;

		if(x==2)break;

	}

	if(x==2)printf("%d\n",cnt);

	else printf("-1\n");

	return 0;

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

#include <cstdio>

#include <cmath>

#include <string>

#include <cstring>

#include <algorithm>

#include <iomanip>

#include <cstdlib>

#include <cctype>

#define ll long long

#define PI 3.14159265358979323846

using namespace std;

const int maxn = 1e6+7;



int main(){

	int n;

	cin>>n;

	int *a=new int[n];

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

		cin>>a[i];

	}

	int j=0;

	int num=0;

	int flag=0;

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

		if(a[j]==2){

			flag=1;

			break;

		}

		num++;

		j=a[j]-1;

	}

	if(flag){

		cout<<num+1<<endl;

	}

	else{

		cout<<-1<<endl;

	}

	return 0;

}


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

const int maxn=1e5+6;

int a[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

	int x=1,cnt=0;

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

	{

		x=a[x];

		cnt++;

		if(x==2)break;

	}

	if(x==2)printf("%d\n",cnt);

	else printf("-1\n");

	return 0;

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

#define N 100005

using namespace std;

int a[N];

int main()

{

    int n;

    while (~scanf("%d", &n)) {

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

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

        int x = 1,ans = 1, flag = 0;

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

            x = a[x];

            ans++;

            if (x == 2) {

                flag = 1;

                break;

            }

        }

        if (flag)

            printf("%d\n", ans - 1);

        else

            printf("-1\n");

    }



    return 0;

}


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

const int maxn=1e5+6;

int a[maxn];

int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

	int x=1,cnt=0;

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

	{

		x=a[x];

		cnt++;

		if(x==2)break;

	}

	if(x==2)printf("%d\n",cnt);

	else printf("-1\n");

	return 0;

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

using namespace std;



int main()

{

    int a[100003] = {0}, n = 0, i = 0, x = 0, y = 1;

    cin >> n;

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

    {

        cin >> a[i];

    }

    i = 0;

    while(a[i] != 2)

    {

        if(y == 1)

        {

            i = 1;

        }

         if(a[i] == 2)

        {

            break;

        }

         i = a[i];



        y++;

         if(y >= n)

         {

             cout << -1;

             x++;

             break;

         }

    }

    if(x == 0)

    {

        cout << y;

    }





return 0;

}

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

using namespace std;

int main()

{

	int n;

	cin>>n;

	int a[n+1];

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

	{

		cin>>a[i];

	}

	int p=a[1],ans=0;

	int temp;

	while(ans<=n)

	{

		if(p==2)

		{

			break;

		}

		temp=p;

		p=a[temp];

		ans++;

	}

	if(ans>n)

	{

		cout<<"-1";

		return 0;

	}

	cout<<ans+1;

	return 0;

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

using namespace std;



int main()

{

    int a[100003] = {0}, n = 0, i = 0, x = 0, y = 1;

    cin >> n;

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

    {

        cin >> a[i];

    }

    i = 0;

    while(a[i] != 2)

    {

        if(y == 1)

        {

            i = 1;

        }

         if(a[i] == 2)

        {

            break;

        }

         i = a[i];



        y++;

         if(y >= n)

         {

             cout << -1;

             x++;

             break;

         }

    }

    if(x == 0)

    {

        cout << y;

    }





return 0;

}

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

#include <string>

using namespace std;



int main(){

    int n,i;

    cin >> n;

    int button[n+1];

    button[0] = 0;

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

        cin >> button[i];

    }

    int bright = 1;

    int times = 0;

    int f = 0;

    while (times <= n){

        times++;

        bright = button[bright];

        if (bright == 2){

            f++;

            cout << times;

            break;

        }

    }

    if (f == 0){

        cout << -1;

    }

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

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

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

#include <algorithm>

#include <cmath>

#include <vector>

#include <string>

#include <iomanip>

//#include <string.h>

#include <bits/stdc++.h>

#define INF 999999999

using namespace std;

#define ll long long

#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int vis[1000000]={0};

int hh[1000000];

int main()

{

    boost;

    int n;

    cin >>n;

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

        cin >>hh[i];

    }

    int j=1,sum=0,falg=0,p=1;

    vis[1]=1;

    while(1){

        if(vis[2]==1)

            break;

        else if(vis[j]==2){

            cout<<-1<<endl;

            falg=1;

            break;

        }

        else {

            j=hh[j];

            //cout<<j<<endl;

            vis[j]++;

            //cout<<" "<<vis[j]<<endl;

            sum++;

        }

    }

    if(!falg)

        cout<<sum<<endl;

    return 0;



}