stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=k; i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 1.4424322338924016x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    int n; ll k; cin >> n >> k;

    vector<int> A(n);

    rep(i, n) cin >> A[i], A[i]--;



    vector<int> first_visited(200200, -1); //その町に最初に訪れた時にテレポーターを使った回数

    first_visited[0] = 0;

    vector<int> pass; pass.push_back(0); //同じ町に二度訪れるまでの経路



    //ループが始まるまでの経路を作成

    //rp:　pass内の閉路の長さ 　nrp: 閉路でない経路の長さ  cur: 現在地

    int rp = 0, nrp = 0, cur = 0; 

    for(int cnt=1; cnt<=k; cnt++){

        int to = A[cur]; 

        if(first_visited[to] == -1){ //次の街をまだ訪れていない場合は経路を追加してそこに移動する

            pass.push_back(to);

            first_visited[to] = cnt;

            cur = to;

        }else{ //同じ町を二度訪れる場合はループ終了 

            rp = cnt - first_visited[to];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size(); //閉路を見つける前にループが終わった場合の処理



    int ans = nrp + (k - nrp) % rp;

    cout << pass[ans] + 1 << ln;

}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=min(n, k); i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 2.616753583889244x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000100010001000100LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){ 

    ll n, k; cin >> n >> k;

    vector<int> next(n);

    rep(i, n){

        int a; cin >> a; a--;

        next[i] = a;

    }

    vector<int> seen(n,-1); //最短距離

    seen[0] = 0; int cur = 0; bool roop = false;

    rep(i, k){

        int nx = next[cur];

        if(seen[nx]== -1){

            seen[nx] = seen[cur]+1;

            cur = nx;

        }

        else{

            roop = true;

            break;

        }

    }

    if(roop){

        ll len = seen[cur]+1 - seen[next[cur]];

        ll rs = k - seen[next[cur]];

        rep(i, rs % len+1){

            cur = next[cur];

        }

    }

    cout << cur+1 << ln;



}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=k; i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 2.616905546751094x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000100010001000100LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){ 

    ll n, k; cin >> n >> k;

    vector<int> next(n);

    rep(i, n){

        int a; cin >> a; a--;

        next[i] = a;

    }

    vector<int> seen(n,-1); //最短距離

    seen[0] = 0; int cur = 0; bool roop = false;

    rep(i, k){

        int nx = next[cur];

        if(seen[nx]== -1){

            seen[nx] = seen[cur]+1;

            cur = nx;

        }

        else{

            roop = true;

            break;

        }

    }

    if(roop){

        ll len = seen[cur]+1 - seen[next[cur]];

        ll rs = k - seen[next[cur]];

        rep(i, rs % len+1){

            cur = next[cur];

        }

    }

    cout << cur+1 << ln;



}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    int n; ll k; cin >> n >> k;

    vector<int> A(n);

    rep(i, n) cin >> A[i], A[i]--;



    vector<int> first_visited(200200, -1); //その町に最初に訪れた時にテレポーターを使った回数

    first_visited[0] = 0;

    vector<int> pass; pass.push_back(0); //同じ町に二度訪れるまでの経路



    //ループが始まるまでの経路を作成

    //rp:　pass内の閉路の長さ 　nrp: 閉路でない経路の長さ  cur: 現在地

    int rp = 0, nrp = 0, cur = 0; 

    for(int cnt=1; cnt<=k; cnt++){

        int to = A[cur]; 

        if(first_visited[to] == -1){ //次の街をまだ訪れていない場合は経路を追加してそこに移動する

            pass.push_back(to);

            first_visited[to] = cnt;

            cur = to;

        }else{ //同じ町を二度訪れる場合はループ終了 

            rp = cnt - first_visited[to];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size(); //閉路を見つける前にループが終わった場合の処理



    int ans = nrp + (k - nrp) % rp;

    cout << pass[ans] + 1 << ln;

}


// Faster C++ solution for the same problem (measured speedup: 1.8142311890031586x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000100010001000100LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){ 

    ll n, k; cin >> n >> k;

    vector<int> next(n);

    rep(i, n){

        int a; cin >> a; a--;

        next[i] = a;

    }

    vector<int> seen(n,-1); //最短距離

    seen[0] = 0; int cur = 0; bool roop = false;

    rep(i, k){

        int nx = next[cur];

        if(seen[nx]== -1){

            seen[nx] = seen[cur]+1;

            cur = nx;

        }

        else{

            roop = true;

            break;

        }

    }

    if(roop){

        ll len = seen[cur]+1 - seen[next[cur]];

        ll rs = k - seen[next[cur]];

        rep(i, rs % len+1){

            cur = next[cur];

        }

    }

    cout << cur+1 << ln;



}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=min(n, k); i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 2.56914402153432x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>; 

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1001001001LL;

constexpr long long LINF = 1001001001001001001;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){

    int n; ll k; cin >> n >> k;

    vector<ll> A(n); rep(i, n) cin >> A[i], A[i]--;



    //ダブリング

    vector<vector<ll>> D(70, vector<ll>(n));

    rep(i, n)D[0][i] = A[i];

    rept(i, 1, 70){

        rep(j, n){

            D[i][j] = D[i-1][D[i-1][j]];

        }

    }

    /*

    rep(i, 5){

        rep(j, n){

            cout << D[i][j] << " ";

        }cout << ln;

    }*/



    vector<int> check(65); ll num = k, rui = pow(2, 60);

    for(int i=60; i>=0; i--){

        if(num >= rui){

            check[i] = true;

            num -= rui;

        }

        rui /= 2;

    }

    int v = 0;

    rep(i, 65){

        if(check[i]){

            v = D[i][v];

        }

    }

    cout << v + 1 << ln;

}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=k; i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 2.569293219563672x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>; 

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1001001001LL;

constexpr long long LINF = 1001001001001001001;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){

    int n; ll k; cin >> n >> k;

    vector<ll> A(n); rep(i, n) cin >> A[i], A[i]--;



    //ダブリング

    vector<vector<ll>> D(70, vector<ll>(n));

    rep(i, n)D[0][i] = A[i];

    rept(i, 1, 70){

        rep(j, n){

            D[i][j] = D[i-1][D[i-1][j]];

        }

    }

    /*

    rep(i, 5){

        rep(j, n){

            cout << D[i][j] << " ";

        }cout << ln;

    }*/



    vector<int> check(65); ll num = k, rui = pow(2, 60);

    for(int i=60; i>=0; i--){

        if(num >= rui){

            check[i] = true;

            num -= rui;

        }

        rui /= 2;

    }

    int v = 0;

    rep(i, 65){

        if(check[i]){

            v = D[i][v];

        }

    }

    cout << v + 1 << ln;

}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    int n; ll k; cin >> n >> k;

    vector<int> A(n);

    rep(i, n) cin >> A[i], A[i]--;



    vector<int> first_visited(200200, -1); //その町に最初に訪れた時にテレポーターを使った回数

    first_visited[0] = 0;

    vector<int> pass; pass.push_back(0); //同じ町に二度訪れるまでの経路



    //ループが始まるまでの経路を作成

    //rp:　pass内の閉路の長さ 　nrp: 閉路でない経路の長さ  cur: 現在地

    int rp = 0, nrp = 0, cur = 0; 

    for(int cnt=1; cnt<=k; cnt++){

        int to = A[cur]; 

        if(first_visited[to] == -1){ //次の街をまだ訪れていない場合は経路を追加してそこに移動する

            pass.push_back(to);

            first_visited[to] = cnt;

            cur = to;

        }else{ //同じ町を二度訪れる場合はループ終了 

            rp = cnt - first_visited[to];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size(); //閉路を見つける前にループが終わった場合の処理



    int ans = nrp + (k - nrp) % rp;

    cout << pass[ans] + 1 << ln;

}


// Faster C++ solution for the same problem (measured speedup: 1.781222825720164x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>; 

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1001001001LL;

constexpr long long LINF = 1001001001001001001;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){

    int n; ll k; cin >> n >> k;

    vector<ll> A(n); rep(i, n) cin >> A[i], A[i]--;



    //ダブリング

    vector<vector<ll>> D(70, vector<ll>(n));

    rep(i, n)D[0][i] = A[i];

    rept(i, 1, 70){

        rep(j, n){

            D[i][j] = D[i-1][D[i-1][j]];

        }

    }

    /*

    rep(i, 5){

        rep(j, n){

            cout << D[i][j] << " ";

        }cout << ln;

    }*/



    vector<int> check(65); ll num = k, rui = pow(2, 60);

    for(int i=60; i>=0; i--){

        if(num >= rui){

            check[i] = true;

            num -= rui;

        }

        rui /= 2;

    }

    int v = 0;

    rep(i, 65){

        if(check[i]){

            v = D[i][v];

        }

    }

    cout << v + 1 << ln;

}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=min(n, k); i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 2.5742628279099695x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>; 

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1001001001LL;

constexpr long long LINF = 1001001001001001001;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){

    int n; ll k; cin >> n >> k;

    vector<ll> A(n); rep(i, n) cin >> A[i], A[i]--;



    //ダブリング

    vector<vector<ll>> D(60, vector<ll>(n));

    rep(i, n)D[0][i] = A[i];

    rept(i, 1, 60){

        rep(j, n){

            D[i][j] = D[i-1][D[i-1][j]];

        }

    }



    int v = 0;

    rep(i, 60){

        if(k&1){

            v = D[i][v];

        }

        k /= 2;

    }

    cout << v + 1 << ln;

}
// Slower C++ solution:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii =  pair<int, int>;

using pll =  pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1000000009LL;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }







int main() {

    ll n, k; cin >> n >> k;

    vector<ll> A(n+10); A.push_back(-1);

    rep(i, n) cin >> A[i+1];



    vector<ll> visited(200200, -1);

    visited[1] = 0;

    vector<ll> pass; pass.push_back(1);



    ll rp = 0, nrp = 0, cur = 1;

    for(ll i=1; i<=k; i++){

        ll next = A[cur];

        if(visited[next] == -1){

            pass.push_back(next);

            visited[next] = i;

            cur = next;

        }else{

            rp = i - visited[next];

            nrp = pass.size() - rp;

            break;

        }

    }

    if(rp == 0) rp = pass.size();

    ll x = (k - nrp) % rp;

    cout << pass[nrp + x] << ln;

}


// Faster C++ solution for the same problem (measured speedup: 2.5744123232040206x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>; 

using pll = pair<long long, long long>;

constexpr char ln =  '\n';

constexpr long long MOD = 1000000007LL;

constexpr long long INF = 1001001001LL;

constexpr long long LINF = 1001001001001001001;

#define all(x) (x).begin(),(x).end()

#define rep(i,n) for(int i=0;i<(n);i++)

#define rept(i, j, n) for(int i=(j); i<(n); i++)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





int main(){

    int n; ll k; cin >> n >> k;

    vector<ll> A(n); rep(i, n) cin >> A[i], A[i]--;



    //ダブリング

    vector<vector<ll>> D(60, vector<ll>(n));

    rep(i, n)D[0][i] = A[i];

    rept(i, 1, 60){

        rep(j, n){

            D[i][j] = D[i-1][D[i-1][j]];

        }

    }



    int v = 0;

    rep(i, 60){

        if(k&1){

            v = D[i][v];

        }

        k /= 2;

    }

    cout << v + 1 << ln;

}