func readInt() -> Int {
    return Int(readLine()!)!
}

func mainB() {
    let n = readInt()
    let bottoms = (0..<n).map { _ in readInt() }

    var flashBotton = 1
    var count = 0
    while true {
        count += 1
        flashBotton = bottoms[flashBotton - 1]
        if flashBotton == 2 {
            print(count)
            return
        }

        if count > n * n * n {
            print(-1)
            return
        }
    }
}

mainB()