import java.util.*;
public class Main{
  public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    int n = sc.nextInt();
    int ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  }
} 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 ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  }
} Main public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    int n = sc.nextInt();
    int ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  } main {
    Scanner sc = new Scanner(System.in);
    
    int n = sc.nextInt();
    int ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int ans = -1; ans -1 1 int[] list = new int[n]; list new int[n] n boolean flg = false; flg false for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    } int i =0; int i =0; i 0 i < n i n i++ i++ i {
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    } {
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    } list[i] = sc.nextInt() list[i] list i sc.nextInt() sc.nextInt sc if(list[i] == 2){
        flg = true;
      } list[i] == 2 list[i] list i 2 {
        flg = true;
      } flg = true flg true boolean[] flgList = new boolean[n]; flgList new boolean[n] n if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    } flg {
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    } int a = 0; a 0 int count = 0; count 0 do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]); !flgList[a] flgList[a] flgList a {
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      } flgList[a] = true flgList[a] flgList a true a = list[a] - 1 a list[a] - 1 list[a] list a 1 count++ count if (a == 1){
          ans = count;
          break;
        } a == 1 a 1 {
          ans = count;
          break;
        } ans = count ans count break; System.out.print(ans) System.out.print System.out System System.out ans String args[] args public class Main{
  public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    int n = sc.nextInt();
    int ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  }
} public class Main{
  public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    int n = sc.nextInt();
    int ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  }
} Main
import java.util.*;
public class Main{
  public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    int n = sc.nextInt();
    int ans = -1;

    
    int[] list = new int[n];
    boolean flg = false;
    for (int i =0; i < n; i++){
      list[i] = sc.nextInt();
      if(list[i] == 2){
        flg = true;
      }
    }
    
    boolean[] flgList = new boolean[n];
    
    if (flg){
      int a = 0;
      int count = 0;
      do{
        flgList[a] = true;
        a = list[a] - 1;
        count++;
          
        if (a == 1){
          ans = count;
          break;
        }
      }while (!flgList[a]);
      
    }
    
    System.out.print(ans);
    
  }
}