Wormhole Sort

소가 놓인 위치의 순열과 너비가 있는 웜홀이 주어질 때, 모든 소를 제자리로 보내면서 사용하는 웜홀 너비의 최솟값을 최대로 만들고, 웜홀이 필요 없으면 -1을 출력한다.

어려움8유니온 파인드그래프정렬그리디아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Farmer John's cows have grown tired of his daily request that they sort themselves before leaving the barn each morning. They have just completed their PhDs in quantum physics, and are ready to speed things up a bit.

This morning, as usual, Farmer John's NN cows (1N1051 \leq N \leq 10^5), conveniently numbered 1N1 \dots N, are scattered throughout the barn at NN distinct locations, also numbered 1N1 \dots N, such that cow ii is at location p_ip\_i. But this morning there are also MM wormholes (1M1051 \leq M \leq 10^5), numbered 1M1 \dots M, where wormhole ii bidirectionally connects location a_ia\_i with location b_ib\_i, and has a width w_iw\_i (1a_i,b_iN,a_ib_i,1w_i1091\le a\_i,b\_i\le N, a\_i\neq b\_i, 1\le w\_i\le 10^9).

At any point in time, two cows located at opposite ends of a wormhole may choose to simultaneously swap places through the wormhole. The cows must perform such swaps until cow ii is at location ii for 1iN1 \leq i \leq N.

The cows are not eager to get squished by the wormholes. Help them maximize the width of the least wide wormhole which they must use to sort themselves. It is guaranteed that it is possible for the cows to sort themselves.

입력

The first line contains two integers, NN and MM.

The second line contains the NN integers p_1,p_2,,p_Np\_1, p\_2, \dots, p\_N. It is guaranteed that pp is a permutation of 1N.1\ldots N.

For each ii between 11 and MM, line i+2i+2 contains the integers a_ia\_i, b_ib\_i, and w_iw\_i.

출력

A single integer: the maximum minimal wormhole width which a cow must squish itself into during the sorting process. If the cows do not need any wormholes to sort themselves, output 1-1.