import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
}
} import java.util.*; util.* java import static java.lang.System.*; import static java.lang.System.*; import static java.lang.System.*; import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
}
} Main public static void main(String[] args){
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
} main {
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
} Scanner sc = new Scanner(in); sc new Scanner(in) int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] a=new int[n+1]; a new int[n+1] n+1 n 1 int[] b=new int[n+1]; b new int[n+1] n+1 n 1 int c=0; c 0 for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
} int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
} {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
} a[i]=Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc if(a[i]!=2){c++;} a[i]!=2 a[i] a i 2 {c++;} c++ c if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
} c==n c n {
    out.println(-1);
} out.println(-1) out.println out -1 1 {
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
} int temp=1; temp 1 for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    } {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    } b[i]=a[temp] b[i] b i a[temp] a temp temp=b[i] temp b[i] b i if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        } b[i]==2 b[i] b i 2 {
            out.println(i);
            break;
        } out.println(i) out.println out i break; if(i==n){
            out.println(-1);
        } i==n i n {
            out.println(-1);
        } out.println(-1) out.println out -1 1 String[] args args public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
}
} public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
}
} Main
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
int n = Integer.parseInt(sc.next());
int[] a=new int[n+1];
int[] b=new int[n+1];
int c=0;    
for (int i = 1; i <= n; i++) {
    a[i]=Integer.parseInt(sc.next());
    if(a[i]!=2){c++;}
}
if(c==n){
    out.println(-1);
}else{
    int temp=1;
    for (int i = 1; i <= n; i++) {
        b[i]=a[temp];
        temp=b[i];
        if(b[i]==2){
            out.println(i);
            break;
        }else if(i==n){
            out.println(-1);
        }
    }
}
}
}