n, m = map(int, input().split()) H = list(map(int, input().split())) X = [[] for _ in range(n)] Y = [[] for _ in range(n)] for _ in range(m): x, y = map(int, input().split()) x -= 1 y -= 1 X[x].append(y) Y[y].append(x) dpu = [-1 << 60] * n dpd = [-1 << 60] * n dpu[0] = 0 dpd[0] = 0 for y in range(1, n): for x in Y[y]: if H[x] > H[y]: dpd[y] = max(dpd[y], dpd[x], dpu[x]) else: dpu[y] = max(dpu[y], dpd[x] + H[y] - H[x]) ans = max(dpu[-1], dpd[-1]) if ans < 0: ans = -1 print(ans) dpu = [-1 << 60] * n dpd = [-1 << 60] * n dpu[-1] = 0 dpd[-1] = 0 for x in range(n - 2, -1, -1): for y in X[x]: if H[y] > H[x]: dpd[x] = max(dpd[x], dpd[y], dpu[y]) else: dpu[x] = max(dpu[x], dpd[y] + H[x] - H[y]) ans = max(dpu[0], dpd[0]) if ans < 0: ans = -1 print(ans)