Language: C++

int main() {
    int N, cnt = 0, next = 1, lp = 0;
    int64_t K, amari;
    cin >> N >> K;
    vector<int> town(N);
    vector<int> bucket(N, 0);
    vector<int> b2(N, 0);
    vector<int> loop;
    for (int i = 0; i < N; i++) cin >> town.at(i);
    
    while (1) {
        bucket.at(next-1)++;
        next = town.at(next-1);
        cnt++;
        if (bucket.at(next-1) > 1) {
            for (int i = 0; b2.at(next-1) < 1; i++) {
                b2.at(next-1)++;
                next = town.at(next-1);
                //loop.push_back(next);
                lp++;
            }
            break;
        }
    }
    if (lp != 0) {
        //amari = (K-cnt+lp-1) % lp;
        amari = K % lp;
        cout << town.at(amari) << endl;
    } else {
        while (K >= 1) {
            next = town.at(next-1);
            cnt++;
            K--;
        }
        cout << town.at(next-1) << endl;
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:3:5: error: 'int64_t' was not declared in this scope
    3 |     int64_t K, amari;
      |     ^~~~~~~
a.cc:1:1: note: 'int64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
  +++ |+#include <cstdint>
    1 | int main() {
a.cc:4:5: error: 'cin' was not declared in this scope
    4 |     cin >> N >> K;
      |     ^~~
a.cc:4:17: error: 'K' was not declared in this scope
    4 |     cin >> N >> K;
      |                 ^
a.cc:5:5: error: 'vector' was not declared in this scope
    5 |     vector<int> town(N);
      |     ^~~~~~
a.cc:5:12: error: expected primary-expression before 'int'
    5 |     vector<int> town(N);
      |            ^~~
a.cc:6:12: error: expected primary-expression before 'int'
    6 |     vector<int> bucket(N, 0);
      |            ^~~
a.cc:7:12: error: expected primary-expression before 'int'
    7 |     vector<int> b2(N, 0);
      |            ^~~
a.cc:8:12: error: expected primary-expression before 'int'
    8 |     vector<int> loop;
      |            ^~~
a.cc:9:40: error: 'town' was not declared in this scope
    9 |     for (int i = 0; i < N; i++) cin >> town.at(i);
      |                                        ^~~~
a.cc:12:9: error: 'bucket' was not declared in this scope
   12 |         bucket.at(next-1)++;
      |         ^~~~~~
a.cc:13:16: error: 'town' was not declared in this scope
   13 |         next = town.at(next-1);
      |                ^~~~
a.cc:16:29: error: 'b2' was not declared in this scope
   16 |             for (int i = 0; b2.at(next-1) < 1; i++) {
      |                             ^~
a.cc:27:9: error: 'amari' was not declared in this scope
   27 |         amari = K % lp;
      |         ^~~~~
a.cc:28:9: error: 'cout' was not declared in this scope
   28 |         cout << town.at(amari) << endl;
      |         ^~~~
a.cc:28:17: error: 'town' was not declared in this scope
   28 |         cout << town.at(amari) << endl;
      |                 ^~~~
a.cc:28:35: error: 'endl' was not declared in this scope
   28 |         cout << town.at(amari) << endl;
      |                                   ^~~~
a.cc:31:20: error: 'town' was not declared in this scope
   31 |             next = town.at(next-1);
      |                    ^~~~
a.cc:35:9: error: 'cout' was not declared in this scope
   35 |         cout << town.at(next-1) << endl;
      |         ^~~~
a.cc:35:17: error: 'town' was not declared in this scope
   35 |         cout << town.at(next-1) << endl;
      |                 ^~~~
a.cc:35:36: error: 'endl' was not declared in this scope
   35 |         cout << town.at(next-1) << endl;
      |                                    ^~~~