import java.util.*;
class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	HashSet<Integer> set = new HashSet<Integer>();
	int N = Integer.parseInt(sc.next());
	int[] a = new int[N+1];
	for(int i = 1; i<=N; i++) {
	    a[i] = Integer.parseInt(sc.next());
	}
	int next = 1;
	int cnt = 0;
	for(int i = 0; i<N; i++) {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	}
	System.out.println(cnt);
    }
} import java.util.*; util.* java class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	HashSet<Integer> set = new HashSet<Integer>();
	int N = Integer.parseInt(sc.next());
	int[] a = new int[N+1];
	for(int i = 1; i<=N; i++) {
	    a[i] = Integer.parseInt(sc.next());
	}
	int next = 1;
	int cnt = 0;
	for(int i = 0; i<N; i++) {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	}
	System.out.println(cnt);
    }
} Main public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	HashSet<Integer> set = new HashSet<Integer>();
	int N = Integer.parseInt(sc.next());
	int[] a = new int[N+1];
	for(int i = 1; i<=N; i++) {
	    a[i] = Integer.parseInt(sc.next());
	}
	int next = 1;
	int cnt = 0;
	for(int i = 0; i<N; i++) {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	}
	System.out.println(cnt);
    } main {
	Scanner sc = new Scanner(System.in);
	HashSet<Integer> set = new HashSet<Integer>();
	int N = Integer.parseInt(sc.next());
	int[] a = new int[N+1];
	for(int i = 1; i<=N; i++) {
	    a[i] = Integer.parseInt(sc.next());
	}
	int next = 1;
	int cnt = 0;
	for(int i = 0; i<N; i++) {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	}
	System.out.println(cnt);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) HashSet<Integer> set = new HashSet<Integer>(); set new HashSet<Integer>() int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] a = new int[N+1]; a new int[N+1] N+1 N 1 for(int i = 1; i<=N; i++) {
	    a[i] = Integer.parseInt(sc.next());
	} int i = 1; int i = 1; i 1 i<=N i N i++ i++ i {
	    a[i] = Integer.parseInt(sc.next());
	} {
	    a[i] = Integer.parseInt(sc.next());
	} a[i] = Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int next = 1; next 1 int cnt = 0; cnt 0 for(int i = 0; i<N; i++) {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	} int i = 0; int i = 0; i 0 i<N i N i++ i++ i {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	} {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	} if(next == 2) {
		System.out.println(cnt);
		return;
	    } next == 2 next 2 {
		System.out.println(cnt);
		return;
	    } System.out.println(cnt) System.out.println System.out System System.out cnt return; if(set.contains(next)) {
		System.out.println(-1);
		return;
	    } set.contains(next) set.contains set next {
		System.out.println(-1);
		return;
	    } System.out.println(-1) System.out.println System.out System System.out -1 1 return; set.add(next) set.add set next next = a[next] next a[next] a next cnt++ cnt System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args
import java.util.*;
class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	HashSet<Integer> set = new HashSet<Integer>();
	int N = Integer.parseInt(sc.next());
	int[] a = new int[N+1];
	for(int i = 1; i<=N; i++) {
	    a[i] = Integer.parseInt(sc.next());
	}
	int next = 1;
	int cnt = 0;
	for(int i = 0; i<N; i++) {
	    if(next == 2) {
		System.out.println(cnt);
		return;
	    }
	    if(set.contains(next)) {
		System.out.println(-1);
		return;
	    }
	    set.add(next);
	    next = a[next];
	    cnt++;
	}
	System.out.println(cnt);
    }
}