#include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repf(i, n, flag) for (int i = 0; i < (int)(n) && flag; i++) #define all(v) v.begin(), v.end() const int INF = 1e9; int main() { int n, m; cin >> n >> m; vector dp(n, vector(n, INF)); rep(i, m) { int a, b; cin >> a >> b; a--; b--; dp[a][b] = dp[b][a] = 1; } int cnt = 0; vector c(n); rep(i, n) { cin >> c[i]; if(c[i] == 1) cnt++; } rep(k, n) rep(i, n) rep(j, n) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); mcf_graph g(2*n+2); int s = 2*n, t = s + 1; rep(i, n) g.add_edge(s, i, 1, 0); rep(i, n) g.add_edge(n+i, t, 1, 0); rep(i, n) rep(j, n) { if(i == j) continue; if(c[i] == 0) continue; if(c[j] == 0) continue; if(dp[i][j] >= INF) continue; g.add_edge(i, n+j, 1, dp[i][j]); } auto res = g.flow(s, t); if(res.first < cnt) cout << -1 << endl; else cout << res.second / 2 << endl; }