#include<bits/stdc++.h>
using namespace std;
int main(){int n;cin>>n;vector<int>a(n);vector<int>b(n,0);for(int i=0;i<n;i++)cin>>a.at(i);
           int x=0;for(int i=1;i<=n;i++){x=a.at(x)-1;if(x==1){cout<<i<<endl;break;}if(b.at(x)){cout<<-1<<endl;break;}b.at(x)=1;}}