Problem:
Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i,n) for(lli i=0; i<n; i++)
#define loop(i, j, n) for(lli i=j; i<n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF  = 0x3f3f3f3f;
const long long mod=1e9+7;
//Function
lli combi(lli n, lli r)
{
	if(r==0 || r==n) return 1;
	else return combi(n-1,r) + combi(n-1,r-1);
}
lli sigma1(lli n){return n*(n+1)/2;}
lli sigma2(lli s, lli n){return sigma1(n) - sigma1(s -1);}
using namespace std;
// }}} End Header

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);
	lli n, k, cnt = 0, basho=0, ans;
	cin >> n >> k;
	vector<lli> a(n+1);
	map<lli, int> memo;
	loop(i, 1, n+1) cin >> a[i];

	int flag = 0;
	memo[1]++;
	lli old = 1;
	vector<lli> q(1,1);//初期設定

	rep(i,n+1){
		memo[a[old]]++;
		if(memo[a[old]] > 1){
			flag = 1;
			for(const auto& x : q){
				if(x == a[old]) break;
				basho++;
			}
		}
		if(flag == 1) break;
		q.push_back(a[old]);
		old = a[old];
	}//ループし始める要素の先頭になったら
	//その要素の過去に出た場所を特定する

	if(k < basho) ans = q[k];
	else if(basho == 0){
		if(k <= q.size()-1) ans = q[k];
		else{
			k -= (q.size());
			k %= q.size();
			ans = q[k];
		}
	}else{
		k -= (basho);
		lli youso = q.size()-basho;
		k %= youso;
		ans = q[basho+k];
	}
	cout << ans << endl;
	return 0;
}