#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1e5 + 5, M = 5e5 + 5;
int head1[N], head2[N], ver1[M], ver2[M], Next1[M], Next2[M], w[N], d1[N], d2[N];
int n, m, tot1, tot2, ans;
void spfamin(int* head, int* ver, int* Next,int start)
{
queue<int>q;
bool v[N];
memset(v, 0, sizeof(v));
memset(d1, 0x3f, sizeof(d1));
d1[start] = w[start], v[start] = 1;
q.push(start);
while (q.size())
{
int x = q.front(); q.pop();
v[x] = 0;
for (int i = head[x]; i; i = Next[i])
{
int y = ver[i];
if (d1[y] > min(d1[x], w[y]))
{
d1[y] = min(d1[x], w[y]);
if (!v[y]) q.push(y), v[y] = 1;
}
}
}
}
void spfamax(int* head, int* ver, int* Next, int start)
{
queue<int>q;
bool v[N];
memset(v, 0, sizeof(v));
d2[start] = w[start], v[start] = 1;
q.push(start);
while (q.size())
{
int x = q.front(); q.pop();
v[x] = 0;
for (int i = head[x]; i; i = Next[i])
{
int y = ver[i];
if (d2[y] < max(d2[x], w[y]))
{
d2[y] = max(d2[x], w[y]);
if (!v[y]) q.push(y), v[y] = 1;
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> w[i];
}
for (int i = 1; i <= m; i++)
{
int x, y, z;
cin >> x >> y >> z;
if (z == 1)
{
ver1[++tot1] = y, Next1[tot1] = head1[x], head1[x] = tot1;
ver2[++tot2] = x, Next2[tot2] = head1[y], head2[y] = tot2;
}
else
{
ver1[++tot1] = y, Next1[tot1] = head1[x], head1[x] = tot1;
ver2[++tot2] = x, Next2[tot2] = head1[y], head2[y] = tot2;
ver1[++tot1] = x, Next1[tot1] = head1[y], head1[y] = tot1;
ver2[++tot2] = y, Next2[tot2] = head1[x], head2[x] = tot2;
}
}
spfamin(head1, ver1, Next1, 1);
spfamax(head2, ver2, Next2, n);
for (int i = 1; i <= n; i++)
{
ans = max(ans, d2[i] - d1[i]);
}
if (ans == 0) cout << 0;
else
cout << ans;
return 0;
}