    import java.util.Scanner;
    
    class Main {
     
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int N = sc.nextInt();
            boolean[] n = new boolean[N+1];
            boolean isEnd = false;
            for(int i = 1; i <=N; i++) {
            	n[i] = false;
            }
            int[] a = new int[N+1];
            for(int i = 1; i <= N; i++) {
            	a[i] = sc.nextInt();
            }
            sc.close();
            
            int count = 0;
            int i = 1;
            
            while(true) {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            }
	         System.out.println(count);
;        }
    } import java.util.Scanner; Scanner java.util class Main {
     
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int N = sc.nextInt();
            boolean[] n = new boolean[N+1];
            boolean isEnd = false;
            for(int i = 1; i <=N; i++) {
            	n[i] = false;
            }
            int[] a = new int[N+1];
            for(int i = 1; i <= N; i++) {
            	a[i] = sc.nextInt();
            }
            sc.close();
            
            int count = 0;
            int i = 1;
            
            while(true) {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            }
	         System.out.println(count);
;        }
    } Main public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int N = sc.nextInt();
            boolean[] n = new boolean[N+1];
            boolean isEnd = false;
            for(int i = 1; i <=N; i++) {
            	n[i] = false;
            }
            int[] a = new int[N+1];
            for(int i = 1; i <= N; i++) {
            	a[i] = sc.nextInt();
            }
            sc.close();
            
            int count = 0;
            int i = 1;
            
            while(true) {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            }
	         System.out.println(count);
;        } main {
            Scanner sc = new Scanner(System.in);
            int N = sc.nextInt();
            boolean[] n = new boolean[N+1];
            boolean isEnd = false;
            for(int i = 1; i <=N; i++) {
            	n[i] = false;
            }
            int[] a = new int[N+1];
            for(int i = 1; i <= N; i++) {
            	a[i] = sc.nextInt();
            }
            sc.close();
            
            int count = 0;
            int i = 1;
            
            while(true) {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            }
	         System.out.println(count);
;        } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc boolean[] n = new boolean[N+1]; n new boolean[N+1] N+1 N 1 boolean isEnd = false; isEnd false for(int i = 1; i <=N; i++) {
            	n[i] = false;
            } int i = 1; int i = 1; i 1 i <=N i N i++ i++ i {
            	n[i] = false;
            } {
            	n[i] = false;
            } n[i] = false n[i] n i false int[] a = new int[N+1]; a new int[N+1] N+1 N 1 for(int i = 1; i <= N; i++) {
            	a[i] = sc.nextInt();
            } int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
            	a[i] = sc.nextInt();
            } {
            	a[i] = sc.nextInt();
            } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc sc.close() sc.close sc int count = 0; count 0 int i = 1; i 1 while(true) {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            } true {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            } if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	} n[i] == false n[i] n i false {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			} n[i] = true n[i] n i true count ++ count if( a[i] == 2 ) {
            			break;
            		} a[i] == 2 a[i] a i 2 {
            			break;
            		} break; i = a[i] i a[i] a i {
     				count = -1;
     				break;
             	} count = -1 count -1 1 break; System.out.println(count) System.out.println System.out System System.out count String[] args args
    import java.util.Scanner;
    
    class Main {
     
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int N = sc.nextInt();
            boolean[] n = new boolean[N+1];
            boolean isEnd = false;
            for(int i = 1; i <=N; i++) {
            	n[i] = false;
            }
            int[] a = new int[N+1];
            for(int i = 1; i <= N; i++) {
            	a[i] = sc.nextInt();
            }
            sc.close();
            
            int count = 0;
            int i = 1;
            
            while(true) {
            	if( n[i] == false ) {
            		n[i] = true;
            		count ++;
            		if( a[i] == 2 ) {
            			break;
            		}
            		i = a[i];
     			}else {
     				count = -1;
     				break;
             	}
            }
	         System.out.println(count);
;        }
    }