#include <bits/stdc++.h>
using namespace std;
const int MN = 50000 + 5;
const int MT = 1000000 + 5;
int N, M;
int col[MN];
int sz[MN];
int tcol[MT];
vector<int> E[MN];
vector<int> tlist[MN];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int(a);
    scanf("%d", &(a));
    int(b);
    scanf("%d", &(b));
    ;
    if (a > b) swap(a, b);
    a--, b--;
    E[b].push_back(a);
  }
  int T = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", &sz[i]);
    for (int j = 0; j < sz[i]; j++) {
      int(a);
      scanf("%d", &(a));
      ;
      tlist[i].push_back(a);
      T = max(T, a);
    }
  }
  for (int i = 0; i < N; i++) {
    int v = 0;
    for (int j : E[i]) {
      if (col[j] == 0)
        v--;
      else
        v++;
    }
    if (v < 0) col[i] = 1;
  }
  while (true) {
    for (int i = 1; i <= T; i++) {
      tcol[i] = (rand() >> 3) & 1;
    }
    bool flag = true;
    for (int i = 0; i < N; i++) {
      int v = 0;
      for (auto &t : tlist[i]) {
        if (tcol[t] == col[i]) v++;
      }
      if (v == 0) {
        flag = false;
        break;
      }
    }
    if (flag) break;
  }
  for (int i = 0; i < N; i++) {
    for (auto &t : tlist[i]) {
      if (tcol[t] == col[i]) {
        printf("%d ", t);
        break;
      }
    }
  }
  puts("");
  for (int i = 1; i <= T; i++) printf("%d ", tcol[i] + 1);
  puts("");
  return 0;
}
