stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define ll long long

using namespace std;

ll fun(ll n, ll m)

{

    if((m/2)<n)return m/2;

    ll ans=n;

    m=m-n*2;

    ans=ans+m/4;

    return ans;

}  

int main()

{

    ll n,m;

    scanf("%lld%lld",&n,&m);

    ll ans=fun(n,m);

    printf("%lld\n",ans);

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio> 

#include <cmath> 

#include <cstdlib> 

#define For(i,j,k) for(int i=j;i<=k;i++)

#define Dow(i,j,k) for(int i=j;i>=k;i--)

#define LL long long

using namespace std ;

inline int read() {

	int x = 0 , f = 1 ;

	char ch = getchar() ;

	while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; }

	while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar() ; }

	return x * f ;

}



const int inf = 1e9; 

LL s,c;



int main() {

	scanf("%lld%lld",&s,&c);

	if( s*2>=c ) {

		printf("%lld\n",c/2);

		return 0;

	}

	c-=s*2;

	printf("%lld\n",s+c/4);

}












// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


#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;

}

// Optimized solution to the same problem (faster):
#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;

} 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

    

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

    }

}

// Optimized solution to the same problem (faster):
#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];

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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");

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

    }

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

} 

// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100003];

int main()

{

	long long b,c,d;

	scanf("%lld",&b);

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

	{

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

	} 

	c=a[1];

	if(c==2) printf("1");

	else {

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

	{

	d=a[c];

	if(d==2) 

	{

	printf("%d",i+1);

	break;

   }

	c=d;

	if(i==b-1){

     

	printf("-1");

	break;

	}       

}

}

	return 0;

}

// Fastest known solution for this problem: