Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.
The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.
Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.
Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

-----Constraints-----
 - 2 ≤ N ≤ 10^5
 - 1 ≤ a_i ≤ N

-----Input-----
Input is given from Standard Input in the following format:
N
a_1
a_2
:
a_N

-----Output-----
Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

-----Sample Input-----
3
3
1
2

-----Sample Output-----
2

Press Button 1, then Button 3.
[{"code": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# FileName: \tsample\n# CreatedDate:  2020-10-10 20:29:20 +0900\n# LastModified: 2020-10-10 20:37:38 +0900\n#\n\n\nimport os\nimport sys\n# import numpy as np\n# import pandas as pd\n\n\ndef main():\n    N = int(input())\n    A = [0]\n    visited = [0]*(N+1)\n    for _ in range(N):\n        A.append(int(input()))\n    i = 1\n    cnt = 0\n    while visited[i] == 0:\n        if i == 2:\n            print(cnt)\n            return\n        cnt += 1\n        visited[i] = 1\n        i = A[i]\n    print((-1))\n\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.25, "memory": 14380.0, "status": "done"}, {"code": "n = int(input())\na = [int(input())-1 for _ in range(n)]\ncurr_node = 0\nfor i in range(n):\n    curr_node = a[curr_node]\n    if curr_node == 1:\n        print(i+1)\n        return\nprint(-1)", "passed": true, "time": 0.15, "memory": 14200.0, "status": "done"}, {"code": "N = int(input())\nl = [int(input())-1 for _ in range(0,N)]\n\ni = 0;\nflg = False\n\nfor n in range(0,N):\n  if l[i] == 1 :\n    print(n+1)\n    flg = True\n    break\n  else:\n    i = l[i]\n\n\nif not flg:\n  print(\"-1\")", "passed": true, "time": 0.16, "memory": 14188.0, "status": "done"}, {"code": "N = int(input())\nA = []\nfor i in range(N):\n  A.append(int(input())-1)\n    \nans = -1\npush = 0\ncount = [False]*N\n \nfor i in range(N+1):\n \n    if A[push] == 1:\n        ans = i+1\n        break\n        \n    elif count[push]:\n      break\n      \n    else:\n        count[push] = True\n        push = A[push]\n        \n      \nprint(ans)\n", "passed": true, "time": 0.16, "memory": 14016.0, "status": "done"}, {"code": "N = int(input())\nButtons = []\nfor i in range(N):\n  Buttons.append(int(input()))\npos = 0\ncount = 0\nvisit = [0]*N\nflag = 1\nwhile pos != 1:\n  if visit[pos]!=0:\n    flag = 0\n    break;\n  else:\n    visit[pos]=1\n    pos = Buttons[pos]-1\n    count = count+1\nif flag == 1:\n  print(count)\nelse :\n  print(-1)", "passed": true, "time": 0.23, "memory": 14096.0, "status": "done"}, {"code": "n = int(input())\na = []\nfor i in range(n):\n  a.append(int(input()))\n\nres = -1\ncc = 0\ncount = 0\nfor i in range(2*n):\n  cc = a[cc] - 1\n  count += 1\n  if cc == 1:\n    res = count\n    break\n\nprint(res)", "passed": true, "time": 0.25, "memory": 14276.0, "status": "done"}, {"code": "n = int(input())\na = [int(input())-1 for _ in range(n)]\n\nvisit_node = [0]*(n)\nans = 0\ncurr_node = 0\n\nwhile(visit_node[curr_node] == 0):\n    visit_node[curr_node] = 1\n    curr_node = a[curr_node]\n    ans += 1\n    if curr_node == 1:\n        print(ans)\n        return\nprint(-1)", "passed": true, "time": 0.25, "memory": 14224.0, "status": "done"}, {"code": "import sys\nN = int(input())\nA = [int(input()) for _ in range(N)]\nx = 0\ncnt = 0\nfor _ in range(N):\n    x = A[x] - 1\n    cnt += 1\n    if x == 1:\n        print(cnt)\n        return\nprint(-1)", "passed": true, "time": 0.23, "memory": 14024.0, "status": "done"}, {"code": "n = int(input())\nlight = [0] * (n+1)\n\nfor i in range(1,n+1):\n  light[i] = int(input())\ns = 1\nans = 0\nwhile light[s] != 2 and ans <= n:\n  s = light[s]\n  ans += 1\n\nif light[s] == 2:\n  print(ans + 1)\nelse:\n  print(-1)", "passed": true, "time": 0.17, "memory": 14304.0, "status": "done"}, {"code": "#!/usr/local/bin/python3\n# https://atcoder.jp/contests/abc065/tasks/abc065_b\n\nN = int(input())\na = [int(input())-1 for _ in range(N)]\nans = 0\ncur = 0\n\nvisit = [-1] * N\nwhile True:\n    # print(cur, visit)\n    cur = a[cur]\n    ans += 1\n    if visit[cur] == -1:\n        visit[cur] = ans\n    else:\n        print(-1)\n        return\n    if cur == 1:\n        print(ans)\n        return", "passed": true, "time": 0.14, "memory": 14116.0, "status": "done"}, {"code": "N=int(input())\na=[int(input())-1 for _ in range(N)]\nx=0\nans=0\nwhile True:\n    ans+=1\n    x=a[x]\n    if x==1:\n        print(ans)\n        break\n    if ans>N:\n        print((-1))\n        break;\n", "passed": true, "time": 0.16, "memory": 14284.0, "status": "done"}, {"code": "import sys\n\nn = int(input())\nA = list(map(int, sys.stdin.readlines()))\n\nindex = 0\npast_index = list()\nfound = False\n\nfor i in range(len(A)):\n    if index == 1:\n        print(i)\n        found = True\n        break\n    index = A[index]-1\n\nif not found:\n    print(-1)", "passed": true, "time": 0.16, "memory": 14128.0, "status": "done"}, {"code": "N = int(input())\nA = [int(input()) for i in range(N)]\ni = 0\ncnt = 1\ncmpr = [0]\nwhile A[i] != 2:\n    if A[i] == i + 1:\n        print(-1)\n        return\n    i = A[i] - 1\n    cmpr.append(i)\n    cnt += 1\n    if cnt > N:\n        print(-1)\n        return\nprint(cnt)", "passed": true, "time": 0.14, "memory": 14224.0, "status": "done"}, {"code": "n=int(input())\na=[int(input()) for i in range(n)]\ncnt=0\ni=1\nwhile i!=2:\n    i=a[i-1]\n    cnt+=1\n    if cnt>10**5:\n        cnt=-1\n        break\nprint(cnt)\n", "passed": true, "time": 0.26, "memory": 14160.0, "status": "done"}, {"code": "n=int(input())\na=[]\nfor i in range(n):\n    a.append(int(input()))\ntotal=1\ni=a[0]-1\nif a[0]==2:\n    print(total)\nelse:\n    while True:\n        if a[i]==2:\n            print(total+1)\n            break\n        elif total>n:\n            print(\"-1\")\n            break\n        else:\n            i=a[i]-1\n            total+=1", "passed": true, "time": 0.22, "memory": 14116.0, "status": "done"}, {"code": "N = int(input())\n\nlst = []\nfor i in range(N):\n   lst.append(int(input()))\n\nnow = 1\ncount = 0\n\nfor i in range(N):\n   now = lst[now - 1]\n   count += 1\n   if now == 2:\n      print(count)\n      return\n\nprint(-1)", "passed": true, "time": 0.24, "memory": 14088.0, "status": "done"}, {"code": "from collections import defaultdict\n\nn = int(input())\nA = [int(input()) for _ in range(n)]\n\ndic = defaultdict()\n\nfor i in range(n):\n    dic[i+1] = A[i]\n\ncnt, idx = 1, 0\nval = dic[1]\n\nwhile idx <= n:\n    if val == 2:\n        print(cnt)\n        return\n    val = dic[val]\n    cnt += 1\n    idx += 1\nprint((-1))\n", "passed": true, "time": 0.16, "memory": 14104.0, "status": "done"}, {"code": "n=int(input())\na=[]\nfor _ in range(n):\n    a.append(int(input()))\nnow=0\ncnt=0\nvisit=[0 for _ in range(n)]\nFlag=False\nwhile now != 1:\n    if visit[now] != 0:\n        Flag=True\n        break\n    else:\n        visit[now]=1\n        now=a[now]-1\n        cnt+=1\nif Flag:\n    print('-1')\nelse:\n    print(cnt)\n", "passed": true, "time": 0.24, "memory": 14240.0, "status": "done"}, {"code": "n = int(input())\nq = [int(input())  for i in range(n)]\ncnt = 0\nind = 0\nfor i in range(n):\n    ind = q[ind]-1\n    cnt += 1\n    if ind == 1:\n        print(cnt)\n        return\n    \nprint((-1))\n\n", "passed": true, "time": 0.21, "memory": 14024.0, "status": "done"}, {"code": "n = int(input())\na = []\nb = []\nfor i in range(n):\n    a.append(int(input()))\n\nnext = a[0] - 1\nb.append(next + 1)\nfor i in range(n):\n    next = a[next] - 1\n    b.append(next + 1)\nprint(b.index(2) + 1) if 2 in b else print(-1)", "passed": true, "time": 0.14, "memory": 14164.0, "status": "done"}, {"code": "# -*- coding:utf-8 -*-\n\nimport sys\nnyuryoku = [int(i) - 1 for i in sys.stdin]\n\nN = nyuryoku[0] + 1\na_list = nyuryoku[1:]\n\n\ncnt_push = 0\nbutton_list = [False] * N\ni = 0\n#cur_but_posi = a_list[0]\n\nwhile True:\n    if button_list[i] == False:\n        button_list[i] = True\n        cnt_push += 1\n        if a_list[i] == 1:\n            break\n        i = a_list[i]\n    else:\n        cnt_push = -1\n        break\n\nprint(cnt_push)\n\n\"\"\"\nimport sys\n\nl = [int(i) - 1 for i in sys.stdin]\n\nN = l[0] + 1\n\na = l[1:]\n\ndone = [False] * N\n\ni = 0\n\nans = 0\n\nwhile True:\n    if done[i] == False:\n        done[i] = True\n        ans += 1\n        if a[i] == 1:\n            break\n        i = a[i]\n    else:\n        ans = -1\n        break\n\nprint(ans)\n\"\"\"\n", "passed": true, "time": 0.14, "memory": 14264.0, "status": "done"}, {"code": "n=int(input())\na=[int(input()) for _ in range(n)]\nlight=0\nans=0\nfor i in range(n):\n    if a[light]==2:\n        ans+=1\n        break\n    else:\n        light=a[light]-1\n        ans+=1\nelse:\n    ans=-1\nprint(ans)", "passed": true, "time": 0.31, "memory": 14104.0, "status": "done"}, {"code": "N = int(input())\nA = []\nfor i in range(N):\n  A.append(int(input())-1)\n    \nans = -1\npush = 0\ncount = set()\n \nfor i in range(N+1):\n \n    if A[push] == 1:\n        ans = i+1\n        break\n        \n    elif A[push] in count:\n      break\n      \n    else:\n      count.add(push)\n      push = A[push]\n        \nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14352.0, "status": "done"}, {"code": "n = int(input())\ndone = [False for _ in range(n)]\nA = [int(input()) - 1 for _ in range(n)]\ntmp = 0\nfor i in range(n + 1):\n    if A[tmp] == 1:\n        print((i + 1))\n\n        break\n    if done[tmp]:\n        print((-1))\n        break\n    else:\n        done[tmp] = True\n    tmp = A[tmp]\nelse:\n    print((-1))\n", "passed": true, "time": 0.14, "memory": 14160.0, "status": "done"}, {"code": "N = int(input())\nA = [int(input()) for _ in range(N)]\nA = [a-1 for a in A]\n\nfin = set()\ncnt = 0\ni = 0\nwhile True:\n    if i in fin:\n        cnt = -1\n        break\n    if i == 1:\n        break\n    cnt += 1\n    fin.add(i)\n    i = A[i]\nprint(cnt)", "passed": true, "time": 0.14, "memory": 14128.0, "status": "done"}, {"code": "# B - Trained?\ndef main():\n    n = int(input())\n    a = [int(input()) for _ in range(n)]\n    cnt = 0\n    x = 1\n\n    if 2 in a:\n        for _ in range(n-1):\n            x = a[x-1]\n            cnt += 1\n\n            if x == 2:\n                print(cnt)\n                return\n        else:\n            print(-1)\n    else:\n        print(-1)\n        \n\nif __name__ ==  \"__main__\":\n    main()", "passed": true, "time": 0.14, "memory": 14100.0, "status": "done"}, {"code": "N = int(input())\nA = [0]*(N+1)\nfor i in range(1,N+1):\n    A[i] = int(input())\n\nis_visited_list = [0]*(N+1)\n\nloop = False\nnow_visit = 1\ncnt = 0\nwhile True:\n    if is_visited_list[now_visit] == 1:\n        loop = True\n        break\n    else:\n        is_visited_list[now_visit] = 1\n    \n    now_visit = A[now_visit]\n    if now_visit == 2:\n        cnt +=1\n        break\n    else:\n        cnt +=1\n\nif loop:\n    print(-1)\nelse:\n    print(cnt)", "passed": true, "time": 0.14, "memory": 14160.0, "status": "done"}, {"code": "N = int(input())\nA = [0] * N\nfor i in range(N):\n    A[i] = int(input()) - 1 # \u914d\u5217\u306b\u5bfe\u5fdc\nindex = 0\ntrials = -1\nfor i in range(N):\n    index = A[index]\n    if index == 1:\n        trials = i + 1\n        break\nprint(trials)\n", "passed": true, "time": 0.15, "memory": 14340.0, "status": "done"}, {"code": "N = int(input())\na = [int(input()) for _ in range(N)]\ncnt = 0\non = 0\nfor _ in range(N):\n    cnt += 1\n    if a[on] == 2:\n        print(cnt)\n        return\n    on = a[on]-1\nprint(-1)", "passed": true, "time": 0.14, "memory": 14340.0, "status": "done"}, {"code": "N = int(input())\na = [int(input()) for i in range(N)]\n\ncount = [0]*(N)\n\ni = 1\nmin_num = 0\ncount[i-1]+=1\nwhile True:\n    i = a[i-1]\n    min_num += 1\n    if i == 2:\n        print(min_num)\n        return\n    if count[i - 1] == 0:\n        count[i-1]+=1\n    else:\n        break\n\nprint(-1)", "passed": true, "time": 0.14, "memory": 14288.0, "status": "done"}, {"code": "n = int(input())\nq = [int(input())  for i in range(n)]\ncnt = 0\nind = 0\nfor i in range(n):\n    ind = q[ind]-1\n    cnt += 1\n    if ind == 1:\n        print(cnt)\n        return\n    \nprint(-1)", "passed": true, "time": 0.15, "memory": 14236.0, "status": "done"}, {"code": "import re\nimport sys\nimport math\nimport itertools\nimport bisect\nfrom copy import copy\nfrom collections import deque,Counter\nfrom decimal import Decimal\nimport functools\ndef v(): return input()\ndef k(): return int(input())\ndef S(): return input().split()\ndef I(): return map(int,input().split())\ndef X(): return list(input())\ndef L(): return list(input().split())\ndef l(): return list(map(int,input().split()))\ndef lcm(a,b): return a*b//math.gcd(a,b)\nsys.setrecursionlimit(10 ** 6)\nmod = 10**9+7\ncnt = 0\nans = 0\ninf = float(\"inf\")\nal = \"abcdefghijklmnopqrstuvwxyz\"\nAL = al.upper()\n\nN = k()\nA = []\nfor i in range(N):\n    a = k()\n    A.append(a)\n    \naaa = 0    \nans = A[0]\ncount = 1\n \nfor i in range(N):\n    if ans == 2:\n        aaa = 1\n        break\n    else:\n        ans = A[ans-1]\n        count += 1\nif aaa == 1:\n    print(count)\nelse:\n    print(-1)", "passed": true, "time": 0.15, "memory": 14160.0, "status": "done"}, {"code": "# N<=10^5\u306a\u306e\u3067\u611a\u76f4\u306b\u3084\u3063\u3066\u3082\u9593\u306b\u5408\u3046\nn = int(input())\na = [int(input()) - 1 for _ in range(n)]\nvisited = [False] * n\nnow = 0\ncount = 0\nwhile visited[now] != True:\n    count += 1\n    visited[now] = True\n    now = a[now]\n    if now == 1:\n        print(count)\n        return\nprint((-1))\n", "passed": true, "time": 0.16, "memory": 14012.0, "status": "done"}, {"code": "N = int(input())\na = []\n \nfor _ in range(N):\n    a.append(int(input()) - 1)\n\ni, count = 0, 0\nsuccess = False\nfor _ in range(N):\n    i = a[i]\n    count += 1\n\n    if i == 1:\n        success = True\n        break\n \nif not success:\n    count = -1\n \nprint(count)", "passed": true, "time": 0.15, "memory": 14188.0, "status": "done"}, {"code": "N = int(input())\nA = [int(input()) for _ in range(N)]\n\nA = [0] + A\ni = 1\nk = 1\nwhile k <= N:\n    if A[i] == 2:\n        print(k)\n        return\n    i = A[i]\n    k += 1\nprint((-1))\n", "passed": true, "time": 0.14, "memory": 14276.0, "status": "done"}, {"code": "N = int(input())\nnum_list = [int(input()) for _ in range(N)]\nnum = 0\nfor i in range(N):\n    if num_list[num] == 2:\n        print(i+1)\n        return\n    num = num_list[num]-1\nprint(-1)", "passed": true, "time": 0.14, "memory": 14180.0, "status": "done"}, {"code": "N=int(input())\na=[None]+[int(input()) for i in range(N)]\nc=1\n\nfor i in range(1, N+1):\n  if a[c]==2:\n    print(i)\n    return\n  else:\n    c=a[c]+0\n    \nprint(-1)", "passed": true, "time": 0.15, "memory": 14068.0, "status": "done"}, {"code": "n = int(input())\na = [0]*n\nfor i in range(n):\n    a[i] = int(input())\n\n\nans = -1\nmemo = 0\nlight = 1\n\nfor i in range(n):\n    if light == 2:\n        ans = memo\n        break\n    light = a[light-1]\n    memo += 1\n\n\nprint(ans)", "passed": true, "time": 0.2, "memory": 14376.0, "status": "done"}, {"code": "n = int(input())\na = [int(input()) for i in range(n)]\ncnt = 0\n\nif not a.count(2):\n    print('-1')\nelse:\n    i = 0\n    while True:\n        cnt += 1\n        if a[i] == 2:\n            print(cnt)\n            break\n        elif a[i] == i+1 or cnt > n:\n            print('-1')\n            break\n        i = a[i]-1", "passed": true, "time": 0.17, "memory": 14196.0, "status": "done"}, {"code": "import sys\ninput = lambda: sys.stdin.readline().rstrip() \nsys.setrecursionlimit(10**7)\nINF = 10**20\ndef I(): return int(input())\ndef F(): return float(input())\ndef S(): return input()\ndef LI(): return [int(x) for x in input().split()]\ndef LI_(): return [int(x)-1 for x in input().split()]\ndef LF(): return [float(x) for x in input().split()]\ndef LS(): return input().split()\n\ndef resolve():\n    N = I()\n    a = [I() - 1 for _ in range(N)]\n\n    c = 0\n    ans = 0\n    is_ok = True\n    while True:\n        c = a[c]\n        ans += 1\n        if c == 0 or ans > N:\n            is_ok = False\n            break\n        if c == 1:\n            break\n\n    if is_ok:\n        print(ans)\n    else:\n        print(-1)\n\ndef __starting_point():\n    resolve()\n__starting_point()", "passed": true, "time": 0.16, "memory": 14156.0, "status": "done"}, {"code": "#!/usr/local/bin/python3\n# https://atcoder.jp/contests/abc065/tasks/abc065_b\n\nN = int(input())\na = [int(input())-1 for _ in range(N)]\nans = 0\ncur = 0\n\nvisit = [-1] * N\nwhile True:\n    # print(cur, visit)\n    cur = a[cur]\n    ans += 1\n    if visit[cur] == -1:\n        visit[cur] = ans\n    else:\n        print((-1))\n        return\n    if cur == 1:\n        print(ans)\n        return\n", "passed": true, "time": 0.15, "memory": 14180.0, "status": "done"}, {"code": "n = int(input())\na = [0]\nfor i in range(n):\n    a.append(int(input()))\ncnt = 0\na_before = 1\nwhile cnt <= n:\n    cnt += 1\n    if a[a_before] == 2:\n        print(cnt)\n        break\n    a_before = a[a_before]\nelse:\n    print(-1)", "passed": true, "time": 0.15, "memory": 14180.0, "status": "done"}, {"code": "n = int(input())\nli = [0]\nfor i in range(n):\n    li.append(int(input()))\n\ncount = 0\nnow = 1\nwhile now != 2:\n    if li[now] == 0:\n        count = -1\n        break\n    else:\n        a = li[now]\n        li[now] = 0\n        now = a\n        count += 1\n\nprint(count)", "passed": true, "time": 0.15, "memory": 14152.0, "status": "done"}, {"code": "n=int(input())\na=[int(input())-1 for _ in range(n)]\nid=[-1]*n\nx=0\nfor c in range(1,n+1):\n    if id[x]!=-1:\n        print(-1)\n        return\n    id[x]=c\n    x=a[x]\n    if x==1:\n        print(c)\n        return", "passed": true, "time": 0.15, "memory": 14132.0, "status": "done"}, {"code": "n = int(input())\n\na = [int(input()) for _ in range(n)]\na.insert(0,0)\n\npos = 1\nfor i in range(1,n+1):\n  pos = a[pos]\n  if pos==2:\n    print(i)\n    break\nif pos!=2:\n  print((-1))\n", "passed": true, "time": 0.23, "memory": 14100.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, [input() for i in range(n)]))\nans = 1\nhikari = 1\nfor i in range(n):\n        hikari = a[hikari -1]\n        if hikari == 2:\n                print(ans)\n                return\n        else:\n                ans  += 1\nprint(-1)", "passed": true, "time": 0.14, "memory": 14344.0, "status": "done"}, {"code": "n = int(input())\na = []\nfor i in range(n):\n  a.append(int(input()))\n    \na.insert(0, 0)\nans = -1\npush = 1\n\nfor i in range(1, n+2):\n    \n    if a[push] == 2:\n        ans = i\n        break\n        \n    else:\n        push = a[push]\n      \nprint(ans)", "passed": true, "time": 0.23, "memory": 14264.0, "status": "done"}, {"code": "n = int(input())\nal = [int(input()) for i in range(n)]\n\ni = 0\nfor j in range(n):\n    i = al[i]-1\n    if i == 1:\n        print(j+1)\n        return\nprint(-1)", "passed": true, "time": 0.15, "memory": 14268.0, "status": "done"}, {"code": "n = int(input())\n#x, a, b = map(int, input().split())\n#l = list(map(int,input().split()))\n#l = [list(map(int,input().split())) for i in range(n)]\nal = []\nfor i in range(n):\n    a = int(input())\n    al.append(a)\n\nal.insert(0, 0)\nans = -1\npush = 1\nfor i in range(1, n+2):\n    light = al[push]\n    push = light\n    if light == 2:\n        ans = i\n        break\nprint(ans)\n", "passed": true, "time": 0.23, "memory": 14076.0, "status": "done"}, {"code": "n=int(input())\na=[int(input()) for i in range(n)]\nans=1\nres=1\nfor i in range(n):\n  if a[res-1]==2:\n    print(ans)\n    return\n  else:\n    ans+=1\n    res=a[res-1]\nprint(-1)", "passed": true, "time": 0.19, "memory": 14088.0, "status": "done"}, {"code": "\ndef resolve():\n    N = int(input())\n    A = [ int(input()) for _ in range(N)]\n\n    #print(N, A)\n\n    next = 0\n    cnt = 0\n    for i in range(N):\n        next = A[next] - 1\n        #print(next)\n        cnt += 1\n        if next == 1:\n            break\n\n    if cnt == N:\n        print(-1)\n    else:\n        print(cnt)\n\n\ndef __starting_point():\n    resolve()\n__starting_point()", "passed": true, "time": 0.14, "memory": 14140.0, "status": "done"}, {"code": "N = int(input())\nA = [0] + [int(input()) for _ in range(N)]\n\nj = 1\ncnt = 0\ncheck = False\n\nfor i in range(N):\n  if j == 2:\n    check= True\n    break\n  cnt += 1\n  j = A[j]\n  \nprint(cnt if check else -1)", "passed": true, "time": 0.15, "memory": 14176.0, "status": "done"}, {"code": "# -*- coding: utf-8 -*-\n\nN = int(input())\na = []\nfor i in range(N):\n    a.append(int(input()))\n\nans = pos = 0\nflag = [0] * N\nwhile True:\n    ans += 1\n    if a[pos] == 2:\n        break\n    #print(pos,a[pos]-1)\n    pos = a[pos] - 1\n\n    if flag[pos] > 0:\n        ans = -1\n        break\n    else:\n        flag[pos] = 1\n\nprint(ans)", "passed": true, "time": 0.16, "memory": 14372.0, "status": "done"}, {"code": "n = int(input())\nl = []\nfor i in range(n):\n    a = int(input())\n    l.append(a)\nx = l[0]\ncnt = 1\nfor i in range(n):\n    if x == 2:\n        print(cnt)\n        return\n    else:\n        cnt +=1\n        x = l[x-1]\nprint('-1')", "passed": true, "time": 0.14, "memory": 14196.0, "status": "done"}, {"code": "n = int(input())\nA = []\nbool = [True] * n\nfor i in range(n):\n    a = int(input())\n    a -= 1\n    A.append(a)\nj = A[0]\ncnt = 1\nfor i in range(n):\n    if j == 1:\n        print(cnt)\n        return\n    if bool[A[j]] == True:\n        bool[A[j]] = False        \n        j = A[j]\n        cnt += 1\n    else :\n        print((-1))\n        return\n\n\n", "passed": true, "time": 0.14, "memory": 14344.0, "status": "done"}, {"code": "n = int(input())\na = [int(input()) for _ in range(n)]\n# a = list(map(lambda x: int(x) - 1, a))\t#\u3059\u3079\u3066\u306e\u8981\u7d20\u304b\u3089-1\n\np = a[0]\n\nif p == 2:\n    print(1)\n    return\n    \ncnt = 1\n\nfor _ in range(n):\n    p = a[p-1]\n    cnt += 1\n    if p == 2:\n        print(cnt)\n        return\n\nprint(-1)", "passed": true, "time": 0.14, "memory": 14160.0, "status": "done"}, {"code": "import re\nimport copy\n\ndef accept_input():\n    N = int(input())\n    A = {}\n    for i in range(1,N+1):\n        A[i] = int(input())\n    return N,A\n\ndef process(s):\n    if s == \"#\":\n        return 1\n    else:\n        return 0\nN,A = accept_input()\ncur = 1\nkaisu = 0\nfor _ in range(N):\n    cur = A[cur]\n    kaisu += 1\n    if cur == 2:\n        break\nif cur == 2:\n    print(kaisu)\nelse:\n    print((-1))\n", "passed": true, "time": 0.15, "memory": 14088.0, "status": "done"}, {"code": "N = int(input())\nd = []\nvisited = [False for _ in range(N+1)]\nfor _ in range(N):\n  d.append(int(input())-1)\n\ni = 0\ncnt = 0\nwhile True:\n  if i == 1:\n    break\n  elif cnt >= N:\n    cnt = -1\n    break\n  elif visited[i]:\n    cnt = -1\n    break\n  else:\n    visited[i] = True\n    cnt += 1\n    i = d[i]\n  \nprint(cnt)", "passed": true, "time": 0.14, "memory": 14368.0, "status": "done"}, {"code": "n = int(input())\na = [0] * n\nfor i in range(n):\n    a[i] = int(input())\n\nvisit = set()\nnow = 1\nfor i in range(1, n + 10):\n    if now not in visit:\n        visit.add(now)\n        now = a[now - 1]\n    else:\n        print((-1))\n        break\n    if now == 2:\n        print(i)\n        break\n", "passed": true, "time": 0.14, "memory": 14324.0, "status": "done"}, {"code": "n = int(input())\nline = [0]\nfor i in range(n):\n  line.append(int(input()))\ncheck = [0 for i in range(n+1)]\ncount = 0\ncheck[1] = 1\na = 1\nwhile True:\n  count += 1\n  if line[a] == 2:\n    break\n  else:\n    a = line[a]\n    check[a] += 1\n  \n  if check[a] > 1:\n    count = -1\n    break\nprint(count)\n  \n", "passed": true, "time": 0.14, "memory": 14204.0, "status": "done"}, {"code": "N = int(input())\na = [0,]\nfor i in range(N):\n    a.append(int(input()))\nidx = 1\nfor i in range(1,N+1):\n    idx = a[idx]\n    if idx == 2:\n        print(i)\n        return\nprint(-1)", "passed": true, "time": 0.14, "memory": 14116.0, "status": "done"}, {"code": "N = int(input())\na = [int(input()) for _ in range(N)]\nlog = set()\ni = 0\ncount = 1\nwhile a[i] != 2:\n  i = a[i] - 1\n  if i in log:\n    print(-1)\n    break\n  else:\n    log.add(i)\n    count += 1\nelse:\n  print(count)", "passed": true, "time": 0.14, "memory": 14216.0, "status": "done"}, {"code": "N = int(input())\n\nl = []\nfor i in range(N):\n  v = int(input())\n  l.append(v)\n  \nv = l[0]\nans = 1\nwhile v != 2 and v != -1:\n  tmp = l[v-1]\n  l[v-1] = -1\n  v = tmp\n  ans += 1\n  \nif v == 2:\n  print(ans)\nelse: \n  print(-1)", "passed": true, "time": 0.15, "memory": 14280.0, "status": "done"}, {"code": "import sys\nN = int(input())\nA ={}\nfor i in range(1,N+1):\n    A[i] = int(input())\n#print(A)\nans = 1\nx = 1\nfor j in range(N):\n    if A[x] == 2:\n        print(ans)\n        return\n    x = A[x]\n    ans += 1\nprint(-1)", "passed": true, "time": 0.23, "memory": 14080.0, "status": "done"}, {"code": "N = int(input())\nA = []\nfor i in range(N):\n    a = int(input())\n    A.append(a)\nb = 1\nx = A[0]\nwhile x != 2:\n    x = A[x - 1]\n    if b > N:\n        print(-1)\n        return\n    b += 1\n\nprint(b)", "passed": true, "time": 0.16, "memory": 14084.0, "status": "done"}, {"code": "n = int(input())\nal = []\nfor i in range(n):\n    a = int(input())\n    al.append(a)\n \nal.insert(0, 0)\nans = -1\npush = 1\nfor i in range(1, n+2):\n    light = al[push]\n    push = light\n    if light == 2:\n        ans = i\n        break\nprint(ans)", "passed": true, "time": 0.15, "memory": 14100.0, "status": "done"}, {"code": "def main():\n    N = int(input())\n    a = [int(input()) for _ in range(N)]\n    cnt = 0\n    tmp = 1\n    while cnt<=100001:\n        tmp = a[tmp-1]\n        cnt += 1\n        if tmp == 2:\n            return cnt\n        elif tmp == 1:\n            return -1\n\n    return -1\n\nprint((main()))\n", "passed": true, "time": 0.14, "memory": 14100.0, "status": "done"}, {"code": "n=int(input())\na=[int(input()) for i in range(n)]\nans=1\nres=1\nfor i in range(n):\n  if a[res-1]==2:\n    print(ans)\n    return\n  else:\n    ans+=1\n    res=a[res-1]\n\nprint(-1)", "passed": true, "time": 0.14, "memory": 14208.0, "status": "done"}, {"code": "N = int(input())\nA = []\nfor i in range(N):\n  A.append(int(input())-1)\n    \n#A.insert(0, 0)\nans = -1\npush = 0\n\nfor i in range(N+1):\n\n    if A[push] == 1:\n        ans = i+1\n        break\n        \n    else:\n        push = A[push]\n      \nprint(ans)\n", "passed": true, "time": 0.23, "memory": 14152.0, "status": "done"}, {"code": "from typing import List\n\n\ndef answer(n: int, a: List[int]) -> int:\n    next_value = 1\n    count = 0\n    for _ in range(n):\n        next_value = a[next_value - 1]\n        count += 1\n        if next_value == 2:\n            return count\n\n    return -1\n\n\ndef main():\n    n = int(input())\n    a = [int(input()) for _ in range(n)]\n    print((answer(n, a)))\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.26, "memory": 15164.0, "status": "done"}, {"code": "n = int(input())\nbuttons = [int(input()) for _ in range(n)]\ndone = [0] * (n + 1)\nnext_button = buttons[0]\nfor _ in range(n):\n    buttons_status = done[next_button]\n    if buttons_status != 0:\n        print((-1))\n        return\n    done[next_button] = 1\n    if next_button == 2:\n        break\n    next_button = buttons[next_button - 1]\nprint((done.count(1)))\n", "passed": true, "time": 0.25, "memory": 14292.0, "status": "done"}, {"code": "LI = lambda: list(map(int, input().split()))\n\nN = int(input())\nA = [int(input()) for _ in range(N)]\n\n\ndef main():\n    a = 1\n    for i in range(N + 1):\n        if a == 2:\n            print(i)\n            return 0\n        a = A[a - 1]\n    print((-1))\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14292.0, "status": "done"}, {"code": "n = int(input())\na = []\ns = [0]*n\nx = 1\nfor i in range(n):\n  a.append(int(input()))\nif 2 not in a:\n  print(-1)\nelse:\n  while True:\n    if a[x-1] == 2:\n      print(sum(s)+1)\n      return\n    else:\n      s[x-1] += 1\n      if s[x-1] == 2:\n        print(-1)\n        return\n      x = a[x-1]", "passed": true, "time": 0.25, "memory": 14332.0, "status": "done"}, {"code": "n = int(input())\nbtn = [0]\nfor i in range(n):\n  btn.append(int(input()))\nsee = [0 for i in range(n+1)]\n  \nnow = 1\ncnt = 0\n\nsee[now] = 1\nwhile True:\n  now = btn[now]\n  cnt += 1\n  \n  if now == 2:\n    print(cnt)\n    break\n  \n  if see[now] == 1:\n    print((-1))\n    break\n  \n  see[now] = 1\n", "passed": true, "time": 0.15, "memory": 14276.0, "status": "done"}, {"code": "n = int(input())\na = [0] + [int(input()) for i in range(n)]\ncnt = 1\nnext_a = a[1]\n\nif a[1] == 2:\n    print(cnt)\n\nelse:\n    while cnt <= 10**5:\n        next_a = a[next_a]\n        cnt += 1\n        if next_a == 2:\n            break\n\n    if cnt <= 10**5:\n        print(cnt)\n    else:\n        print(-1)", "passed": true, "time": 0.15, "memory": 14320.0, "status": "done"}, {"code": "n = int(input())\nA = [int(input()) for _ in range(n)]\n\nnow = 0\ncnt = 0\nwhile A[now] != 2 and cnt <= n:\n    now = A[now] - 1\n    cnt += 1\n\nif A[now] == 2:\n    print((cnt + 1))\nelse:\n    print((-1))\n", "passed": true, "time": 0.15, "memory": 14320.0, "status": "done"}, {"code": "def f():\n    n = int(input())\n    a = [int(input()) for _ in range(n)]\n    x = 1\n    c = 0\n    for i in range(n):\n        if x != 2:\n            x = a[x-1]\n            c += 1\n            if c == n:\n                return -1\n        else:\n            return c\nprint(f())", "passed": true, "time": 0.14, "memory": 14224.0, "status": "done"}, {"code": "import sys\nN = int(input())\na = [int(input()) for _ in range(N)]\nflag = [0]*N\nk = 0\ni = 0\nflag[0] = 1\nwhile k <N:\n  k += 1\n  i = a[i] - 1\n  if flag[i] == 0:\n    flag[i] = 1\n  elif flag[i] == 1:\n    print(-1)\n    return\n  if i == 1:\n    print(k)\n    return", "passed": true, "time": 0.14, "memory": 14248.0, "status": "done"}, {"code": "import sys\nN = int(input())\nd = [int(input()) for i in range(N)]\n\ncnt = 0\nbutton = d[0]\nwhile button != 2:\n    button = d[button-1]\n    cnt += 1\n\n    if cnt > 100000:\n        print(-1)\n        return\n\nprint(cnt+1)", "passed": true, "time": 0.15, "memory": 14064.0, "status": "done"}, {"code": "N = int(input())\nL = []\nlight = [0]*(N)\nlight[0] = 1\ncnt, now = 0, 0\n\nfor i in range(N):\n    L.append(int(input()))\n\nfor i in range(N):\n    now = L[now] - 1\n    cnt += 1\n    if now == 1:\n        print(cnt)\n        return\n\nprint((-1))\n", "passed": true, "time": 0.24, "memory": 14032.0, "status": "done"}, {"code": "N = int(input())\nV = [int(input()) for i in range(1,N+1)]\ncnt = 1\nval = V[0]\nwhile True:\n  if val == 2:\n    print(cnt)\n    break\n  if (cnt >= N):\n    print((-1))\n    break\n  cnt += 1\n  val = V[val-1]\n", "passed": true, "time": 0.24, "memory": 14100.0, "status": "done"}, {"code": "n = int(input())\nal = list(int(input()) for _ in range(n))\nnum = [0]*n\ni = 0\ncnt = 0\nwhile True:\n    if al[i] == 2:\n        print((cnt+1))\n        break\n    else:\n        if num[al[i]-1] == 1:\n            print((-1))\n            break\n        else:\n            num[al[i]-1] += 1\n            i = al[i]-1\n            cnt += 1\n", "passed": true, "time": 0.14, "memory": 14076.0, "status": "done"}, {"code": "n = int(input())\nA = [int(input()) for _ in range(n)]\n\nnow = 0\ncnt = 0\nwhile A[now] != 2 and cnt <= n:\n    now = A[now] - 1\n    cnt += 1\n\nif A[now] == 2:\n    print((cnt + 1))\nelse:\n    print((-1))\n", "passed": true, "time": 0.14, "memory": 14084.0, "status": "done"}, {"code": "N=int(input())\na=[]\nfor i in range(N):\n  a.append(int(input())-1)\nflag=True\nstep=[]\npivot=0\nfor i in range(N+1):\n  step.append(pivot)\n  if a[pivot]==1:\n    break\n  pivot=a[pivot]\n\nif len(step)>N:\n  print(-1)\nelse:\n  print(len(step))", "passed": true, "time": 0.14, "memory": 14076.0, "status": "done"}, {"code": "N = int(input())\nA = [int(input())-1 for _ in range(N)]\ncnt = 1\nnext_button = 0\nwhile cnt <= len(A):\n    next_button = A[next_button]\n    if next_button == 1:\n        print(cnt)\n        return\n    cnt += 1\nprint((-1))\n", "passed": true, "time": 0.14, "memory": 14052.0, "status": "done"}, {"code": "n = int(input())\nl = [0 for _ in range(n)]\nalready = set()\nnext = 1\nc = 0\nfor i in range(n):\n  a = int(input())\n  l[i] = a\nwhile c <= n:\n  next = l[next-1]\n  c += 1\n  if next == 2:\n    print(c)\n    break\n  elif next in already:\n    print(-1)\n    break\n  already.add(next)", "passed": true, "time": 0.14, "memory": 14268.0, "status": "done"}, {"code": "n = int(input())\nbtn = {b+1: int(input()) for b in range(n)}\ncur = 1\ncnt = 0\nwhile cnt <= n and cur != 2:\n  cur = btn[cur]\n  cnt += 1\n  if cur == 2:\n    print(cnt)\n    return\nprint(-1)", "passed": true, "time": 0.14, "memory": 14216.0, "status": "done"}, {"code": "N = int(input())\na = tuple(int(input()) for _ in range(N))\nlog = set()\ni = 0\ncount = 1\nwhile a[i] != 2:\n  i = a[i] - 1\n  if i in log:\n    print(-1)\n    break\n  else:\n    log.add(i)\n    count += 1\nelse:\n  print(count)", "passed": true, "time": 0.14, "memory": 14108.0, "status": "done"}, {"code": "import sys\nimport cProfile\ninput = sys.stdin.readline\ndef main():\n    n=int(input())\n    a=[int(input()) for _ in range(n)]\n    l=[1]\n    i=0\n    aa=1\n    while True:\n        aa=a[aa-1]\n        if aa == 2:\n            print(i+1)\n            return\n        if i>n:\n            print(-1)\n            return\n        i+=1\nmain()", "passed": true, "time": 0.14, "memory": 14092.0, "status": "done"}, {"code": "n = int(input())\na = [0] * n\nfor i in range(n):\n    a[i] = int(input())\n\nnow = 1\nfor i in range(1, n + 10):\n    now = a[now - 1]\n    if now == 2:\n        print(i)\n        break\nelse:\n    print((-1))\n", "passed": true, "time": 0.14, "memory": 14084.0, "status": "done"}, {"code": "n = int(input())\na = [int(input()) for i in range(n)]\ncnt = 0\n\nif 2 not in a:\n  print(-1)\n  return\n\nx = 1\nfor i in range(n-1):\n \n  x = a[x-1]\n  cnt += 1\n\n            \n  if x == 2:\n    print(cnt)\n    return\n\nprint(-1)", "passed": true, "time": 0.14, "memory": 14312.0, "status": "done"}, {"code": "N = int(input())\n\nbuttons = []\nfor _ in range(N):\n  buttons.append(int(input()))\n\ns = 1\ncnt = 0\nwhile True:\n  s = buttons[s-1]\n  cnt += 1\n  if s == 2:\n    break\n  elif cnt > N:\n    cnt = -1\n    break\n\nprint(cnt)", "passed": true, "time": 0.14, "memory": 14112.0, "status": "done"}, {"code": "n = int(input())\ndata = [int(input()) for i in range(n)]\ncount = 0\ntemp = 1\nfor i in range(n):\n    temp = data[temp-1]\n    count += 1\n    if temp == 2:\n        print(count)\n        break\nelse:\n    print(-1)", "passed": true, "time": 0.14, "memory": 14324.0, "status": "done"}, {"code": "n=int(input())\nl=[int(input()) for i in range(n)]\n\nnum=l[0]\ncnt=0\nfor i in range(10**5):\n  if num==2:\n    cnt+=1\n    break\n  else:\n    num=l[num-1]\n    cnt+=1\n    \nif cnt==10**5:\n  cnt=-1\n  \nprint(cnt)", "passed": true, "time": 0.14, "memory": 14048.0, "status": "done"}, {"code": "N = int(input())\na = []\nfor i in range(N):\n    a.append(int(input()))\n\nx = 1\nSum = 0\nwhile x != 2 and Sum < N:\n    x = a[x - 1]\n    Sum += 1\n\nif Sum >= N:\n    print(-1)\nelse:\n    print(Sum)", "passed": true, "time": 0.14, "memory": 14360.0, "status": "done"}, {"code": "n = int(input())\na = [0] * n\nfor i in range(n):\n    a[i] = int(input())\n\ncounter = 0\nnow = 1\nvisit = [False] * n\nvisit[0] = True\nwhile True:\n    counter += 1\n    if a[now - 1] == 2:\n        print(counter)\n        break\n    elif visit[a[now - 1]-1] == True:\n        print((-1))\n        break\n    else:\n        visit[a[now - 1]-1] = True\n    now = a[now - 1]\n", "passed": true, "time": 0.14, "memory": 14144.0, "status": "done"}]