output_description: Print the integer representing the town the king will be in if he starts at
  Town 1 and uses a teleporter exactly K times from there.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N K
      A_1 A_2 \dots A_N
attempt: def readinput():
      n, k = list(map(int, input().split()))
      a = list(map(int, input().split()))
      return n, k, a
  
  
  def main(n, k, a):
      tbl = {}
      for i in range(1, n + 1):
          tbl[i] = a[i - 1]
      machi = 1
      for i in range(k):
          machi = tbl[machi]
      return machi
  
  
  def main2(n, k, a):
      a.insert(0, 0)
      # print(a)
      nvisited = [0 for i in range(n + 1)]
      # print(nvisited)
      visited = []
      current = 1
      visited.append(current)
      nvisited[current] += 1
      next = a[current]
      # print('next='+str(next))
      while nvisited[next] == 0:
          current = next
          visited.append(current)
          nvisited[current] += 1
          next = a[current]
      loophead = next
      # print('loophead:'+str(loophead))
      # print('visited'+str(visited))
      beforeloop = visited.index(loophead)
      # print('beforeloop:'+str(beforeloop))
      loop = visited[beforeloop:]
      # print('loop:'+str(loop))
      looplen = len(loop)
  
      if k < beforeloop:
          return visited[k + 1]
      else:
          return loop[(k - beforeloop) % looplen]
  
  
  def detectloop(a):
      # loopを検出して
      # loopに入る前に訪れた町リスト:head
      # loop内で訪れる町リスト:loop
      # を返す
      nvisit = [0 for i in range(len(a))]
      visited = []
      n = a[0]
      while nvisit[n - 1] == 0:
          nvisit[n - 1] += 1
          visited.append(n)
          n = a[n - 1]
      # print(visited,n)
      i = visited.index(n)
      # print(i)
      if i == 0:
          loop = visited[:]
          head = []
      else:
          loop = visited[i:]
          head = visited[:i]
      return head, loop
  
  
  def main3(n, k, a):
      head, loop = detectloop(a)
      print(head, loop, k)
      if k <= len(head):
          return head[k - 1]
      else:
          return loop[(k - len(head)) % len(loop) - 1]
  
  
  if __name__ == "__main__":
      n, k, a = readinput()
      ans = main3(n, k, a)
      print(ans)
  
problem_description: Statement
  
  The Kingdom of Takahashi has N towns, numbered 1 through N.
  
  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq
  N) sends you to Town A_i.
  
  Takahashi, the king, loves the positive integer K. The selfish king wonders
  what town he will be in if he starts at Town 1 and uses a teleporter exactly K
  times from there.
  
  Help the king by writing a program that answers this question.
samples: [{"input": "4 5\n    3 2 4 1", "output": "4\n    \n\nIf we start at Town 1 and use the teleporter 5 times, our travel will be as\nfollows: 1 \\to 3 \\to 4 \\to 1 \\to 3 \\to 4.\n\n* * *"}, {"input": "6 727202214173249351\n    6 5 2 5 3 2", "output": "2"}]