#include <bits/stdc++.h>
using namespace std;
const int maxn = 50050;
const int maxt = 1001000;
int n, q, t;
vector<int> teams[maxn], hate[maxn];
mt19937 rnd;
int conf[maxt], tcol[maxn], match[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    hate[u].emplace_back(v);
    hate[v].emplace_back(u);
  }
  t = 0;
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    teams[i].resize(k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &teams[i][j]), --teams[i][j];
      t = max(t, teams[i][j] + 1);
    }
  }
  while (true) {
    for (int i = 0; i < t; ++i) conf[i] = rnd() & 1;
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      bool same = true;
      for (int x : teams[i]) {
        same &= conf[x] == conf[teams[i][0]];
      }
      ok &= !same;
    }
    if (ok) break;
  }
  for (int i = 0; i < n; ++i) {
    int cnt[2] = {0, 0};
    for (int to : hate[i]) {
      if (to > i) continue;
      cnt[tcol[to]]++;
    }
    tcol[i] = cnt[0] > cnt[1] ? 1 : 0;
    for (int x : teams[i]) {
      if (tcol[i] == conf[x]) {
        match[i] = x;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    printf("%d ", match[i] + 1);
  }
  printf("\n");
  for (int i = 0; i < t; ++i) {
    printf("%d ", conf[i] + 1);
  }
  printf("\n");
  return 0;
}
