import java.util.Scanner;

/**
 * Created by suzuki on 17/06/24.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int count = 1; count 1 int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] a = new int[N]; a new int[N] N int[] check = new int[N]; check new int[N] N for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<a.length i a.length a a.length 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 index = 0; index 0 boolean loopFlag = false; loopFlag false while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        } a[index] != 2 a[index] a index 2 {
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        } index = a[index] - 1 index a[index] - 1 a[index] a index 1 if(check[index] == 1){
                loopFlag = true;
                break;
            } check[index] == 1 check[index] check index 1 {
                loopFlag = true;
                break;
            } loopFlag = true loopFlag true break; check[index] = 1 check[index] check index 1 count++ count if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        } loopFlag {
            System.out.println(-1);
        } System.out.println(-1) System.out.println System.out System System.out -1 1 {
            System.out.println(count);
        } System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    }
} Main
import java.util.Scanner;

/**
 * Created by suzuki on 17/06/24.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        int N = sc.nextInt();
        int[] a = new int[N];
        int[] check = new int[N];
        for(int i=0;i<a.length;i++){
            a[i] = sc.nextInt();
        }
        int index = 0;
        boolean loopFlag = false;
        while (a[index] != 2){
            index = a[index] - 1;
            if(check[index] == 1){
                loopFlag = true;
                break;
            }
            check[index] = 1;
            count++;
        }
        if(loopFlag){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    }
}