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

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.IOException; IOException java.io public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  }
} Main public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  } main {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(in.readLine()); n Integer.parseInt(in.readLine()) Integer.parseInt Integer in.readLine() in.readLine in int[] a = new int[n]; a new int[n] n for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      a[i] = Integer.parseInt(in.readLine());
    } {
      a[i] = Integer.parseInt(in.readLine());
    } a[i] = Integer.parseInt(in.readLine()) a[i] a i Integer.parseInt(in.readLine()) Integer.parseInt Integer in.readLine() in.readLine in int count = 0; count 0 int currentButton = a[0]; currentButton a[0] a 0 int[] ary = new int[n]; ary new int[n] n boolean hantei = false; hantei false for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    } int i=0; int i=0; i 0 i<n i n currentButton=a[currentButton-1] currentButton=a[currentButton-1] currentButton a[currentButton-1] a currentButton-1 currentButton 1 {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    } {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    } if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      } currentButton==2 currentButton 2 {
        count++;
        System.out.println(count);
        return;
      } count++ count System.out.println(count) System.out.println System.out System System.out count return; if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      } ary[currentButton-1]==2 ary[currentButton-1] ary currentButton-1 currentButton 1 2 {
        System.out.println(-1);
        return;
      } System.out.println(-1) System.out.println System.out System System.out -1 1 return; {
        count++;
        ary[currentButton-1]++;
      } count++ count ary[currentButton-1]++ ary[currentButton-1] ary currentButton-1 currentButton 1 String[] args args public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  }
} public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(in.readLine());
    int[] a = new int[n];
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(in.readLine());
    }
    
    int count = 0;
    int currentButton = a[0];
    int[] ary = new int[n];
    boolean hantei = false;
    for(int i=0; i<n; currentButton=a[currentButton-1]) {
      if(currentButton==2) {
        count++;
        System.out.println(count);
        return;
      } else if(ary[currentButton-1]==2){
        System.out.println(-1);
        return;
      } else {
        count++;
        ary[currentButton-1]++;
      }
    }
  }
}