stablegradients/pie-gem5-pairs huggingface dataset:
// 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.92849129858603x):
#include<stdio.h>

typedef long long LL;

const int sz=2e5;

int A[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,p;LL K;

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

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

  return printf("%d\n",p+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.875941103537402x):
#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;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,p;LL K;

  N=IN(),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,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.875964842023645x):
#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;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,p;LL K;

  N=IN(),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,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.875927743030399x):
#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;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,p;LL K;

  N=IN(),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.87564117689389x):
#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;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,p;LL K;

  N=IN(),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.875834858594478x):
#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;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,p;LL K;

  N=IN(),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.836558604867252x):
#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;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,p;LL K;

  N=IN(),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,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.852213447840747x):
#include<stdio.h>

#include<stdlib.h>

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

typedef long long LL;

const int sz=1<<22;

inline char CH(void)

{

  static char buf[sz],*s1=buf,*s2=buf;

  return s1==s2&&(s2=(s1=buf)+fread(buf,1,sz,stdin),s1==s2)?EOF:*s1++;

}

inline LL IN(void)

{

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

  while(c<'0'||c>'9')f^=c=='-',c=CH();

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

  return f?-x:x;

}

signed main(void)

{

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

  N=IN(),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,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.852237145734527x):
#include<stdio.h>

#include<stdlib.h>

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

typedef long long LL;

const int sz=1<<22;

inline char CH(void)

{

  static char buf[sz],*s1=buf,*s2=buf;

  return s1==s2&&(s2=(s1=buf)+fread(buf,1,sz,stdin),s1==s2)?EOF:*s1++;

}

inline LL IN(void)

{

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

  while(c<'0'||c>'9')f^=c=='-',c=CH();

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

  return f?-x:x;

}

signed main(void)

{

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

  N=IN(),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,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.852200110180014x):
#include<stdio.h>

#include<stdlib.h>

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

typedef long long LL;

const int sz=1<<22;

inline char CH(void)

{

  static char buf[sz],*s1=buf,*s2=buf;

  return s1==s2&&(s2=(s1=buf)+fread(buf,1,sz,stdin),s1==s2)?EOF:*s1++;

}

inline LL IN(void)

{

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

  while(c<'0'||c>'9')f^=c=='-',c=CH();

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

  return f?-x:x;

}

signed main(void)

{

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

  N=IN(),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.851914034067393x):
#include<stdio.h>

#include<stdlib.h>

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

typedef long long LL;

const int sz=1<<22;

inline char CH(void)

{

  static char buf[sz],*s1=buf,*s2=buf;

  return s1==s2&&(s2=(s1=buf)+fread(buf,1,sz,stdin),s1==s2)?EOF:*s1++;

}

inline LL IN(void)

{

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

  while(c<'0'||c>'9')f^=c=='-',c=CH();

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

  return f?-x:x;

}

signed main(void)

{

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

  N=IN(),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.852107384575117x):
#include<stdio.h>

#include<stdlib.h>

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

typedef long long LL;

const int sz=1<<22;

inline char CH(void)

{

  static char buf[sz],*s1=buf,*s2=buf;

  return s1==s2&&(s2=(s1=buf)+fread(buf,1,sz,stdin),s1==s2)?EOF:*s1++;

}

inline LL IN(void)

{

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

  while(c<'0'||c>'9')f^=c=='-',c=CH();

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

  return f?-x:x;

}

signed main(void)

{

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

  N=IN(),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.812898292665494x):
#include<stdio.h>

#include<stdlib.h>

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

typedef long long LL;

const int sz=1<<22;

inline char CH(void)

{

  static char buf[sz],*s1=buf,*s2=buf;

  return s1==s2&&(s2=(s1=buf)+fread(buf,1,sz,stdin),s1==s2)?EOF:*s1++;

}

inline LL IN(void)

{

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

  while(c<'0'||c>'9')f^=c=='-',c=CH();

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

  return f?-x:x;

}

signed main(void)

{

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

  N=IN(),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,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.624818521363009x):
#include<stdio.h>

#include<stdlib.h>

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

signed main(void)

{

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

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

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

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

  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,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.624841830237315x):
#include<stdio.h>

#include<stdlib.h>

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

signed main(void)

{

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

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

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

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

  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,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.62480540265042x):
#include<stdio.h>

#include<stdlib.h>

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

signed main(void)

{

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

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

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

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

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

#include<stdlib.h>

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

signed main(void)

{

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

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

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

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

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

#include<stdlib.h>

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

signed main(void)

{

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

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

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

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

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

#include<stdlib.h>

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

signed main(void)

{

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

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

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

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

  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,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.873700953707079x):
#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,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.873724688360952x):
#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;

}