#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
int n, m, team[maxn], T;
bool choose[1000005], visit[maxn];
vector<int> G[maxn];
vector<int> List[maxn];
bool solve() {
  memset(visit, 0, sizeof(visit));
  for (int i = 1; i <= T; ++i) choose[i] = rand() % 2;
  for (int i = 1; i <= n; ++i) {
    int cnt = 0;
    visit[i] = 1;
    for (int j = 0; j < G[i].size(); ++j) {
      int v = G[i][j];
      if (!visit[v]) continue;
      if (choose[team[v]])
        cnt++;
      else
        cnt--;
    }
    cnt = (cnt <= 0);
    bool ok = 0;
    for (int j = 0; j < List[i].size(); ++j) {
      int v = List[i][j];
      if (choose[v] == cnt) {
        team[i] = v;
        ok = 1;
      }
    }
    if (!ok) return 0;
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  srand(time(0));
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    int u, v;
    cin >> u;
    while (u--) {
      cin >> v;
      List[i].push_back(v);
      T = max(T, v);
    }
  }
  while (!solve()) {
  }
  for (int i = 1; i <= n; ++i) cout << team[i] << ' ';
  cout << '\n';
  for (int i = 1; i <= T; ++i) cout << choose[i] + 1 << ' ';
}
