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

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    }

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    }

} Main public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    } main {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    } BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); input new BufferedReader(new InputStreamReader(System.in)) int n  = Integer.parseInt(input.readLine()); n Integer.parseInt(input.readLine()) Integer.parseInt Integer input.readLine() input.readLine input int [] positions = new int[n + 1]; positions new int[n + 1] n + 1 n 1 int currentIndex = 1; currentIndex 1 for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
            positions[i] = Integer.parseInt(input.readLine());
        } {
            positions[i] = Integer.parseInt(input.readLine());
        } positions[i] = Integer.parseInt(input.readLine()) positions[i] positions i Integer.parseInt(input.readLine()) Integer.parseInt Integer input.readLine() input.readLine input int count = 0; count 0 while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        } currentIndex != 2 && positions[currentIndex] > 0 currentIndex != 2 currentIndex 2 positions[currentIndex] > 0 positions[currentIndex] positions currentIndex 0 {
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        } int copyCurrentIndex = currentIndex; copyCurrentIndex currentIndex currentIndex = positions[currentIndex] currentIndex positions[currentIndex] positions currentIndex positions[copyCurrentIndex] = -1 positions[copyCurrentIndex] positions copyCurrentIndex -1 1 count++ count System.out.println(currentIndex == 2 ? count: -1) System.out.println System.out System System.out currentIndex == 2 ? count: -1 currentIndex == 2 currentIndex 2 count -1 1 String[] args args public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    }

} public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    }

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int n  = Integer.parseInt(input.readLine());
        int [] positions = new int[n + 1];
        int currentIndex = 1;
        for (int i = 1; i <= n; i++) {
            positions[i] = Integer.parseInt(input.readLine());
        }
        int count = 0;
        while (currentIndex != 2 && positions[currentIndex] > 0){
            int copyCurrentIndex = currentIndex;
            currentIndex = positions[currentIndex];
            positions[copyCurrentIndex] = -1;
            count++;
        }
        System.out.println(currentIndex == 2 ? count: -1);
    }

}
