#include<bits/stdc++.h>
using namespace std;
const int MAXN=5e2+5;
int n,k,a[MAXN][MAXN],s[MAXN*MAXN+5],s2[MAXN*MAXN+5];
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
if(k==1){
cout<<1;
return 0;
}
int tmp=n*n+1,pt=1,ch=1,to=2;
while(tmp--){
s[pt]=ch;
int temp=ch;
ch=to;
to=a[to][temp];
pt++;
}
if(k<=n*n+1){
cout<<s[k]<<endl;
return 0;
}
int pt2=1,che=ch,ton=to;
do
{
s2[pt2]=che;
int temp=che;
che=ton;
ton=a[ton][temp];
pt2++;
}while(che!=ch||ton!=to);
s2[pt2]=che;
int temp=che;
che=ton;
ton=a[ton][temp];
int tmp2=k-n*n-1;
cout<<s2[tmp2%pt2];
return 0;
}