#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> k(M), p(M);
  vector<vector<int>> s(M, vector<int>(N));
  for (int i = 0; i < M; i++) {
    cin >> k[i];
    for (int j = 0; j < k[i]; j++) {
      cin >> s.at(i).at(j);
    }
  }
  for (int i = 0; i < M; i++) {
    cin >> p[i];
  }
  int ans = 0;
  vector<int> b(M + 1);
  int a = 0;
  while (1) {
    for (int i = 0; i < N; i++) {
      if (b[i] == 2) {
        b[i] = 0;
        b[i + 1]++;
      }
    }
    cout << a << " " << b[N] << endl;
    a++;
    if (b[N] == 1) break;
    for (int i = 0; i < M; i++) {
      int count = 0;
      for (int j = 0; j < k[i]; j++) {
        if (b[s.at(i).at(j)] == 1) {
          count++;
        }
      }
      if (count % 2 != p[i]) break;
      if (i == M - 1) {
        ans++;
      }
    }
    b[0]++;
  }
  cout << ans << endl;
}
