#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
#define pii pair<int,int>
using namespace std;
const ll maxn=1e6+5, inf=1e9;
int n, m, a[maxn], d1[maxn], d2[maxn], ans;
vector<int> G[maxn];
priority_queue<pii, vector<pii>, greater<pii>> pq;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp", "r", stdin);
        freopen(__file_name ".out", "w", stdout);
    }
    // code here
    cin >> n >> m;
    f1(i,n) cin >> a[i];
    f1(i,m){
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    if(n <= 2){
        cout << 0;
        return 0;
    }
    f1(i,n) d1[i] = inf;
    d1[1] = 0;
    pq.push({d1[1], 1});
    while(!pq.empty()){
        int dst = pq.top().first, u = pq.top().second;
        pq.pop();
        if(dst != d1[u]) continue;
        for(int c: G[u]){
            if(d1[c] > max(a[c], d1[u] + 1)){
                d1[c] = max(a[c], d1[u] + 1);
                pq.push({d1[c], c});
            }
        }
    }
    f1(i,n) d2[i] = inf;
    d2[n] = 0;
    pq.push({d2[n], n});
    while(!pq.empty()){
        int dst = pq.top().first, u = pq.top().second;
        pq.pop();
        if(dst != d2[u]) continue;
        for(int c: G[u]){
            if(d2[c] > max(a[c], d2[u] + 1)){
                d2[c] = max(a[c], d2[u] + 1);
                pq.push({d2[c], c});
            }
        }
    }
    ans = inf;
    for(int i=2;i<n;i++) {
        ans = min(ans, d1[i] + d2[i]);
    }
    cout << ans;
    return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen(__file_name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen(__file_name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |