#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, T;
int u[N], v[N], e;
int W[N][25], in[N];
int co[N * 10];
int main() {
  register int i, j;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= m; i++) scanf("%d %d", &u[i], &v[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &W[i][0]);
    for (j = 1; j <= W[i][0]; j++) scanf("%d", &W[i][j]), T = max(T, W[i][j]);
  }
  while (1) {
    for (i = 1; i <= T; i++) co[i] = (rand() % 2) + 1;
    for (i = 1; i <= n; i++) in[i] = W[i][rand() % W[i][0] + 1];
    for (i = 1; i <= m; i++) e += co[in[u[i]]] != co[in[v[i]]];
    if (e * 2 >= m) break;
    e = 0;
  }
  for (i = 1; i <= n; i++) printf("%d%c", in[i], i != n ? ' ' : '\n');
  for (i = 1; i <= T; i++) printf("%d%c", co[i], i != n ? ' ' : '\n');
  return 0;
}
