Option Infer On
Imports System.Collections.Generic
Imports System.Console

Module Program
    Sub Main()
        Dim N As Integer = NextInt()
        Dim a As Integer() = New Integer(N - 1) {}

        For i = 0 To N - 1
            a(i) = NextInt()
        Next

        Dim j = 1
        Dim dic As Dictionary(Of Integer, Boolean) = New Dictionary(Of Integer, Boolean)

        For i = 1 To N
            dic(j) = True
            j = a(j - 1)
            If dic.ContainsKey(j) Then
                Exit For
            ElseIf j = 2 Then
                WriteLine(i)
                Exit Sub
            End If
        Next

        WriteLine(-1)
    End Sub

    Function NextInt() As Integer
        Return CInt(NextString())
    End Function

    Function NextString() As String
        Dim result As New List(Of Char)
        Do
            Dim tmp = Read()
            If tmp < 0 Then Exit Do
            Dim nextChar = Chr(tmp)

            If Not Char.IsWhiteSpace(nextChar) Then
                result.Add(nextChar)
            ElseIf nextChar <> vbCr Then
                Exit Do
            End If
        Loop
        Return New String(result.ToArray())
    End Function

End Module
