stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 13.873687595357016x):
#include<stdio.h>

#include<stdlib.h>

#define array(N,t) (t*)calloc(N,sizeof(t))

typedef long long LL;

inline LL IN(void)

{

  LL x=0;short f=0,c=getchar();

  while(c<48||c>57)f^=c==45,c=getchar();

  while(c>47&&c<58)x=x*10+c-48,c=getchar();

  return f?-x:x;

}

signed main(void)

{

  int N=IN(),i,j,p;LL K=IN();

  int *A=array(N,int),*town=array(N,int);

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

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

  for(p=0;K>0;p=A[p]-1,K--)if(i==p){K%=town[i]-j;if(!K)break;}

  free(A),free(town);

  return printf("%d\n",p+1),0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 13.87340107548411x):
#include<stdio.h>

#include<stdlib.h>

#define array(N,t) (t*)calloc(N,sizeof(t))

typedef long long LL;

inline LL IN(void)

{

  LL x=0;short f=0,c=getchar();

  while(c<48||c>57)f^=c==45,c=getchar();

  while(c>47&&c<58)x=x*10+c-48,c=getchar();

  return f?-x:x;

}

signed main(void)

{

  int N=IN(),i,j,p;LL K=IN();

  int *A=array(N,int),*town=array(N,int);

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

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

  for(p=0;K>0;p=A[p]-1,K--)if(i==p){K%=town[i]-j;if(!K)break;}

  free(A),free(town);

  return printf("%d\n",p+1),0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 13.873594725916478x):
#include<stdio.h>

#include<stdlib.h>

#define array(N,t) (t*)calloc(N,sizeof(t))

typedef long long LL;

inline LL IN(void)

{

  LL x=0;short f=0,c=getchar();

  while(c<48||c>57)f^=c==45,c=getchar();

  while(c>47&&c<58)x=x*10+c-48,c=getchar();

  return f?-x:x;

}

signed main(void)

{

  int N=IN(),i,j,p;LL K=IN();

  int *A=array(N,int),*town=array(N,int);

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

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

  for(p=0;K>0;p=A[p]-1,K--)if(i==p){K%=town[i]-j;if(!K)break;}

  free(A),free(town);

  return printf("%d\n",p+1),0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 13.834324812998313x):
#include<stdio.h>

#include<stdlib.h>

#define array(N,t) (t*)calloc(N,sizeof(t))

typedef long long LL;

inline LL IN(void)

{

  LL x=0;short f=0,c=getchar();

  while(c<48||c>57)f^=c==45,c=getchar();

  while(c>47&&c<58)x=x*10+c-48,c=getchar();

  return f?-x:x;

}

signed main(void)

{

  int N=IN(),i,j,p;LL K=IN();

  int *A=array(N,int),*town=array(N,int);

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

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

  for(p=0;K>0;p=A[p]-1,K--)if(i==p){K%=town[i]-j;if(!K)break;}

  free(A),free(town);

  return printf("%d\n",p+1),0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<stdio.h>

#include<stdlib.h>

#include<set>

#include<map>

#include<vector>

#include<queue>

#include<cstring>

using namespace std;

#define mp(a,b) make_pair(a,b)

#define pb push_back



int main(){

    long long n,k;

    cin>>n>>k;

    int a[200010], b[200010], c[200010];

    int cnt = 0;

    int start = 1;

    int over = 1;

    memset(a,0,sizeof(a));

    memset(b,0,sizeof(b));

    memset(c,-1,sizeof(c));

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

        cin>>a[i];

    }

    b[0] = 1;

    c[1] = 0;

    while (true){

        //cout<<a[b[cnt]]<<" "<<cnt<<" "<<b[cnt]<<endl;

        long long cur = a[b[cnt]];

        if (c[cur] >= 0){

            start = c[cur];

            over  = cnt;

            break;

        }

        cnt++;

        b[cnt] = cur;

        c[cur] = cnt;

    }

    int total = over - start + 1;

    int ex = (k - start +1) % total;

    //cout<<start<<" "<<over<<" "<<ex<<endl;

    if (k <= over){

         cout<<b[k];

    }else{

        if (ex == 0){

            cout<<b[over];

        }else{

            cout<<b[start - 1 + ex];

        }

    }





//    system("pause");

}


// Faster C++ solution for the same problem (measured speedup: 3.4004992089047783x):
#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef long double ld;

typedef pair<int, int> pdd;



#define INF 0x7f7f7f

#define mem(a,b) memset(a , b , sizeof(a))

#define FOR(i, x, n) for(int i = x;i <= n; i++)

// const ll mod = 1e9 + 7;

// const int maxn = 1e5 + 10;

// const double eps = 1e-6;



ll town[200005];

int vis[200005];

ll rul1[200005];

ll rul2[200005];



ll n, k;



void solve()

{

    ll cnt1 = 1;

    ll cnt2 = 1;

    cin >> n >> k;

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

    {

        cin >> town[i];

    }

    ll ans = 1;

    ll m = k;

    while(m--) // 判断循环一次需要传送多少

    {

        ans = town[ans]; // 传送

        if(vis[ans] > 2)

            break;

        if(vis[ans] == 0)

            rul1[cnt1++] = ans;

        else if(vis[ans] == 1)

            rul2[cnt2++] = ans;

        vis[ans]++;

    }

    cnt1--;

    cnt2--;

    if(k <= cnt1)

        cout<<rul1[k] << endl;

    else if(cnt2 == -1 && k > cnt1)

    {

        ll a = k % cnt1;

        if(!a) a = cnt1;

    }

    else if(cnt2 != -1 && k > cnt1)

    {

        k -= cnt1;

        ll a = k % cnt2;

        if(!a)

            a = cnt2;

        cout << rul2[a] << endl;

    }

}



signed main()

{

    ios_base::sync_with_stdio(false);

    //cin.tie(nullptr);

    //cout.tie(nullptr);

#ifdef FZT_ACM_LOCAL

    freopen("in.txt", "r", stdin);

    freopen("out.txt", "w", stdout);

#else

    ios::sync_with_stdio(false);

    int T = 1;

    //cin >> T;

    while(T--)

        solve();

#endif

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<stdio.h>

#include<stdlib.h>

#include<set>

#include<map>

#include<vector>

#include<queue>

#include<cstring>

using namespace std;

#define mp(a,b) make_pair(a,b)

#define pb push_back



int main(){

    long long n,k;

    cin>>n>>k;

    int a[200010], b[200010], c[200010];

    int cnt = 0;

    int start = 1;

    int over = 1;

    memset(a,0,sizeof(a));

    memset(b,0,sizeof(b));

    memset(c,-1,sizeof(c));

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

        cin>>a[i];

    }

    b[0] = 1;

    c[1] = 0;

    while (true){

        //cout<<a[b[cnt]]<<" "<<cnt<<" "<<b[cnt]<<endl;

        long long cur = a[b[cnt]];

        if (c[cur] >= 0){

            start = c[cur];

            over  = cnt;

            break;

        }

        cnt++;

        b[cnt] = cur;

        c[cur] = cnt;

    }

    int total = over - start + 1;

    int ex = (k - start +1) % total;

    //cout<<start<<" "<<over<<" "<<ex<<endl;

    if (k <= over){

         cout<<b[k];

    }else{

        if (ex == 0){

            cout<<b[over];

        }else{

            cout<<b[start - 1 + ex];

        }

    }





//    system("pause");

}


// Faster C++ solution for the same problem (measured speedup: 3.4376936867826364x):
#include<bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 200010;

ll a[N];

bool vis[N];



vector<ll> vec;



int main(){

    ll n, k;

	cin >> n >> k;



	for (int i = 0; i < n; i++) cin >> a[i];



    ll buffer = 1;



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

		vis[buffer] = 1;

        vec.push_back(buffer);

		buffer = a[buffer - 1];

        if (vis[buffer] == 1) {

            break;

        }

    }



    auto it = find(vec.begin(), vec.end(), buffer);



    ll Length = vec.end() - it;



    ll step = it - vec.begin();



    ll now;

    if (k > step) {

        now = (k - step) % Length;

    } else {

        now = (k - step);

    }

    printf("%lld\n", vec[step + now]);

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<stdio.h>

#include<stdlib.h>

#include<set>

#include<map>

#include<vector>

#include<queue>

#include<cstring>

using namespace std;

#define mp(a,b) make_pair(a,b)

#define pb push_back



int main(){

    long long n,k;

    cin>>n>>k;

    int a[200010], b[200010], c[200010];

    int cnt = 0;

    int start = 1;

    int over = 1;

    memset(a,0,sizeof(a));

    memset(b,0,sizeof(b));

    memset(c,-1,sizeof(c));

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

        cin>>a[i];

    }

    b[0] = 1;

    c[1] = 0;

    while (true){

        //cout<<a[b[cnt]]<<" "<<cnt<<" "<<b[cnt]<<endl;

        long long cur = a[b[cnt]];

        if (c[cur] >= 0){

            start = c[cur];

            over  = cnt;

            break;

        }

        cnt++;

        b[cnt] = cur;

        c[cur] = cnt;

    }

    int total = over - start + 1;

    int ex = (k - start +1) % total;

    //cout<<start<<" "<<over<<" "<<ex<<endl;

    if (k <= over){

         cout<<b[k];

    }else{

        if (ex == 0){

            cout<<b[over];

        }else{

            cout<<b[start - 1 + ex];

        }

    }





//    system("pause");

}


// Faster C++ solution for the same problem (measured speedup: 3.3559829581323775x):
#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef long double ld;

typedef pair<int, int> pdd;



#define INF 0x7f7f7f

#define mem(a,b) memset(a , b , sizeof(a))

#define FOR(i, x, n) for(int i = x;i <= n; i++)

// const ll mod = 1e9 + 7;

// const int maxn = 1e5 + 10;

// const double eps = 1e-6;



const int maxn = 2e5 + 10;

int a[maxn], vis[maxn];

int ans, pos;

vector<int> v;



void dfs(int u)

{

    ans++;

    vis[u] = ans;

    v.push_back(u);

    if(!vis[a[u]])

        dfs(a[u]);

    else

    {

        pos = vis[a[u]];

        v.push_back(a[u]);

        return ;

    }

}



void solve()

{

    ll n, k;

    cin >> n >> k;

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

        cin >> a[i];

    dfs(1);

    if(k < v.size())

        cout << v[k] << endl;

    else

    {

        k -= v.size();

        ll len = v.size() - pos;

        cout << v[pos + k % len] << endl;

    }

}



signed main()

{

    ios_base::sync_with_stdio(false);

    //cin.tie(nullptr);

    //cout.tie(nullptr);

#ifdef FZT_ACM_LOCAL

    freopen("in.txt", "r", stdin);

    freopen("out.txt", "w", stdout);

#else

    ios::sync_with_stdio(false);

    int T = 1;

    //cin >> T;

    while(T--)

        solve();

#endif

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<stdio.h>

#include<stdlib.h>

#include<set>

#include<map>

#include<vector>

#include<queue>

#include<cstring>

using namespace std;

#define mp(a,b) make_pair(a,b)

#define pb push_back



int main(){

    long long n,k;

    cin>>n>>k;

    int a[200010], b[200010], c[200010];

    int cnt = 0;

    int start = 1;

    int over = 1;

    memset(a,0,sizeof(a));

    memset(b,0,sizeof(b));

    memset(c,-1,sizeof(c));

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

        cin>>a[i];

    }

    b[0] = 1;

    c[1] = 0;

    while (true){

        //cout<<a[b[cnt]]<<" "<<cnt<<" "<<b[cnt]<<endl;

        long long cur = a[b[cnt]];

        if (c[cur] >= 0){

            start = c[cur];

            over  = cnt;

            break;

        }

        cnt++;

        b[cnt] = cur;

        c[cur] = cnt;

    }

    int total = over - start + 1;

    int ex = (k - start +1) % total;

    //cout<<start<<" "<<over<<" "<<ex<<endl;

    if (k <= over){

         cout<<b[k];

    }else{

        if (ex == 0){

            cout<<b[over];

        }else{

            cout<<b[start - 1 + ex];

        }

    }





//    system("pause");

}


// Faster C++ solution for the same problem (measured speedup: 3.4401893218449384x):
#include<bits/stdc++.h>

using namespace std;

long long n,k,a[200050],tag[200050],cnt;

bool vis[200050];

int main()

{

	cin>>n>>k;

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

		cin>>a[i],a[i]--;

	long long now=0,len=1;

	while(vis[now]!=1)

	{

		vis[now]=1;

		tag[now]=++cnt;

		if(cnt>k)

		{

			cout<<now+1;

			return 0;

		}

		now=a[now];

		len++;

	}

	k-=tag[now];

	k++;

	len=cnt-tag[now]+1;

//	cout<<now<<' '<<len<<' ';

	k%=len;

	for(int i=0;i<k;i++)

	{

		now=a[now];

	}

	cout<<now+1;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<stdio.h>

#include<stdlib.h>

#include<set>

#include<map>

#include<vector>

#include<queue>

#include<cstring>

using namespace std;

#define mp(a,b) make_pair(a,b)

#define pb push_back



int main(){

    long long n,k;

    cin>>n>>k;

    int a[200010], b[200010], c[200010];

    int cnt = 0;

    int start = 1;

    int over = 1;

    memset(a,0,sizeof(a));

    memset(b,0,sizeof(b));

    memset(c,-1,sizeof(c));

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

        cin>>a[i];

    }

    b[0] = 1;

    c[1] = 0;

    while (true){

        //cout<<a[b[cnt]]<<" "<<cnt<<" "<<b[cnt]<<endl;

        long long cur = a[b[cnt]];

        if (c[cur] >= 0){

            start = c[cur];

            over  = cnt;

            break;

        }

        cnt++;

        b[cnt] = cur;

        c[cur] = cnt;

    }

    int total = over - start + 1;

    int ex = (k - start +1) % total;

    //cout<<start<<" "<<over<<" "<<ex<<endl;

    if (k <= over){

         cout<<b[k];

    }else{

        if (ex == 0){

            cout<<b[over];

        }else{

            cout<<b[start - 1 + ex];

        }

    }





//    system("pause");

}


// Faster C++ solution for the same problem (measured speedup: 3.424004909592343x):
#include<bits/stdc++.h>

using namespace std;

#define int long long 

int32_t main()

{

    int n,k;

    cin>>n>>k;

    int a[n+1];

    int vis[n+1];

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

    {

        cin>>a[i];

        vis[i]=0;

    }

    vector<int>v;

    v.push_back(1);

    int x=1;

    vis[1]=1;

    int u;

    while(1)

    {

        if(vis[a[x]]==0)

        {

            v.push_back(a[x]);

            vis[a[x]]=1;

            x=a[x];

            //cout<<x<<"\n";

        }

        else

        {

            u=a[x];

            break;

        }

    }

    int z=v.size();

    int f;

    for(int i=0;i<z;i++)

    {

        if(v[i]==u)

        {

            f=i;

            break;

        }

    }

    if(k<z)

    cout<<v[k]<<"\n";

    else

    {

        k-=z;

        z-=f;

        k%=z;

        cout<<v[f+k]<<"\n";

    }

    

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

vector<int> Edge[200005],vec;

int mark[200005];

void dfs(int u)

{

    vec.push_back(u);

    for(int i=0; i<Edge[u].size(); i++)

    {

        int v=Edge[u][i];

        if(mark[v]==0)

        {

            mark[v]=1;

            dfs(v);

        }

    }

}

int main()

{

    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    ll n,k;

    cin>>n>>k;

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

    {

        ll v;

        cin>>v;

        Edge[i].push_back(v);

    }

    mark[1]=1;

    dfs(1);

    ll len=vec.size();

    if(k<len)

    {

        cout<<vec[k]<<endl;

        return 0;

    }

    ll idx=Edge[vec[len-1]][0];

    memset(mark,0,sizeof(mark));

    vec.clear();

    k-=(len-1);

    mark[idx]=1;

    dfs(idx);

    len=vec.size();

    idx=k%len;

    idx--;

    if(idx==-1)

        idx=len-1;

    cout<<vec[idx]<<endl;





    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.716850766092707x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll inf = 1e9;

#define Fast    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define sz      200005



ll arr[sz];

int brr[sz];





int main(){

    Fast;

    ll n,k,i;



    cin>>n>>k;

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

        cin>>arr[i];

    }

    memset(brr,0,sizeof(brr));



    i=1;

    while(k && brr[i]==0){

        k--;

        brr[i]=1;

        i=arr[i];

    }

    if(k==0){

        cout<<i<<endl;

        return 0;

    }



    ll cnt=0;

    memset(brr,0,sizeof(brr));

    while(k && brr[i]==0){

        k--;

        cnt++;

        brr[i]=1;

        i=arr[i];

    }

    if(k==0){

        cout<<i<<endl;

        return 0;

    }

    k=k%cnt;

    memset(brr,0,sizeof(brr));

    while(k && brr[i]==0){

        k--;

        brr[i]=1;

        i=arr[i];

    }

    cout<<i<<endl;



    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

vector<int> Edge[200005],vec;

int mark[200005];

void dfs(int u)

{

    vec.push_back(u);

    for(int i=0; i<Edge[u].size(); i++)

    {

        int v=Edge[u][i];

        if(mark[v]==0)

        {

            mark[v]=1;

            dfs(v);

        }

    }

}

int main()

{

    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    ll n,k;

    cin>>n>>k;

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

    {

        ll v;

        cin>>v;

        Edge[i].push_back(v);

    }

    mark[1]=1;

    dfs(1);

    ll len=vec.size();

    if(k<len)

    {

        cout<<vec[k]<<endl;

        return 0;

    }

    ll idx=Edge[vec[len-1]][0];

    memset(mark,0,sizeof(mark));

    vec.clear();

    k-=(len-1);

    mark[idx]=1;

    dfs(idx);

    len=vec.size();

    idx=k%len;

    idx--;

    if(idx==-1)

        idx=len-1;

    cout<<vec[idx]<<endl;





    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 7.235252291029829x):
#include<bits/stdc++.h>

#define int long long int

using namespace std;

int n,m;

bool check(int x, int y){

    if(x >=0 and y >=0 and x < n and y < m) return true;

    return false;

}

signed main(){

    ios_base::sync_with_stdio(false);

   // freopen("input.txt", "r", stdin);

    int n, k; cin>>n>>k;

    int a[n+1];

    for(int i=1; i<=n; i++) cin>>a[i];

        //for(int i=0; i<n; i++) cout<<a[i]<<" "; cout<<endl;

    vector<int> dp(n+1, 0);

    int i=1, c=0; 

    for(; c <k; c++){

        if(dp[i]==0){

            dp[i]=1;

            i=a[i];

            //cout<<i<<" d"<<endl;

        }

        else break;

    }

       

    if(c==k) cout<<i<<endl;

    else{

        vector<int> d(n+1,0);

        int j=i, count=0;

        //d[j]=1;

        for(int z=0; z < n; z++){

            if(d[j]==0){

                d[j]=1;

                count++;

                j=a[j];

            }            

        }

        k -=(c-count);

        //int mod=c-j;

        k = (k%count);

        int e=0; 

        while(e<k){

            e++;

            i=a[i];

        }

        cout<<i<<endl;



    }

    

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll inf = 1e9;

#define Fast    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define sz      200005



ll arr[sz];

int brr[sz];





int main(){

    Fast;

    ll n,k,i;



    cin>>n>>k;

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

        cin>>arr[i];

    }

    memset(brr,0,sizeof(brr));



    i=1;

    while(k && brr[i]==0){

        k--;

        brr[i]=1;

        i=arr[i];

    }

    if(k==0){

        cout<<i<<endl;

        return 0;

    }



    ll cnt=0;

    memset(brr,0,sizeof(brr));

    while(k && brr[i]==0){

        k--;

        cnt++;

        brr[i]=1;

        i=arr[i];

    }

    if(k==0){

        cout<<i<<endl;

        return 0;

    }

    k=k%cnt;

    memset(brr,0,sizeof(brr));

    while(k && brr[i]==0){

        k--;

        brr[i]=1;

        i=arr[i];

    }

    cout<<i<<endl;



    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.9466082300193603x):
#include<bits/stdc++.h>

#define int long long int

using namespace std;

int n,m;

bool check(int x, int y){

    if(x >=0 and y >=0 and x < n and y < m) return true;

    return false;

}

signed main(){

    ios_base::sync_with_stdio(false);

   // freopen("input.txt", "r", stdin);

    int n, k; cin>>n>>k;

    int a[n+1];

    for(int i=1; i<=n; i++) cin>>a[i];

        //for(int i=0; i<n; i++) cout<<a[i]<<" "; cout<<endl;

    vector<int> dp(n+1, 0);

    int i=1, c=0; 

    for(; c <k; c++){

        if(dp[i]==0){

            dp[i]=1;

            i=a[i];

            //cout<<i<<" d"<<endl;

        }

        else break;

    }

       

    if(c==k) cout<<i<<endl;

    else{

        vector<int> d(n+1,0);

        int j=i, count=0;

        //d[j]=1;

        for(int z=0; z < n; z++){

            if(d[j]==0){

                d[j]=1;

                count++;

                j=a[j];

            }            

        }

        k -=(c-count);

        //int mod=c-j;

        k = (k%count);

        int e=0; 

        while(e<k){

            e++;

            i=a[i];

        }

        cout<<i<<endl;



    }

    

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

vector<int> Edge[200005],vec;

int mark[200005];

void dfs(int u)

{

    vec.push_back(u);

    for(int i=0; i<Edge[u].size(); i++)

    {

        int v=Edge[u][i];

        if(mark[v]==0)

        {

            mark[v]=1;

            dfs(v);

        }

    }

}

int main()

{

    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    ll n,k;

    cin>>n>>k;

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

    {

        ll v;

        cin>>v;

        Edge[i].push_back(v);

    }

    mark[1]=1;

    dfs(1);

    ll len=vec.size();

    if(k<len)

    {

        cout<<vec[k]<<endl;

        return 0;

    }

    ll idx=Edge[vec[len-1]][0];

    memset(mark,0,sizeof(mark));

    vec.clear();

    k-=(len-1);

    mark[idx]=1;

    dfs(idx);

    len=vec.size();

    idx=k%len;

    idx--;

    if(idx==-1)

        idx=len-1;

    cout<<vec[idx]<<endl;





    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 7.2911083922252375x):
#include <bits/stdc++.h>

using namespace std;



int main() 

{

int n;

long long int k;

cin>>n>>k;

vector<int>visit(n+1);

vector<int>arr(n+1);

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

{

      cin>>arr[i];    

}

int pos=1;

int len=0;

while(visit[pos]==0)

{     

          visit[pos]=1;

          pos=arr[pos];

          len++;

}

int pos1=arr[pos];

int cycle=1;

while(pos1!=pos)

{

          pos1=arr[pos1];

          cycle++;

}

 

int j;

if(k<=(len-cycle))

{

          j=k;

          pos=1;

}

else

{

          k=(k-(len-cycle));

          j=k%cycle;

}

 

while(j--)

{

          pos=arr[pos];

}

cout<<pos<<endl;

}