#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test() int t;cin>>t;for(int test=1;test<=t;test++)
#define pb push_back
#define nl cout<<"\n"
#define F first
#define S second
#define all(x) x.begin(),x.end()
template<class C> void min_self( C &a, C b ){ a = min(a,b); }
template<class C> void max_self( C &a, C b ){ a = max(a,b); }
const ll MOD = 1000000007;
ll mod( ll n, ll m=MOD ){ n%=m,n+=m,n%=m;return n; }
const int MAXN = 1e5+5;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
class DSU
{
public:
vector<int>rt,sz;
int components;
DSU( int n )
{
rt.resize(n+5);
sz.resize(n+5);
components = n;
for(int i=0;i<n;i++)
{
rt[i] = i;
sz[i] = 1;
}
}
int get_components()
{
return components;
}
int root( int x )
{
while( x != rt[x] )
{
rt[x] = rt[rt[x]];
x = rt[x];
}
return x;
}
void connect( int x, int y )
{
int r1 = root(x);
int r2 = root(y);
if( r1 == r2 )
return;
if( sz[r1] < sz[r2] )
swap(r1,r2);
sz[r1] += sz[r2];
rt[r2] = r1;
components--;
}
};
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("../input.txt", "r", stdin);
// freopen("../output.txt", "w", stdout);
// #endif
// freopen("wormsort.in", "r", stdin);
// freopen("wormsort.out", "w", stdout);
fastio();
int n,m;
cin>>n>>m;
vector<int>p(n);
set<int>unmatched;
DSU d(2*n);
for(int i=0;i<n;i++)
{
cin>>p[i];
p[i]--;
if( p[i] != i )
{
d.connect(i+n,p[i]);
unmatched.insert(i);
}
}
vector<vector<int>>edges(m,vector<int>(3,0));
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
u--,v--;
edges.pb({w,u,v});
}
sort(all(edges),greater<vector<int>>());
if( unmatched.empty() )
{
cout<<-1,nl;
return 0;
}
for(int i=0;i<m;i++)
{
int w = edges[i][0], u = edges[i][1], v = edges[i][2];
d.connect(u,v);
if( d.root(u) == d.root(u+n) )
unmatched.erase(u);
if( d.root(v) == d.root(v+n) )
unmatched.erase(v);
for( auto x : unmatched )
{
if( d.root(x) == d.root(x+n) )
unmatched.erase(x);
}
if( unmatched.empty() )
{
cout<<w,nl;
return 0;
}
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyB1bGw7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIGxvbmcgZG91YmxlIGxkOwp0eXBlZGVmIHBhaXI8aW50LGludD4gcGlpOwp0eXBlZGVmIHBhaXI8bGwsbGw+IHBsbDsKCiNkZWZpbmUgZmFzdGlvKCkgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKDApO2NvdXQudGllKDApCiNkZWZpbmUgdGVzdCgpIGludCB0O2Npbj4+dDtmb3IoaW50IHRlc3Q9MTt0ZXN0PD10O3Rlc3QrKykKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBubCBjb3V0PDwiXG4iCiNkZWZpbmUgRiBmaXJzdAojZGVmaW5lIFMgc2Vjb25kCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSx4LmVuZCgpCgp0ZW1wbGF0ZTxjbGFzcyBDPiB2b2lkIG1pbl9zZWxmKCBDICZhLCBDIGIgKXsgYSA9IG1pbihhLGIpOyB9CnRlbXBsYXRlPGNsYXNzIEM+IHZvaWQgbWF4X3NlbGYoIEMgJmEsIEMgYiApeyBhID0gbWF4KGEsYik7IH0KCmNvbnN0IGxsIE1PRCA9IDEwMDAwMDAwMDc7CmxsIG1vZCggbGwgbiwgbGwgbT1NT0QgKXsgbiU9bSxuKz1tLG4lPW07cmV0dXJuIG47IH0KCmNvbnN0IGludCBNQVhOID0gMWU1KzU7CmNvbnN0IGludCBMT0dOID0gMjE7CmNvbnN0IGxsIElORiA9IDFlMTQ7CmludCBkeFtdID0gezEsMCwtMSwwfTsKaW50IGR5W10gPSB7MCwxLDAsLTF9OwoKCmNsYXNzIERTVQp7CnB1YmxpYzoKICAgIHZlY3RvcjxpbnQ+cnQsc3o7CiAgICBpbnQgY29tcG9uZW50czsKICAgIERTVSggaW50IG4gKQogICAgewogICAgICAgIHJ0LnJlc2l6ZShuKzUpOwogICAgICAgIHN6LnJlc2l6ZShuKzUpOwogICAgICAgIGNvbXBvbmVudHMgPSBuOwogICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspCiAgICAgICAgewogICAgICAgICAgICBydFtpXSA9IGk7CiAgICAgICAgICAgIHN6W2ldID0gMTsKICAgICAgICB9CiAgICB9CiAgICBpbnQgZ2V0X2NvbXBvbmVudHMoKQogICAgewogICAgICAgIHJldHVybiBjb21wb25lbnRzOwogICAgfQogICAgaW50IHJvb3QoIGludCB4ICkKICAgIHsKICAgICAgICB3aGlsZSggeCAhPSBydFt4XSApCiAgICAgICAgewogICAgICAgICAgICBydFt4XSA9IHJ0W3J0W3hdXTsKICAgICAgICAgICAgeCA9IHJ0W3hdOwogICAgICAgIH0KICAgICAgICByZXR1cm4geDsKICAgIH0KICAgIHZvaWQgY29ubmVjdCggaW50IHgsIGludCB5ICkKICAgIHsKICAgICAgICBpbnQgcjEgPSByb290KHgpOwogICAgICAgIGludCByMiA9IHJvb3QoeSk7CiAgICAgICAgaWYoIHIxID09IHIyICkKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIGlmKCBzeltyMV0gPCBzeltyMl0gKQogICAgICAgICAgICBzd2FwKHIxLHIyKTsKICAgICAgICBzeltyMV0gKz0gc3pbcjJdOwogICAgICAgIHJ0W3IyXSA9IHIxOwogICAgICAgIGNvbXBvbmVudHMtLTsKICAgIH0KfTsKCgppbnQgbWFpbigpIAp7CiAgICAvLyAjaWZuZGVmIE9OTElORV9KVURHRQogICAgICAgIC8vIGZyZW9wZW4oIi4uL2lucHV0LnR4dCIsICJyIiwgc3RkaW4pOwogICAgICAgIC8vIGZyZW9wZW4oIi4uL291dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CiAgICAvLyAjZW5kaWYKICAgIC8vIGZyZW9wZW4oIndvcm1zb3J0LmluIiwgInIiLCBzdGRpbik7CiAgICAvLyBmcmVvcGVuKCJ3b3Jtc29ydC5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICBmYXN0aW8oKTsKCiAgICBpbnQgbixtOwogICAgY2luPj5uPj5tOwogICAgdmVjdG9yPGludD5wKG4pOwogICAgc2V0PGludD51bm1hdGNoZWQ7CiAgICBEU1UgZCgyKm4pOwogICAgZm9yKGludCBpPTA7aTxuO2krKykKICAgIHsKICAgICAgICBjaW4+PnBbaV07CiAgICAgICAgcFtpXS0tOwogICAgICAgIGlmKCBwW2ldICE9IGkgKQogICAgICAgIHsKICAgICAgICAgICAgZC5jb25uZWN0KGkrbixwW2ldKTsKICAgICAgICAgICAgdW5tYXRjaGVkLmluc2VydChpKTsKICAgICAgICB9CiAgICB9CgogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PmVkZ2VzKG0sdmVjdG9yPGludD4oMywwKSk7CiAgICBmb3IoaW50IGk9MDtpPG07aSsrKQogICAgewogICAgICAgIGludCB1LHYsdzsKICAgICAgICBjaW4+PnU+PnY+Pnc7CiAgICAgICAgdS0tLHYtLTsKICAgICAgICBlZGdlcy5wYih7dyx1LHZ9KTsKICAgIH0KICAgIHNvcnQoYWxsKGVkZ2VzKSxncmVhdGVyPHZlY3RvcjxpbnQ+PigpKTsKCiAgICBpZiggdW5tYXRjaGVkLmVtcHR5KCkgKQogICAgewogICAgICAgIGNvdXQ8PC0xLG5sOwogICAgICAgIHJldHVybiAwOwogICAgfQoKICAgIGZvcihpbnQgaT0wO2k8bTtpKyspCiAgICB7CiAgICAgICAgaW50IHcgPSBlZGdlc1tpXVswXSwgdSA9IGVkZ2VzW2ldWzFdLCB2ID0gZWRnZXNbaV1bMl07CgogICAgICAgIGQuY29ubmVjdCh1LHYpOwogICAgICAgIGlmKCBkLnJvb3QodSkgPT0gZC5yb290KHUrbikgKQogICAgICAgICAgICB1bm1hdGNoZWQuZXJhc2UodSk7CiAgICAgICAgaWYoIGQucm9vdCh2KSA9PSBkLnJvb3QodituKSApCiAgICAgICAgICAgIHVubWF0Y2hlZC5lcmFzZSh2KTsKCiAgICAgICAgZm9yKCBhdXRvIHggOiB1bm1hdGNoZWQgKQogICAgICAgIHsKICAgICAgICAgICAgaWYoIGQucm9vdCh4KSA9PSBkLnJvb3QoeCtuKSApCiAgICAgICAgICAgICAgICB1bm1hdGNoZWQuZXJhc2UoeCk7CiAgICAgICAgfQoKICAgICAgICBpZiggdW5tYXRjaGVkLmVtcHR5KCkgKQogICAgICAgIHsKICAgICAgICAgICAgY291dDw8dyxubDsKICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgfQogICAgfQoKCiAgICBjZXJyIDw8ICJcblRpbWUgZWxhcHNlZDogIiA8PCAxMDAwICogY2xvY2soKSAvIENMT0NLU19QRVJfU0VDIDw8ICJtc1xuIjsKICAgIHJldHVybiAwOwp9