#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100001

int seq[MAX_SIZE];

int main(){
	int n;
	while(scanf_s("%d",&n)!=-1){

		memset(seq,0,sizeof(seq));
		for(int i=1;i<=n;i++){
			scanf_s("%d",&seq[i]);
		}

		int step=1;
		int num=seq[1];
		int seq_n=1;
		do{
			seq_n=num;
			step++;
			if(seq[seq_n]==2){
				printf("%d\n",step);
				break;
			}
			else{
				if(seq_n==num){
					printf("-1\n");
					break;
				}
				else{
					num=seq[seq_n];
				}
			}
		}while(seq[num]!=num);
	}
	return 0;
}