#include <bits/stdc++.h>
using namespace std;
const int _ = 1e2;
const int maxn = 5e4 + _;
const int maxm = 1e5 + _;
int as[maxn], ans[maxn], c[maxm], g[maxn][22];
vector<int> h[maxn];
int main() {
  int n, m, x, y, L = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    if (x < y) swap(x, y);
    h[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &g[i][0]);
    for (int j = 1; j <= g[i][0]; j++)
      scanf("%d", &g[i][j]), L = max(L, g[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    int aa = 0, bb = 0;
    for (int j = 0; j < h[i].size(); j++)
      if (as[h[i][j]] == 0)
        aa++;
      else
        bb++;
    as[i] = aa >= bb;
  }
  srand(252628);
  bool bk = false;
  while (bk == false) {
    for (int i = 1; i <= L; i++) c[i] = rand() % 2;
    bk = true;
    for (int i = 1; i <= n; i++) {
      bool flag = false;
      for (int j = 1; j <= g[i][0]; j++)
        if (c[g[i][j]] == as[i]) {
          ans[i] = g[i][j];
          flag = true;
          break;
        }
      if (!flag) {
        bk = false;
        break;
      }
    }
  }
  for (int i = 1; i < n; i++) printf("%d ", ans[i]);
  printf("%d\n", ans[n]);
  for (int i = 1; i < L; i++) printf("%d ", c[i] + 1);
  printf("%d\n", c[L] + 1);
  return 0;
}
