import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    } main {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int[] buttons = new int[n+1]; buttons new int[n+1] n+1 n 1 int[] selected = new int[n+1]; selected new int[n+1] n+1 n 1 for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        } {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        } buttons[i] = Integer.parseInt(br.readLine()) buttons[i] buttons i Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br selected[i] = 0 selected[i] selected i 0 int ans = 0; ans 0 boolean possible = false; possible false int index = 1; index 1 while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        } selected[index] == 0 selected[index] selected index 0 {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        } ans++ ans if (buttons[index] == 2) {
                possible = true;
                break;
            } buttons[index] == 2 buttons[index] buttons index 2 {
                possible = true;
                break;
            } possible = true possible true break; selected[index] = 1 selected[index] selected index 1 index = buttons[index] index buttons[index] buttons index System.out.println(possible?ans:-1) System.out.println System.out System System.out possible?ans:-1 possible ans -1 1 String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] buttons = new int[n+1];
        int[] selected = new int[n+1];
        
        for (int i = 1; i <= n ;i++) {
            buttons[i] = Integer.parseInt(br.readLine());
            selected[i] = 0;
        }
        
        int ans = 0;
        boolean possible = false;
        int index = 1;
        
        
        
        while (selected[index] == 0) {
            ans++;
            if (buttons[index] == 2) {
                possible = true;
                break;
            }
            selected[index] = 1;
            index = buttons[index];
        }
        System.out.println(possible?ans:-1);
    }
}
