n,m = map(int,input().split()) h = list(map(int,input().split())) graph = [[] for i in range(n+1)] for i in range(m): a,b = map(int,input().split()) graph[a-1].append(b-1) graph[b-1].append(a-1) import heapq #ダイクストラ法 INF = 10**15 dist = [[INF,INF] for i in range(n*1)] que = [(0,0,0), (0,1,0)] dist[0][0] = 0 dist[0][1] = 0 while que: c,ud,crr = heapq.heappop(que) if c > dist[crr][ud]: continue for nxt in graph[crr]: if crr > nxt: continue if ud == 0: if h[nxt] < h[crr]: if dist[crr][0] < dist[nxt][1]: dist[nxt][1] = dist[crr][0] heapq.heappush(que,(dist[nxt][1],1,nxt)) else: if dist[crr][0] + (h[crr]-h[nxt]) < dist[nxt][1]: dist[nxt][1] = dist[crr][0] + (h[crr]-h[nxt]) heapq.heappush(que,(dist[nxt][1],1,nxt)) else: if h[nxt] > h[crr]: continue if dist[crr][1] < dist[nxt][0]: dist[nxt][0] = dist[crr][1] heapq.heappush(que,(dist[nxt][0],0,nxt)) ans = min(dist[n-1]) print(-1) if ans == INF else print(-ans) dist = [[INF,INF] for i in range(n*1)] que = [(0,0,n-1), (0,1,n-1)] dist[n-1][0] = 0 dist[n-1][1] = 0 while que: c,ud,crr = heapq.heappop(que) if c > dist[crr][ud]: continue for nxt in graph[crr]: if crr < nxt: continue if ud == 0: if h[nxt] < h[crr]: if dist[crr][0] < dist[nxt][1]: dist[nxt][1] = dist[crr][0] heapq.heappush(que,(dist[nxt][1],1,nxt)) else: if dist[crr][0] + (h[crr]-h[nxt]) < dist[nxt][1]: dist[nxt][1] = dist[crr][0] + (h[crr]-h[nxt]) heapq.heappush(que,(dist[nxt][1],1,nxt)) else: if h[nxt] > h[crr]: continue if dist[crr][1] < dist[nxt][0]: dist[nxt][0] = dist[crr][1] heapq.heappush(que,(dist[nxt][0],0,nxt)) ans = min(dist[0]) print(-1) if ans == INF else print(-ans)