/*
TASK: move
LANG: C++
*/

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int n, m;
vector<vector<int> > G;
int c1;

struct pos {
    int moves;
    vector<int> val;
    
    pos() {
        moves = 0;
        val.resize(n);
    }
    int find() {
        for(int i = 0; i < n; ++i)
            if( val[i] == 1 )
                return i;
    }
    pos &operator=(pos &x) {
        for(int i = 0; i < n; ++i)
            this->val[i] = x.val[i];
        return (*this);
    }
    bool operator==(pos &P) const {
        for(int i = 0; i < P.val.size(); ++i)
            if( this->val[i] != P.val[i] ) return false;
        return true;
    }
};

inline void swap(int &a, int &b) {
    int x = a; a = b; b = x;
}

void bfs(pos crr, vector<pos>& Q) {
    int pos1 = crr.find();

    vector<int> p(n+1);

    for(int i = 0; i < n; ++i)
        p[ crr.val[i] ] = i;

    for(int i = 0; i < G[p[1]].size(); ++i) {
        bool f = true;
        pos z = crr;
        swap( z.val[ p[1] ], z.val[ G[p[1]][i] ]); 
        for(int j = 0; j < Q.size(); ++j)
            if( Q[j] == z ) { f = false; break ; }
        if( f ) {
            z.moves = crr.moves+1;
            Q.push_back(z); 
        } 
    }
}

int main() {

    cin >> n >> m; 
    G.resize(n);
    for(int i = 0; i < m; ++i) {
        int k1, k2;
        cin >> k1 >> k2;
        G[k1-1].push_back(k2-1);
        G[k2-1].push_back(k1-1);
    }

    pos wanted; for(int i = 0; i < n; ++i) wanted.val[i] = i+1;
    pos crr; for(int i = 0; i < n; ++i) cin >> crr.val[i];
    
    vector<pos> Q; Q.push_back(crr);

    int ans=0;

    while ( c1 < Q.size() ) {
        pos r = Q[c1++];
        if( r == wanted ) { ans = r.moves; break ; }
        bfs(r, Q);
    }    
    if( ans != 0 )
        cout << ans << '\n';
    else cout << "-1\n";
    return 0;
}
