#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
using namespace std;
const int maxn = 1000009;
const int maxxn = 100009;
int tt = 0;
int n,m;
int sum[maxxn];
int ans = 0;
int h[maxxn];
int lmaxx[maxxn];
int lminn[maxxn];
int rmaxx[maxxn];
int rminn[maxxn];
int a[maxn];
int tot = 0;
int vv[maxxn];
int head[maxxn];
bool v[maxxn];
struct node {
int fr,ver,Next,id;
};
node edge[maxn];
node ee[maxn];
void bfs (int s) {
lminn[s] = a[s];
lmaxx[s] = a[s];
queue <int > q;
q.push(s);
v[s] = 1;
vv[s]++;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = head[x];i;i = edge[i].Next) {
int y = edge[i].ver;
if (ee[i].id == 2) {
lmaxx[y] = max(lmaxx[x],a[y]);
}
lminn[y] = min(lminn[x],a[y]);
if (v[y] == 0) vv[y]++,q.push(y),v[y] = 1;
}
}
}
void bfs_ (int s) {
rminn[s] = a[s];
rmaxx[s] = a[s];
queue <int > q;
q.push(s);
v[s] = 1;
vv[s]++;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = h[x];i;i = ee[i].Next) {
int y = ee[i].ver;
rmaxx[y] = max(rmaxx[x],a[y]);
if (ee[i].id == 2) {
rminn[y] = min(rminn[x],a[y]);
}
if (v[y] == 0) vv[y]++,q.push(y),v[y] = 1;
}
}
}
inline int Read () {
int xx = 0;
int ff = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') ff = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
xx = (xx << 1) + (xx << 3) + ch - '0';
ch = getchar();
}
return xx * ff;
}
inline void add (int x,int y,int z) {
edge[++tot].fr = x; edge[tot].ver = y; edge[tot].Next = head[x]; head[x] = tot; edge[tot].id = z;
}
inline void ddd (int x,int y,int z) {
ee[++tt].fr = x; ee[tt].ver = y; ee[tt].Next = h[x]; h[x] = tt; ee[tt].id = z;
}
int main () {
n = Read(); m = Read();
for (int i = 1;i <= n;i++) {
a[i] = Read();
}
for (int i = 1;i <= m;i++) {
int aa = Read();
int bb = Read();
int cc = Read();
add(aa,bb,cc);
ddd(bb,aa,cc);
}
for (int i = 1;i <= n;i++) {
lminn[i] = a[i];
lmaxx[i] = a[i];
rmaxx[i] = a[i];
rminn[i] = a[i];
}
memset(vv,0,sizeof(vv));
memset(v,0,sizeof(v));
bfs_(n);
memset(v,0,sizeof(v));
bfs(1);
for (int i = 1;i <= n;i++) {
if(vv[i] == 2) {
sum[i] = max(rmaxx[i] - lminn[i],lmaxx[i] - rminn[i]);
ans = max(ans,sum[i]);
}
}
printf("%d\n",ans);
return 0;
}