#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010,MAXM = 500000 +10;
unordered_map<int,vector<int>> path;
unordered_map<int,vector<int>> rpath;
int price[MAXN];
int dp_max[MAXN],dp_min[MAXN];
int n,m;
void spfa(int start_point,int type) {
queue<int> q;
unordered_set<int> inq;
// printf("start_point:=%d\n",start_point);
if(type >0) {
dp_min[start_point] = price[start_point];
}else {
dp_max[start_point] = price[start_point];
}
q.push(start_point);
inq.insert(start_point);
while(q.size()) {
int u = q.front();q.pop(); inq.erase(u);
//cout << u<<endl;
if(type>0) {
//求最小值
for(int v: path[u]) {
if(dp_min[v] > min(dp_min[u] ,price[v] ) )
{
dp_min[v] = min(dp_min[u] ,price[v] ) ;
// printf("v:=%d , %d\n",v,dp_min[v]);
if(inq.count(v)==false) {
inq.insert(v);
q.push(v);
}
}
}
}else {
for(int v: rpath[u]) {
if(dp_max[v] < max(dp_max[u],price[v] ) )
{
dp_max[v] = max(dp_max[u],price[v] );
// printf("r v:=%d , %d\n",v,dp_max[v]);
if(inq.count(v)==false) {
inq.insert(v);
q.push(v);
}
}
}
}
}
}
int main()
{
cin>> n>>m;
for(int i=1;i<=n;++i)
cin>> price[i];
// for(int i = 1; i <= n; i ++)
// {
// //cin >> value[i];
// dp_min[i] = 999999999;
// dp_max[i] = -999999999;//先取一下极值,也就是初始化
// }
memset(dp_min,0x3f,sizeof dp_min);
memset(dp_max,-0x3f,sizeof dp_max);
//path.clear();
//rpath.clear();
while (m -- ) {
int a,b,c;
cin>> a>>b>>c;
// path[a].push_back(b);
// rpath[b].push_back(a);
// if(c == 2) {
// path[b].push_back(a);
// rpath[a].push_back(b);
// }
if(c==2) {
path[a].push_back(b);
path[b].push_back(a);
rpath[a].push_back(b);
rpath[b].push_back(a);
}else {
path[a].push_back(b);
rpath[b].push_back(a);
}
}
spfa(1,1);
spfa(n,-1);
int res = 0;
for(int i=1;i<=n;++i)
{
// printf("dp_max[%d] = %d\n",i,dp_max[i]);
// printf("dp_min[%d] = %d\n",i,dp_min[i]);
res = max(res, dp_max[i] -dp_min[i]);
}
printf("%d\n",res);
//cout << 0x3f<<endl;
return 0;
}