#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100, M = 5e5 + 100;
int head1[N], ver1[M], e1[M], tot1;
int head2[N], ver2[M], e2[M], tot2;
int d1[N], d2[N];
bool v1[N], v2[N];
int price[N];
int n, m;
void add1(int x, int y)
{
++ tot1;
ver1[tot1] = y;
e1[tot1] = head1[x];
head1[x] = tot1;
}
void add2(int x, int y)
{
++ tot2;
ver2[tot2] = y;
e2[tot2] = head2[x];
head2[x] = tot2;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++) cin >> price[i];
for (int i = 1; i <= m; i ++)
{
int x, y, z;
cin >> x >> y >> z;
if (z == 1)
{
add1(x, y);
add2(y, x);
}
else
{
add1(x, y);
add1(y, x);
add2(x, y);
add2(y, x);
}
}
memset(d1, 0x3f, sizeof d1);
memset(v1, false, sizeof v1);
d1[1] = price[1];
queue<int> q1;
q1.push(1);
while (q1.size())
{
int x = q1.front(); q1.pop();
v1[x] = false;
for (int i = head1[x]; i; i = e1[i])
{
int y = ver1[i];
int t = min(d1[x], price[y]);
if (d1[y] > t)
{
d1[y] = t;
if (!v1[y])
{
q1.push(y);
v1[y] = true;
}
}
}
}
memset(d2, 0, sizeof d2);
memset(v2, false, sizeof v2);
d2[n] = price[n];
queue<int> q2;
q2.push(n);
while (q2.size())
{
int x = q2.front(); q2.pop();
v2[x] = false;
for (int i = head2[x]; i; i = e2[i])
{
int y = ver2[i];
int t = max(d2[x], price[y]);
if (d2[y] < t)
{
d2[y] = t;
if (!v2[y])
{
v2[y] = true;
q2.push(y);
}
}
}
}
int mx = 0;
for (int i = 1; i <= n; i ++) mx = max(mx, d2[i] - d1[i]);
cout << mx;
/*for (int i = 1; i <= n; i ++)
{
cout << d1[i] << " " << d2[i] << endl;
}*/
return 0;
}