#include <bits/stdc++.h>



using namespace std;
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<vector<int> > v;
    for(int i = 0 ; i < n ; i++) {
        vector<int> temp(5);
        for(int i = 0 ; i < 5 ; i++) {
            cin >> temp[i];
        }
        v.push_back(temp);
    }
    vector<vector<int> > A(n);
    A[n-1]=v[n-1];
    for(int i=n-2; i>=0; i--) {
        if(A[i+1][0]>v[i][0]) {
            A[i]=A[i+1];
        } else {
            A[i]=v[i];
        }
    }

    vector<vector<int> > B(n);
    B[n-1]=v[n-1];
    for(int i=n-2; i>=0; i--) {
        if(B[i+1][1]>v[i][1]) {
            B[i]=B[i+1];
        } else {
            B[i]=v[i];
        }
    }

    vector<vector<int> > C(n);
    C[n-1]=v[n-1];
    for(int i=n-2; i>=0; i--) {
        if(C[i+1][2]>v[i][2]) {
            C[i]=C[i+1];
        } else {
            C[i]=v[i];
        }
    }

    vector<vector<int> > D(n);
    D[n-1]=v[n-1];
    for(int i=n-2; i>=0; i--) {
        if(D[i+1][3]>v[i][3]) {
            D[i]=D[i+1];
        } else {
            D[i]=v[i];
        }
    }
    vector<vector<int> > E(n);
    E[n-1]=v[n-1];
    for(int i=n-2; i>=0; i--) {
        if(E[i+1][4]>v[i][4]) {
            E[i]=E[i+1];
        } else {
            E[i]=v[i];
        }
    }

    int res=0;
    for(int i=0; i<n; i++) {
        for(int j=i+1; j<n-1; j++) {
            for(int k=0; k<5; k++) {
                vector<int> *side;
                if(k==0) {
                    side=&A[j+1];
                } else if(k==1) {
                    side=&B[j+1];
                } else if(k==2) {
                    side=&C[j+1];
                } else if(k==3) {
                    side=&D[j+1];
                } else if(k==4) {
                    side=&E[j+1];
                }
                int foo=INT_MAX;
                for(int k=0; k<5; k++) {
                    foo = min(foo,max(v[i][k],max((*side)[k],v[j][k])));
                }
                res = max(res,foo);
            }
        }
    }

    cout << res << '\n';




    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}
int search(vec_int& DP, vector<vec_int>& G, vec_int& A, int town){
    if(G.at(town).size()==0){
        DP.at(town) = A.at(town-1);
        return A.at(town-1);
    }
    if(DP.at(town)>=0){
        return DP.at(town);
    }
    int result = -1;
    for(auto next_town: G.at(town)){
        int temp  = search(DP, G, A, next_town);
        result = max(temp, result);
    }
    result = max(A.at(town-1), result);
    DP.at(town) = result;

    return result;
}

signed main(){
    int N; cin>>N;
    int M; cin>>M;
    vec_int A(N); rep(i,N)cin>>A.at(i);
    vec_int X(M);
    vec_int Y(M);
    rep(i,M)cin>>X.at(i)>>Y.at(i);

    vector<vec_int> G(N+1);
    vector<vec_int> G2(N+1);
    rep(i,M){
        G.at(X.at(i)).push_back(Y.at(i));
    }


    vec_int DP(N+1, -1);
    int ans = -1*pow(10,12);
    for(int i=1;i<=N;i++){
        if(G.at(i).size()==0)continue;
        int temp = search(DP, G, A, i);
        int max_val = -1;
        for(auto next_city:G.at(i)){
            max_val = max(max_val, DP.at(next_city));
        }
        ans = max(ans, max_val- A.at(i-1));
    }
    cout<<ans<<endl;

    return 0;
}