#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int maxn = 50005, maxe = 100005;
int n, e, T, t[maxn][22], blt[maxn], fz[maxn * 22];
int cnt, to[maxe], nx[maxe], hd[maxn], c[2][maxn];
void addedge(int u, int v) {
  to[cnt] = v;
  nx[cnt] = hd[u];
  hd[u] = cnt++;
}
int main() {
  n = rd(), e = rd();
  memset(hd, -1, sizeof hd);
  for (int i = 1; i <= (int)e; i++) {
    int u = rd(), v = rd();
    if (u > v) swap(u, v);
    addedge(u, v);
  }
  for (int u = 1; u <= (int)n; u++) {
    if (c[0][u] > c[1][u])
      blt[u] = 0;
    else
      blt[u] = 1;
    for (int i = hd[u]; ~i; i = nx[i]) {
      int v = to[i];
      c[blt[u] ^ 1][v]++;
    }
  }
  for (int i = 1; i <= (int)n; i++) {
    *t[i] = rd();
    for (int j = 1; j <= (int)*t[i]; j++) t[i][j] = rd(), T = max(T, t[i][j]);
  }
  srand((unsigned)time(NULL));
  while (1) {
    for (int i = 1; i <= (int)T; i++) fz[i] = rand() % 2;
    bool ok = 1;
    for (int i = 1; i <= (int)n; i++) {
      ok = 0;
      for (int j = 1; j <= (int)*t[i]; j++)
        if (fz[t[i][j]] == blt[i]) ok = 1;
      if (!ok) break;
    }
    if (ok) {
      for (int i = 1; i <= (int)n; i++) {
        for (int j = 1; j <= (int)*t[i]; j++)
          if (fz[t[i][j]] == blt[i]) {
            rt(t[i][j]), putchar(' ');
            break;
          }
      }
      puts("");
      for (int i = 1; i <= (int)T; i++) rt(fz[i] + 1), putchar(' ');
      return 0;
    }
  }
}
