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: import glob
  import os
  
  bn = os.path.basename(__file__).split(".")[0]
  dn = os.path.dirname(__file__).split("/")
  
  # 問題ごとのディレクトリのトップからの相対パス
  REL_PATH = dn[len(dn) - 2] + "\\" + dn[len(dn) - 1] + "\\" + bn
  
  # テスト用ファイル置き場のトップ
  TOP_PATH = "C:\\AtCoder"
  
  
  class Common:
  
      problem = []
      index = 0
  
      def __init__(self, rel_path):
          self.rel_path = rel_path
  
      def initialize(self, path):
          file = open(path)
          self.problem = file.readlines()
          self.index = 0
          return
  
      def input_data(self):
          try:
              IS_TEST
              self.index += 1
              return self.problem[self.index - 1]
  
          except NameError:
              return input()
  
      def resolve(self):
          pass
  
      def exec_resolve(self):
          try:
              IS_TEST
              for path in glob.glob(TOP_PATH + "\\" + self.rel_path + "/*.txt"):
                  print("Test: " + path)
                  self.initialize(path)
                  self.resolve()
                  print("\n\n")
          except NameError:
              self.resolve()
  
  
  class Solver(Common):
  
      alph = []
  
      def rec(self, result, now, max, N):
  
          if now == N:
              res = ""
              for i in result:
                  res += self.alph[i]
              print(res)
              return
  
          if now == 0:
              result[0] = 0
              self.rec(result, now + 1, 0, N)
              return
  
          for i in range(0, max + 2):
              if i == N:
                  return
              result[now] = i
              if i > max:
                  self.rec(result, now + 1, i, N)
              else:
                  self.rec(result, now + 1, max, N)
  
      def resolve(self):
  
          N, K = map(int, self.input_data().split())
          A = list(map(int, self.input_data().split()))
          A.insert(0, 0)
          town_visit_time = [-1] * (N + 1)
  
          num_loop = 0
          loop_start_count = 0
          loop_start_town = 0
  
          next_town = 1
          for i in range(0, N + 1):
              if town_visit_time[next_town] != -1:
                  num_loop = i - town_visit_time[next_town]
                  loop_start_count = town_visit_time[next_town]
                  loop_start_town = next_town
                  break
  
              town_visit_time[next_town] = i
              next_town = A[next_town]
  
              if i + 1 == K:
                  print(str(next_town))
                  exit(0)
  
          remain = (K - loop_start_count) % num_loop
  
          result = loop_start_town
          for i in range(remain):
              result = A[result]
  
          print(str(result))
  
  
  solver = Solver(REL_PATH)
  solver.exec_resolve()
  
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"}]