#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);
}
vector<vector<ll>> dp(n, vector<ll>(2001, 1e18));
dp[0][a[0]] = 0;
queue<pair<ll, ll>> q;
q.push({0, 0});
while (!q.empty()) {
auto [u, h] = q.front();
q.pop();
for (auto v : adj[u]) {
if (a[v] <= h - 1 and h - 1 >= 0 and dp[v][h - 1] > dp[u][h] + 1) {
dp[v][h - 1] = dp[u][h] + 1;
q.push({v, h - 1});
}
if (a[v] <= h and dp[v][h] > dp[u][h] + 1) {
dp[v][h] = dp[u][h] + 1;
q.push({v, h});
}
if (a[v] <= h + 1 and h + 1 <= 2000 and dp[v][h + 1] > dp[u][h] + 1) {
dp[v][h + 1] = dp[u][h] + 1;
q.push({v, h + 1});
}
}
}
cout << dp[n - 1][0] << "\n";
}