import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  }

} import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  }

} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  } main {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  } 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+1; i++) {
      a[i] = sc.nextInt();
    } int i =1; int i =1; i 1 i < N+1 i N+1 N 1 i++ i++ i {
      a[i] = sc.nextInt();
    } {
      a[i] = sc.nextInt();
    } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc boolean b = true; b true int sq = 0; sq 0 int cr = 1; cr 1 while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    } b {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    } sq++ sq cr = a[cr] cr a[cr] a cr if (cr == 2) b = false; cr == 2 cr 2 b = false b false if (sq == N) {
        sq = 0;
        b = false;
      } sq == N sq N {
        sq = 0;
        b = false;
      } sq = 0 sq 0 b = false b false System.out.println(sq == 0 ? -1 : sq) System.out.println System.out System System.out sq == 0 ? -1 : sq sq == 0 sq 0 -1 1 sq String[] args args public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  }

} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  }

} Main
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    int[] a = new int[N + 1];

    for (int i =1; i < N+1; i++) {
      a[i] = sc.nextInt();
    }

    boolean b = true;
    int sq = 0;
    int cr = 1;

    while (b) {
      sq++;
      cr = a[cr];

      if (cr == 2) b = false;
      if (sq == N) {
        sq = 0;
        b = false;
      }
    }

    System.out.println(sq == 0 ? -1 : sq);

  }

}
