#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int INF = 1E9;
const int MAXN = 50100;
const int MAXT = 1000100;
int n, m, T, id[MAXN], used[MAXN], choice[MAXN], usedT[MAXN], cnt[2];
vector<int> g[MAXN];
vector<int> lst[MAXN], cur;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(m); i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  T = 0;
  for (int i = 0; i < (int)(n); i++) {
    int L;
    scanf("%d", &L);
    lst[i].resize(L);
    for (int j = 0; j < (int)(L); j++) {
      scanf("%d", &lst[i][j]);
      T = max(lst[i][j], T);
      lst[i][j]--;
    }
  }
  for (int i = 0; i < (int)(n); i++) id[i] = i;
  random_shuffle(id, id + n);
  memset(usedT, -1, sizeof(usedT));
  for (int i = 0; i < (int)(n); i++) {
    int v = i;
    used[v] = 1;
    cnt[0] = cnt[1] = 0;
    for (auto w : g[v]) {
      if (used[w]) {
        cnt[usedT[choice[w]]]++;
      }
    }
    int conf = 0;
    if (cnt[0] > cnt[1]) conf = 1;
    cur.clear();
    for (auto lid : lst[v]) {
      if (usedT[lid] == -1 || usedT[lid] == conf) cur.push_back(lid);
    }
    if (cur.empty()) {
      for (auto lid : lst[v]) {
        cur.push_back(lid);
      }
    }
    int f = rand() % cur.size();
    int t = cur[f];
    choice[v] = t;
    usedT[t] = conf;
  }
  for (int i = 0; i < (int)(T); i++)
    if (usedT[i] == -1) usedT[i]++;
  for (int i = 0; i < (int)(n); i++) printf("%d ", choice[i] + 1);
  cout << '\n';
  for (int i = 0; i < (int)(T); i++) printf("%d ", usedT[i] + 1);
  return 0;
}
