// #pragma GCC target("avx2") #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; using P = pair; const int M = 998244353; const long long LM = 1LL << 60; int n, m; vector> edge; vector> dist; unordered_map mp; long long dfs(long long p) { if (p == 0) return 0; if (mp.count(p)) return mp[p]; long long res = M; int fir = -1; for (int i = 0; i < 50; ++i) { if ((p >> i) & 1) { if (fir == -1) { fir = i; } else { res = min(res, dfs(p ^ (1LL << fir) ^ (1LL << i)) + dist[fir][i]); } } } return mp[p] = res; } int main() { cin.tie(0); ios::sync_with_stdio(0); cin >> n >> m; edge = vector>(n); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u; --v; edge[u].push_back(v); edge[v].push_back(u); } vector c(n); long long st = 0; for (int i = 0; i < n; ++i) { cin >> c[i]; if (c[i] == 1) { st ^= 1LL << i; } } dist = vector>(n, vector(n, M)); for (int i = 0; i < n; ++i) { dist[i][i] = 0; for (auto& j : edge[i]) { dist[i][j] = 1; } } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } long long res = dfs(st); if (res >= M) { cout << -1 << '\n'; } else { cout << res << '\n'; } return 0; }