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

public class Main {

  public static void main(String[] args) {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -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) {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -1);
  }
} Main public static void main(String[] args) {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -1);
  } main {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -1);
  } int n = 0; n 0 int[] nums = null; nums null try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    } IOException e {
      e.printStackTrace();
      System.exit(1);
    } e.printStackTrace() e.printStackTrace e System.exit(1) System.exit System 1 {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) new BufferedReader(new InputStreamReader(System.in)) n = Integer.parseInt(br.readLine()) n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br nums = new int[n] nums new int[n] n for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      } int i = 0; int i = 0; i 0 i < nums.length i nums.length nums nums.length i++ i++ i {
        nums[i] = Integer.parseInt(br.readLine());
      } {
        nums[i] = Integer.parseInt(br.readLine());
      } nums[i] = Integer.parseInt(br.readLine()) nums[i] nums i Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br boolean b = false; b false int idx = 0; idx 0 int sum = 0; sum 0 for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    } int i = 0; int i = 0; i 0 i <= nums.length - 1 i nums.length - 1 nums.length nums nums.length 1 i++ i++ i {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    } {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    } sum++ sum if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      } nums[idx] == 2 nums[idx] nums idx 2 {
        b = true;
        break;
      } b = true b true break; {
        idx = nums[idx]-1;
      } idx = nums[idx]-1 idx nums[idx]-1 nums[idx] nums idx 1 System.out.print(b == true ? sum : -1) System.out.print System.out System System.out b == true ? sum : -1 b == true b true sum -1 1 String[] args args public class Main {

  public static void main(String[] args) {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -1);
  }
} public class Main {

  public static void main(String[] args) {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -1);
  }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

  public static void main(String[] args) {

    int n = 0;
    int[] nums = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
      n = Integer.parseInt(br.readLine());
      nums = new int[n];

      for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    boolean b = false;
    int idx = 0;
    int sum = 0;
    for (int i = 0; i <= nums.length - 1; i++) {
      sum++;
      if (nums[idx] == 2) {
        b = true;
        break;
      }
      else {
        idx = nums[idx]-1;
      }
    }

    System.out.print(b == true ? sum : -1);
  }
}
