import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); long[] evValue = new long[(m + 1) / 2]; long[] odValue = new long[m / 2]; for (int i = 0; i < m; i++) { if (i % 2 == 0) { evValue[i / 2] = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1)); } else { odValue[i / 2] = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1)); } } long[] evDp = new long[1 << evValue.length]; HashMap evCount = new HashMap<>(); for (int i = 0; i < (1 << evValue.length); i++) { int pop = getPop(i); for (int j = 0; j < evValue.length; j++) { if ((i & (1 << j)) == 0) { continue; } evDp[i] = evDp[i ^ (1 << j)] ^ evValue[j]; break; } evCount.put(evDp[i], Math.min(evCount.getOrDefault(evDp[i], Integer.MAX_VALUE), pop)); } long[] odDp = new long[1 << odValue.length]; HashMap odCount = new HashMap<>(); for (int i = 0; i < (1 << odValue.length); i++) { int pop = getPop(i); for (int j = 0; j < odValue.length; j++) { if ((i & (1 << j)) == 0) { continue; } odDp[i] = odDp[i ^ (1 << j)] ^ odValue[j]; break; } odCount.put(odDp[i], Math.min(odCount.getOrDefault(odDp[i], Integer.MAX_VALUE), pop)); } long base = 0; for (int i = 0; i < n; i++) { base += (sc.nextInt() << i); } int ans = Integer.MAX_VALUE / 2; for (long x : evCount.keySet()) { ans = Math.min(ans, evCount.get(x) + odCount.getOrDefault(x ^ base, Integer.MAX_VALUE / 2)); } if (ans >= Integer.MAX_VALUE / 2) { System.out.println(-1); } else { System.out.println(ans); } } static int getPop(int x) { int pop = 0; while (x > 0) { pop += x % 2; x >>= 1; } return pop; } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }