#include #include int cmp_ll_alt (const void *a, const void *b) { long long *a_ = (long long *)a; long long *b_ = (long long *)b; if (a_[0] < b_[0]) { return -1; } if (a_[0] > b_[0]) { return 1; } if (a_[1] < b_[1]) { return 1; } if (a_[1] > b_[1]) { return -1; } return 0; } int main () { int n = 0; int m = 0; long long a[40] = {}; long long b[40] = {}; long long c = 0LL; int res = 0; int ans = 0; long long cbit = 0LL; long long half[1048576][2] = {}; res = scanf("%d", &n); res = scanf("%d", &m); for (int i = 0; i < m; i++) { res = scanf("%lld", a+i); res = scanf("%lld", b+i); a[i] -= 1LL; b[i] -= 1LL; } for (int i = 0; i < n; i++) { res = scanf("%lld", &c); cbit |= (c<<((long long)i)); } ans = m+1; for (int i = 0; i < (1<<(m/2)); i++) { for (int j = 0; j < m/2; j++) { if ((i&(1< 0) { half[i][1] += 1LL; half[i][0] ^= (1LL< 0) { cnt++; hbit ^= (1LL< 1) { int nxt = (idx[0]+idx[1])/2; if (half[nxt][0] <= (cbit^hbit)) { idx[0] = nxt; } else { idx[1] = nxt; } } if (half[idx[0]][0] == (cbit^hbit) && ans > cnt+((int)half[idx[0]][1])) { ans = cnt+((int)half[idx[0]][1]); } } if (ans > m) { printf("-1\n"); return 0; } printf("%d\n", ans); return 0; }