#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, M, u, v; ll INF = 1ll << 30; cin >> n >> M; vector>> g1(2 * n), g2(2 * n); vector h(n); for(int i = 0; i < n; i++){ cin >> h[i]; g1[i].emplace_back(i + n, 0); g2[i].emplace_back(i + n, 0); } for(int i = 0; i < M; i++){ cin >> u >> v; u--, v--; if(h[u] > h[v])swap(u, v); if(u < v){ g1[u].emplace_back(n + v, abs(v - u) * INF - (h[v] - h[u])); g2[v + n].emplace_back(u, abs(v - u) * INF); }else{ g2[u].emplace_back(n + v, abs(v - u) * INF - (h[v] - h[u])); g1[v + n].emplace_back(u, abs(v - u) * INF); } } priority_queue, vector>, greater>> pq; ll d; vector dist1(2 * n, 1ll << 61), dist2(2 * n, 1ll << 61); dist1[0] = 0; pq.emplace(0, 0); while(!pq.empty()){ tie(d, v) = pq.top(); pq.pop(); //cerr << d << " " << v << '\n'; if(d > dist1[v])continue; for(auto &&p:g1[v]){ if(d + p.second >= dist1[p.first])continue; dist1[p.first] = d + p.second; pq.emplace(dist1[p.first], p.first); } } dist2[n - 1] = 0; pq.emplace(0, n - 1); while(!pq.empty()){ tie(d, v) = pq.top(); pq.pop(); //cerr << d << " " << v << '\n'; if(d > dist2[v])continue; for(auto &&p:g2[v]){ if(d + p.second >= dist2[p.first])continue; dist2[p.first] = d + p.second; pq.emplace(dist2[p.first], p.first); } } //cerr << "enter" << '\n'; ll ans = min(dist1[n - 1], dist1[2 * n - 1]); if(ans >> 61 & 1){ cout << -1 << '\n'; }else{ cout << INF * (n - 1) - ans << '\n'; } ans = min(dist2[0], dist2[n]); if(ans >> 61 & 1){ cout << -1 << '\n'; }else{ cout << INF * (n - 1) - ans << '\n'; } }