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=j-check[i];break;}

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

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

}

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

#include<ctype.h>

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

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

  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;

}
// 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.661036677010419x):
#include<stdio.h>

#include<ctype.h>

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

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

  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;

}
// 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.660754548811587x):
#include<stdio.h>

#include<ctype.h>

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

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

  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;

}
// 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.66094523103943x):
#include<stdio.h>

#include<ctype.h>

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

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

  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;

}
// 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.622277233292559x):
#include<stdio.h>

#include<ctype.h>

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

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

  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;

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

}

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

#include<ctype.h>

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

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

  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;

}
// 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=j-check[i];break;}

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

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

}

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

#include<ctype.h>

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

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

  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;

}
// 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.682034923697046x):
#include<stdio.h>

#include<ctype.h>

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

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

  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;

}
// 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.681752361841703x):
#include<stdio.h>

#include<ctype.h>

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

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

  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;

}
// 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.681943337165343x):
#include<stdio.h>

#include<ctype.h>

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

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

  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;

}
// 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.643215903215003x):
#include<stdio.h>

#include<ctype.h>

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

inline int IN(void)

{

  int 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;

}

int main(void)

{

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

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

  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;

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

}

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

#include<ctype.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,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;

}

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;

}
// 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=j-check[i];break;}

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

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

}

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

#include<ctype.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,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;

}

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;

}
// 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.971139782883615x):
#include<stdio.h>

#include<ctype.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,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;

}

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;

}
// 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.970851250424786x):
#include<stdio.h>

#include<ctype.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,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;

}

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;

}
// 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.971046261105329x):
#include<stdio.h>

#include<ctype.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,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;

}

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;

}
// 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.931500506678441x):
#include<stdio.h>

#include<ctype.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,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;

}

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;

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

}

// Faster C++ solution for the same problem (measured speedup: 13.97653285869411x):
#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;

}
// 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=j-check[i];break;}

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

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

}

// Faster C++ solution for the same problem (measured speedup: 13.976556769269296x):
#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;

}
// 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.976519401331924x):
#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;

}
// 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.97623075777303x):
#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;

}
// 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.976425843542868x):
#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;

}