Language: C++

#include <iostream>
#include <vector>

int main()
{
  uint64_t N,K;
  std::cin>>N>>K;
  
  std::vector<uint64_t> A(N);
  for(auto &e:A)
    std::cin>>e;
  
  uint64_t currentPos=1;
  std::vector<uint64_t> firstFoundCount(N, 0);
  uint64_t loopLength=0;
  uint64_t loopStartCount=0;
  for(uint64_t i=1;;i++)
  {
    currentPos=A[currentPos-1];
    if(i==K)
    {
      int64_t result=currentPos;  
      std::cout<<result<<std::endl;
      return 0;
    }
    if(firstFoundCount[currentPos-1]!=0)
    {
      loopStartPos=firstFoundCount[currentPos-1];
      loopLength=i-firstFoundCount[currentPos-1];
      break;
    }
    firstFoundCount[currentPos-1]=i;
  }
  
  uint64_t newK=(K-loopStartCount)%loopLength;
  for(uint64_t i=0;i!=newK;i++)
  {
    currentPos=A[currentPos-1];
  }
  
  int64_t result=currentPos;
  
  std::cout<<result<<std::endl;
  
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:6:3: error: 'uint64_t' was not declared in this scope
    6 |   uint64_t N,K;
      |   ^~~~~~~~
a.cc:3:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    2 | #include <vector>
  +++ |+#include <cstdint>
    3 | 
a.cc:7:13: error: 'N' was not declared in this scope
    7 |   std::cin>>N>>K;
      |             ^
a.cc:7:16: error: 'K' was not declared in this scope
    7 |   std::cin>>N>>K;
      |                ^
a.cc:9:23: error: template argument 2 is invalid
    9 |   std::vector<uint64_t> A(N);
      |                       ^
a.cc:10:15: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   10 |   for(auto &e:A)
      |               ^
      |               std::begin
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:10:15: error: 'end' was not declared in this scope; did you mean 'std::end'?
   10 |   for(auto &e:A)
      |               ^
      |               std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:13:11: error: expected ';' before 'currentPos'
   13 |   uint64_t currentPos=1;
      |           ^~~~~~~~~~~
      |           ;
a.cc:14:23: error: template argument 2 is invalid
   14 |   std::vector<uint64_t> firstFoundCount(N, 0);
      |                       ^
a.cc:14:45: error: expression list treated as compound expression in initializer [-fpermissive]
   14 |   std::vector<uint64_t> firstFoundCount(N, 0);
      |                                             ^
a.cc:15:11: error: expected ';' before 'loopLength'
   15 |   uint64_t loopLength=0;
      |           ^~~~~~~~~~~
      |           ;
a.cc:16:11: error: expected ';' before 'loopStartCount'
   16 |   uint64_t loopStartCount=0;
      |           ^~~~~~~~~~~~~~~
      |           ;
a.cc:17:15: error: expected ';' before 'i'
   17 |   for(uint64_t i=1;;i++)
      |               ^~
      |               ;
a.cc:17:21: error: 'i' was not declared in this scope
   17 |   for(uint64_t i=1;;i++)
      |                     ^
a.cc:19:5: error: 'currentPos' was not declared in this scope
   19 |     currentPos=A[currentPos-1];
      |     ^~~~~~~~~~
a.cc:28:7: error: 'loopStartPos' was not declared in this scope
   28 |       loopStartPos=firstFoundCount[currentPos-1];
      |       ^~~~~~~~~~~~
a.cc:29:7: error: 'loopLength' was not declared in this scope
   29 |       loopLength=i-firstFoundCount[currentPos-1];
      |       ^~~~~~~~~~
a.cc:35:11: error: expected ';' before 'newK'
   35 |   uint64_t newK=(K-loopStartCount)%loopLength;
      |           ^~~~~
      |           ;
a.cc:36:15: error: expected ';' before 'i'
   36 |   for(uint64_t i=0;i!=newK;i++)
      |               ^~
      |               ;
a.cc:36:20: error: 'i' was not declared in this scope
   36 |   for(uint64_t i=0;i!=newK;i++)
      |                    ^
a.cc:36:23: error: 'newK' was not declared in this scope
   36 |   for(uint64_t i=0;i!=newK;i++)
      |                       ^~~~
a.cc:38:5: error: 'currentPos' was not declared in this scope
   38 |     currentPos=A[currentPos-1];
      |     ^~~~~~~~~~
a.cc:41:18: error: 'currentPos' was not declared in this scope
   41 |   int64_t result=currentPos;
      |                  ^~~~~~~~~~