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

public class Main {
    public static void main(String[] args) throws Exception {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args) throws Exception {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    }
} Main public static void main(String[] args) throws Exception {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    } main {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int[] data = new int[n+1]; data new int[n+1] n+1 n 1 for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        } int i = 1; int i = 1; i 1 i < n+1 i n+1 n 1 i ++ i ++ i {
            data[i] = Integer.parseInt(br.readLine());
        } {
            data[i] = Integer.parseInt(br.readLine());
        } data[i] = Integer.parseInt(br.readLine()) data[i] data i Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int[] light = new int[n+1]; light new int[n+1] n+1 n 1 int[] pushed = new int[n+1]; pushed new int[n+1] n+1 n 1 light[1] = 1 light[1] light 1 1 pushed[1] = 0 pushed[1] pushed 1 0 for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        } int i = 2; int i = 2; i 2 i <= n i n i++ i++ i {
            light[i] = 0;
            pushed[i] = 0;
        } {
            light[i] = 0;
            pushed[i] = 0;
        } light[i] = 0 light[i] light i 0 pushed[i] = 0 pushed[i] pushed i 0 int count = 0; count 0 boolean achieve = false; achieve false int next = 1; next 1 while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        } true {
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        } boolean breakFlag = false; breakFlag false for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            } int i = next; int i = next; i next i <= n i n i++ i++ i {
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            } {
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            } if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                } light[i] == 1 light[i] light i 1 {
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                } light[i] = 0 light[i] light i 0 light[data[i]] = 1 light[data[i]] light data[i] data i 1 next = data[i] next data[i] data i pushed[i] = 1 pushed[i] pushed i 1 count++ count if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    } data[i] == 2 data[i] data i 2 {
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    } achieve = true achieve true breakFlag = true breakFlag true if(pushed[data[i]] == 1){
                        breakFlag = true;
                    } pushed[data[i]] == 1 pushed[data[i]] pushed data[i] data i 1 {
                        breakFlag = true;
                    } breakFlag = true breakFlag true break; if(breakFlag){
                break;
            } breakFlag {
                break;
            } break; if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        } achieve {
            System.out.println(count);
        } System.out.println(count) System.out.println System.out System System.out count {
            System.out.println(-1);
        } System.out.println(-1) System.out.println System.out System System.out -1 1 String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        // Here your code !
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        int[] data = new int[n+1];
        for(int i = 1; i < n+1; i ++){
            data[i] = Integer.parseInt(br.readLine());
        }
        
        int[] light = new int[n+1];
        int[] pushed = new int[n+1];
        light[1] = 1;
        pushed[1] = 0;
        for(int i = 2; i <= n ; i++){
            light[i] = 0;
            pushed[i] = 0;
        }
        
        int count = 0;
        boolean achieve = false;
        //光っているボタンを探す
        int next = 1;
        while(true){
            boolean breakFlag = false;
            
            
            //System.out.println("count = "+count);
            for(int i = next; i <= n; i++){
                //System.out.println("i = "+i);
                if(light[i] == 1){
                    //System.out.println(i + " was pushed next is "+data[i]);
                    light[i] = 0;
                    light[data[i]] = 1;
                    next = data[i];
                    pushed[i] = 1;
                    count++;
                    
                    if(data[i] == 2){
                        //System.out.println("clear");
                        achieve = true;
                        breakFlag = true;
                    }
                    
                    else if(pushed[data[i]] == 1){
                        breakFlag = true;
                    }
                    
                    break;
                }
                /*
                else if(light[i] > 1 && count > 1){
                    //System.out.println("i = "+i+" failed");
                    breakFlag = true;
                }*/
            }
            
            //System.out.println(breakFlag);
            if(breakFlag){
                break;
            }
        }
        
        if(achieve){
            System.out.println(count);
        }
        else {
            System.out.println(-1);
        }
        
    }
}
