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

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

int main(void)

{

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

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.693446659275388x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

int main(void)

{

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

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.693410048265564x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

int main(void)

{

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

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.693127251490758x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

int main(void)

{

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

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.693318385589619x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

int main(void)

{

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

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.65455875398828x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

int main(void)

{

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

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.965813685210035x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

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)

{

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.965837577447223x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

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)

{

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.965800238168848x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

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)

{

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.96551181598248x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

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)

{

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.965706752133078x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

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)

{

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.926176111456918x):
#include<stdio.h>

typedef long long LL;

int N,i,j,x,y=0,A[200000],town[200000];LL K;

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)

{

  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{x=town[i];break;}

  while(K>0){if(i==y){K%=x-j;if(!K)return printf("%d",i+1),0;}y=A[y]-1,K--;}

  return printf("%d\n",y+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.954213593515474x):
#include<stdio.h>

typedef long long LL;

int N,i,j,p,A[200000],town[200000];LL K;

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)

{

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

typedef long long LL;

int N,i,j,p,A[200000],town[200000];LL K;

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)

{

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

typedef long long LL;

int N,i,j,p,A[200000],town[200000];LL K;

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)

{

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

typedef long long LL;

int N,i,j,p,A[200000],town[200000];LL K;

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)

{

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

typedef long long LL;

int N,i,j,p,A[200000],town[200000];LL K;

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)

{

  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>

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

typedef long long LL;

int N,i,j,p,A[200000],town[200000];LL K;

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)

{

  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.968135461972928x):
#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,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.968159358182133x):
#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,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.96812201269621x):
#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,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.967833542560474x):
#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,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.968028511118654x):
#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;

}