#include <bits/stdc++.h>
using namespace std;
int n, m;
int x, y, z;
int a[100005];
int ans;
struct graph {
int f[100005];
int g[100005];
int vs[100005];
vector<int>E[100005];
void edd(int x, int y) {
E[x].push_back(y);
}
void D(int nw, int la) {
if (vs[nw])return;
vs[nw] = 1;
for (int i = 0; i < E[nw].size(); i++) {
D(E[nw][i], nw);
f[la] = max(f[la], f[nw]);
g[la] = min(g[la], g[nw]);
}
return ;
}
}G;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
G.f[i] = G.g[i] = a[i];
}
G.edd(0, 1);
G.f[0] = -0x7fffffff;
G.g[0] = 0x7fffffff;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
if (--z) {
G.edd(y, x);
}
G.edd(x, y);
}
G.D(1, 0);
cout << G.f[0] - G.g[0] << '\n';
return 0;
}