#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, m;
cin >> n >> m;
vector<ll> a(n);
for (auto &i : a) {
cin >> i;
}
vector<vector<ll>> adj(n);
for (int i = 0; i < m; i++) {
ll u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++) {
adj[i].push_back(i);
}
auto bfs = [&](ll u) {
vector<ll> dist(n, 1e18);
dist[u] = 0;
priority_queue<pair<ll, ll>> pq;
pq.push({0, u});
while (!pq.empty()) {
auto [h, u] = pq.top();
pq.pop();
h = -h;
if (h > dist[u])
continue;
for (auto v : adj[u]) {
if (dist[v] > max(h + 1, a[v])) {
dist[v] = max(h + 1, a[v]);
pq.push({-dist[v], v});
}
}
}
return dist;
};
vector<ll> s = bfs(0);
vector<ll> e = bfs(n - 1);
ll ans = 1e18;
for (int i = 0; i < n; i++) {
vector<ll> dist(n, 1e18);
dist[i] = 0;
queue<ll> q;
q.push(i);
while (!q.empty()) {
auto u = q.front();
q.pop();
for (auto v : adj[u]) {
if (a[v] > a[i])
continue;
if (dist[v] != 1e18)
continue;
dist[v] = dist[u] + 1;
q.push(v);
}
}
ll left = 1e18, right = 1e18;
for (int j = 0; j < n; j++) {
if (dist[j] == 1e18)
continue;
if (s[j] <= a[i]) {
left = min(left, dist[j]);
}
if (e[j] <= a[i]) {
right = min(right, dist[j]);
}
}
ans = min(ans, 2 * a[i] + left + right);
}
cout << ans << "\n";
}