import java.util.Scanner;

/**
 * Created by dizzyvn on 6/24/17.
 */
public class Main {
    public static void main(String[] args){
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args){
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    }
} Main public static void main(String[] args){
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    } main {
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt 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] = 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 int count = 0; count 0 boolean[] flag = new boolean[n+1]; flag new boolean[n+1] n+1 n 1 int pos = 1; pos 1 while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        } pos != 2 && !flag[pos] pos != 2 pos 2 !flag[pos] flag[pos] flag pos {
            flag[pos] = true;
            pos = a[pos];
            count++;
        } flag[pos] = true flag[pos] flag pos true pos = a[pos] pos a[pos] a pos count++ count if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        } pos == 2 pos 2 {
            System.out.println(count);
        } System.out.println(count) System.out.println System.out System System.out count {
            System.out.println(-1);
        } System.out.println(-1) System.out.println System.out System System.out -1 1 String[] args args public class Main {
    public static void main(String[] args){
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    }
} public class Main {
    public static void main(String[] args){
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    }
} Main
import java.util.Scanner;

/**
 * Created by dizzyvn on 6/24/17.
 */
public class Main {
    public static void main(String[] args){
        // input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        // process
        int count = 0;
        boolean[] flag = new boolean[n+1];

        int pos = 1;

        while (pos != 2 && !flag[pos]) {
            flag[pos] = true;
            pos = a[pos];
            count++;
        }

        // output
        if (pos == 2) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    }
}