import Foundation

let n = Int(readLine()!)!
var ai: [Int] = []
var ans = 0
var index = 0
for _ in 0..<n {
  ai.append(Int(readLine()!)!)
}
while true {
  ans += 1
  if ans > n || ai[index] == 2 {
    break
  }
  index = ai[index] - 1
}
print(ans > n ? -1 : ans)