stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;



}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;



}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



using ll = long long;

using ii = pair<int,int>;



#define ff first

#define ss second



const int maxn = 2e5+10;



int p[maxn],sz[maxn];



void init(){

	for(int i=0;i<maxn;++i){

		p[i]=i;

		sz[i]=1;

	}

}



int find_set(int x){

	return p[x] == x ? x : p[x] = find_set(p[x]);

}



void union_set(int a,int b){

	a = find_set(a);

	b = find_set(b);

	if(a==b) return;

	if(sz[a]>sz[b])swap(a,b);

	p[a]=b;

	sz[b]+=sz[a];

}



vector<int> g[maxn],ts;

bitset<maxn> vs;



int cicle=0;



void dfs(int x){

	if(vs[x]){

		cicle=x;

		return;

	}

	vs[x]=true;

	for(auto &u:g[x])

		dfs(u);

	ts.push_back(x);

}



int main(){

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	init();

	int n;

	ll k;

	cin >> n >> k;

	vector<int> A(n);

	for(int i=1;i<=n;++i){

		cin >> A[i-1];

		g[i].push_back(A[i-1]);

	}

	dfs(1);

	reverse(ts.begin(),ts.end());

	vector<int> towns;

	//cout << "cicle start at value = " << cicle << '\n';

	for(int i=0;i<ts.size();++i){

		//cout << ts[i] << " \n"[i==ts.size()-1];

		if(ts[i]==cicle){

			for(int j=i;j<ts.size();++j)

				towns.push_back(ts[j]);

			break;

		}

	}

	if(k<ts.size()){

		cout << ts[k] << '\n';

	}

	else{

		k-=ts.size();

		//cout << "k = " << k << " towns.size() = "<< towns.size() << " k%t.sz() = "<< k%towns.size() << '\n';

		cout << towns[k%towns.size()] << '\n';

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



using ll = long long;

using ii = pair<int,int>;



#define ff first

#define ss second



const int maxn = 2e5+10;



vector<int> g[maxn],ts;

bitset<maxn> vs;



int cicle=0;



void dfs(int x){

	if(vs[x]){

		cicle=x;

		return;

	}

	vs[x]=true;

	for(auto &u:g[x])

		dfs(u);

	ts.push_back(x);

}



int main(){

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	int n;

	ll k;

	cin >> n >> k;

	vector<int> A(n);

	for(int i=1;i<=n;++i){

		cin >> A[i-1];

		g[i].push_back(A[i-1]);

	}

	dfs(1);

	reverse(ts.begin(),ts.end());

	vector<int> towns;

	//cout << "cicle start at value = " << cicle << '\n';

	for(int i=0;i<ts.size();++i){

		//cout << ts[i] << " \n"[i==ts.size()-1];

		if(ts[i]==cicle){

			for(int j=i;j<ts.size();++j)

				towns.push_back(ts[j]);

			break;

		}

	}

	if(k<ts.size()){

		cout << ts[k] << '\n';

	}

	else{

		k-=ts.size();

		//cout << "k = " << k << " towns.size() = "<< towns.size() << " k%t.sz() = "<< k%towns.size() << '\n';

		cout << towns[k%towns.size()] << '\n';

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



const int N = 2e5+5;

ll n, k;

int a[N];

bool vis[N];

vector<int> path;



int main() {

	ios::sync_with_stdio(0); cin.tie(0);

	cin >> n >> k;

	for(int i = 1; i <= n; i++)

		cin >> a[i];



	memset(vis, 0, sizeof vis);

	int pos = 1;

	vis[pos] = 1;

	path.push_back(1);

	while(!vis[a[pos]] && k > 0)

		pos = a[pos], vis[pos] = 1, path.push_back(pos), k--;



	if(k == 0)

		cout << pos << '\n';

	else {

		int x = a[pos];

		int idx = 0;

		while(path[idx] != x) idx++;

		ll m = path.size() - idx;

		k = (k+m-1)%m;

		int p = 0;

		while(p < k) p++;

		cout << path[idx+p] << '\n';

	}



	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

int a[2000005], b[2000005], flag[2000005] = { 0 };

int main()

{

	int n, i, j, mod;

	long long k;

	scanf("%d %lld", &n, &k);

	for (i = 1; i <= n; i++)

		scanf("%d", &a[i]);//a[i]存下一位

	flag[1] = 0;

	b[1] = a[1];

	i = 1;

	j = 1;

	while (flag[b[j]]==0&&j<=k)

	{

		flag[b[j]] = j;

		b[j+1] = a[b[j]];

		j++;

	}

//	b[j]已经有了

	if (j > k)

	{

		printf("%d", b[k]);

		return 0;

	}

	mod = j - flag[b[j]];

	k = k - flag[b[j]];

	k = k % mod;

	k = k + flag[b[j]];

	printf("%d", b[k]);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2e5+5;

ll k;

int a[N],b[N]={0};

int vis[N]={0};

int main()

{

	int n,t=1,now=1,loop=0;

	cin>>n>>k;

	for(int i=1;i<=n;i++)

		cin>>a[i];

	vis[1]=1;

	b[1]=0;

	while(k--)

	{	

		now=a[now];

		vis[now]++;

		if(vis[now]==2&&loop==0)

		{

			loop=t-b[now];

			k%=loop;

		}

		b[now]=t;

		t++;

		if(k==0)

		{

			cout<<now<<endl;

			break;

		}

	}		

} 

// Optimized solution to the same problem (faster):
#include <stdio.h>

int a[2000005], b[2000005], flag[2000005] = { 0 };

int main()

{

	int n, i, j, mod;

	long long k;

	scanf("%d %lld", &n, &k);

	for (i = 1; i <= n; i++)

		scanf("%d", &a[i]);//a[i]存下一位

	flag[1] = 0;

	b[1] = a[1];

	i = 1;

	j = 1;

	while (flag[b[j]]==0&&j<=k)

	{

		flag[b[j]] = j;

		b[j+1] = a[b[j]];

		j++;

	}

//	b[j]已经有了

	if (j > k)

	{

		printf("%d", b[k]);

		return 0;

	}

	mod = j - flag[b[j]];

	k = k - flag[b[j]];

	k = k % mod;

	k = k + flag[b[j]];

	printf("%d", b[k]);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2e5+5;

const int inf=0x3f3f3f3f;

#define endl '\n'



int n,a[N],sq[N],cnt,start,t;

ll k;



int main(){

	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

	cin>>n>>k;

	for(int i=1;i<=n;i++){

		cin>>a[i];

		sq[i]=-1;

	}

	cnt=-1;

	for(int i=1;;){

		cnt++;

		if(cnt==k) {

			cout<<i<<endl;

			return 0;

		}

		if(sq[i]==-1) {

			sq[i]=cnt;

			i=a[i];

		}

		else {

			t=cnt-sq[i];

			if(i==1){

				start=a[1];

				k=(k-1)%t;

			}

			else {

				start=i;

				k=(k-sq[i])%t;

			}

			break;

		}

	}

	cnt=-1;

	for(int i=start;;i=a[i]){

		cnt++;

		if(cnt==k) {

			cout<<i<<endl;

			break;

		}

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

int a[2000005], b[2000005], flag[2000005] = { 0 };

int main()

{

	int n, i, j, mod;

	long long k;

	scanf("%d %lld", &n, &k);

	for (i = 1; i <= n; i++)

		scanf("%d", &a[i]);//a[i]存下一位

	flag[1] = 0;

	b[1] = a[1];

	i = 1;

	j = 1;

	while (flag[b[j]]==0&&j<=k)

	{

		flag[b[j]] = j;

		b[j+1] = a[b[j]];

		j++;

	}

//	b[j]已经有了

	if (j > k)

	{

		printf("%d", b[k]);

		return 0;

	}

	mod = j - flag[b[j]];

	k = k - flag[b[j]];

	k = k % mod;

	k = k + flag[b[j]];

	printf("%d", b[k]);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define pb push_back

#define mp make_pair

#define SZ(x) ((int)(x).size())

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

#define mem0(s) memset(s,0,sizeof(s))

#define meminf(s) memset(s,0x3f,sizeof(s))

#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))

double pi=acos(-1);

typedef long long ll;

typedef unsigned long long ull;

inline ll lowbit(ll x){return x&(-x);}

inline ll min(ll a,ll b){return a<b?a:b;}

inline ll max(ll a,ll b){return a>b?a:b;}

const int INF=0x3f3f3f3f,max_n=200005,mod=998244353;

inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}

inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

int qidian;

int huan;

int a[max_n];

int ci[max_n];

int bu;

int count1;

int ans[max_n];

vector<int>v;

int main()

{

	int n;ll k;

	cin>>n>>k;

	for(int i=1;i<=n;i++)

	{

		cin>>a[i];

	}

	int dang=1;ci[dang]=1;

	while(1)

	{

		dang=a[dang];

		count1++;

		ans[count1]=dang;

		if(ci[dang]==0)ci[dang]=1;

		else

		{

			qidian=dang;

			ci[dang]++;

			if(bu==0)bu=count1;

		}

		if(qidian)

		{

			huan++;

			v.pb(dang);

		}

		if(ci[dang]==3)

		{

			break;

		}

	}

	if(k<bu)

	{

		cout<<ans[k];

	}

	else

	{

		huan--;

	k-=(bu-huan);

	cout<<v[k%huan];

	}

	

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

int a[2000005], b[2000005], flag[2000005] = { 0 };

int main()

{

	int n, i, j, mod;

	long long k;

	scanf("%d %lld", &n, &k);

	for (i = 1; i <= n; i++)

		scanf("%d", &a[i]);//a[i]存下一位

	flag[1] = 0;

	b[1] = a[1];

	i = 1;

	j = 1;

	while (flag[b[j]]==0&&j<=k)

	{

		flag[b[j]] = j;

		b[j+1] = a[b[j]];

		j++;

	}

//	b[j]已经有了

	if (j > k)

	{

		printf("%d", b[k]);

		return 0;

	}

	mod = j - flag[b[j]];

	k = k - flag[b[j]];

	k = k % mod;

	k = k + flag[b[j]];

	printf("%d", b[k]);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<stdio.h>

//#include <bits/stdc++.h>

#include<vector>

#include<float.h>

#include<iomanip>

#include<algorithm>

#include<string>

#include<cstring>

#include<math.h>

#include<cmath>

#include<sstream>

#include<set>

#include<map>

#include<queue>

#include <cassert>

#include <cmath>

#include<cstdint>



#define INF 1e9

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

#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)

#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;

#define vec(type,n) vector<type>(n)

#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))

#define ALL(a)  (a).begin(),(a).end()



using namespace std;

using ll = long long;

using Graph = vector<vector<int>>;

using P = pair<ll, ll>;



ll cnt;

Graph G(200000);

vector<int> mem;

vector<int> seen(200000);



ll s, e;

void dfs(int v){

    cnt++;

    if(seen[v] > 0){

        s = seen[v];

        e = cnt;

        return;

    }

    seen[v] = cnt;

    mem.push_back(v);

    dfs(G[v][0]);

}



int main(){

    ll n, k;

    cin >> n >> k;

    rep(i, n){

        int a;

        cin >> a;

        a--;

        G[i].push_back(a);

    }

    dfs(0);

    s--;

  	k -= s;

    if(k < 0){

        k += s;

        cout << mem[k] + 1;

        return 0;

    }

  	k %= (e - s - 1);

  	cout << mem[s + k] + 1; 

}




// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

//#include <atcoder/all>



#define INF 1e9

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

#define REP(i,a,b)for(long long i=(long long)(a);(i)<=(long long)(b);i++)

#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;

#define vec(type,n) vector<type>(n)

#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))

#define ALL(a)  (a).begin(),(a).end()

#define pb push_back



using namespace std;

//using namespace atcoder;

using ll = long long;

using P = pair<ll, ll>;

//using mint = modint1000000007;





ll cnt[20][20];

int main(){

    ll n, k;

    cin >> n >> k;

    vector<ll> a(n + 1);

    rep(i, n)cin >> a[i];

    int mx = 62;

    vector<vector<ll>> next(mx + 1, vector<ll> (n + 1, -1));

    rep(i, n)next[0][i + 1] = a[i];

    rep(i, mx){

        REP(j, 1, n)next[i + 1][j] = next[i][next[i][j]];

    }

    int now = 1;

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

        if(((k >> i) & 1)){

            now = next[i][now];

        }

    }

    cout << now << endl;

  //rep(i, 10){rep(j, n + 1) cout << next[i][j] << " "; cout << endl;}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

int main(void)

{

  int N,i,j,x,MOD,A[200000]={0},check[200000]={0},move[200000]={0};

  long long K;

  x=scanf("%d %lld",&N,&K);

  for(i=0;i<N;i++)x=scanf("%d",&A[i]),check[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(check[i]<0)check[i]=j,move[j]=i;else{x=j-1,MOD=j-check[i];break;}

  if(K>x)K=(K-x+MOD-1)%MOD+x-MOD+1;

  printf("%d\n",move[K]+1);

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[sz]={0},here[sz]={0},town[sz]={0};

inline LL IN(void)

{

  LL x=0;int f=1;char c=getchar();

  while((c<'0'||c>'9')&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();

  return f*x;

}

int main(void)

{

  int N,i,j,x;LL K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",town[K]+1),0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

int main(void)

{

  int N,i,j,x,y,MOD,A[200000]={0},check[200000]={},move[200000]={0};

  long long K;

  x=scanf("%d %lld",&N,&K);

  for(i=0;i<N;i++)x=scanf("%d",&A[i]),check[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(check[i]<0)check[i]=j,move[j]=i;else{x=j-1,y=check[i],MOD=j-check[i];break;}

  if(K>x)K=(K-y)%MOD+y;

  printf("%d\n",move[K]+1);

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[sz]={0},here[sz]={0},town[sz]={0};

inline LL IN(void)

{

  LL x=0;int f=1;char c=getchar();

  while((c<'0'||c>'9')&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();

  return f*x;

}

int main(void)

{

  int N,i,j,x;LL K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",town[K]+1),0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

int main(void)

{

  int N,i,j,x,y,MOD,A[200000]={0},check[200000]={},move[200000]={0};

  long long K;

  x=scanf("%d %lld",&N,&K);

  for(i=0;i<N;i++)x=scanf("%d",&A[i]),check[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(check[i]<0)check[i]=j,move[j]=i;else{x=j-1,y=check[i],MOD=x-y+1;break;}

  if(K>x)K=(K-y)%MOD+y;

  return printf("%d\n",move[K]+1),0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[sz]={0},here[sz]={0},town[sz]={0};

inline LL IN(void)

{

  LL x=0;int f=1;char c=getchar();

  while((c<'0'||c>'9')&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();

  return f*x;

}

int main(void)

{

  int N,i,j,x;LL K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",town[K]+1),0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

int main(void)

{

  int N,i,j,A[200000]={0},check[200000]={},move[200000]={0};

  long long K;

  i=scanf("%d %lld",&N,&K);

  for(i=0;i<N;i++)check[i]=-scanf("%d",&A[i]);

  for(i=0,j=0;;i=A[i]-1,j++)if(check[i]<0)check[i]=j,move[j]=i;else break;

  if(K>=j)K=(K-check[i])%(check[i]-j)+check[i];

  return printf("%d\n",move[K]+1),0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[sz]={0},here[sz]={0},town[sz]={0};

inline LL IN(void)

{

  LL x=0;int f=1;char c=getchar();

  while((c<'0'||c>'9')&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();

  return f*x;

}

int main(void)

{

  int N,i,j,x;LL K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",town[K]+1),0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

int main(void)

{

  int N,i,j,x,A[200000]={0},check[200000]={0},move[200000]={0};

  long long K;

  i=scanf("%d %lld",&N,&K);

  for(i=0;i<N;i++)check[i]=-scanf("%d",&A[i]);

  for(i=0,j=0;;i=A[i]-1,j++)if(check[i]<0)check[i]=j,move[j]=i;else{x=check[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",move[K]+1),0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[sz]={0},here[sz]={0},town[sz]={0};

inline LL IN(void)

{

  LL x=0;int f=1;char c=getchar();

  while((c<'0'||c>'9')&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();

  return f*x;

}

int main(void)

{

  int N,i,j,x;LL K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",town[K]+1),0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<ctype.h>

long long IN(void)

{

  long long x=0,f=1;char c=getchar();

  while(!isdigit(c)&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(isdigit(c))x=x*10+c-48,c=getchar();

  return f*x;

}

void OUT(int x){if(x<0)putchar('-'),x=-x;if(x>=10)OUT(x/10);putchar(x%10+48);}

int main(void)

{

  int N,i,j,x,A[200000]={0},here[200000]={0},town[200000]={0};

  long long K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return OUT(town[K]+1),0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[sz]={0},here[sz]={0},town[sz]={0};

inline LL IN(void)

{

  LL x=0;int f=1;char c=getchar();

  while((c<'0'||c>'9')&&c!='-')c=getchar();

  if(c=='-')f=-f,c=getchar();

  while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();

  return f*x;

}

int main(void)

{

  int N,i,j,x;LL K;

  N=IN(),K=IN();

  for(i=0;i<N;i++)A[i]=IN(),here[i]=-1;

  for(i=0,j=0;;i=A[i]-1,j++)if(here[i]<0)here[i]=j,town[j]=i;else{x=here[i];break;}

  if(K>=j)K=(K-x)%(x-j)+x;

  return printf("%d\n",town[K]+1),0;

}

// Fastest known solution for this problem: