#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50005;
const int MAXM = 100005;
const int MAXT = 1000005;
pair<int, int> p[MAXM];
vector<int> v[MAXN];
int conf[MAXT], ans[MAXN];
int n, e, T = 0;
void init() {
  for (int i = 0; i < MAXT; i++) {
    conf[i] = -1;
  }
  for (int i = 0; i < MAXN; i++) {
    ans[i] = -1;
  }
}
bool check() {
  int cnt = 0;
  for (int i = 0; i < e; i++) {
    int x = p[i].first;
    int y = p[i].second;
    if (conf[ans[x]] != conf[ans[y]]) {
      cnt++;
    }
  }
  return cnt >= e / 2;
  cout << "cnt = " << cnt << endl;
  if (cnt >= e / 2) {
    cout << "Check OK!@" << endl;
  } else {
    cout << "CNT invalid" << endl;
  }
}
int main() {
  srand(time(0));
  init();
  cin >> n >> e;
  for (int i = 0, x, y; i < e; i++) {
    scanf("%d %d", &x, &y);
    p[i] = pair<int, int>(x, y);
  }
  for (int i = 1, k; i <= n; i++) {
    scanf("%d", &k);
    for (int j = 0, x; j < k; j++) {
      scanf("%d", &x);
      T = max(T, x);
      v[i].push_back(x);
    }
    random_shuffle(v[i].begin(), v[i].end());
  }
  random_shuffle(p, p + e);
  for (int i = 0, cnt = 0; i < e && cnt < e / 2; i++, cnt++) {
    int x = p[i].first;
    int y = p[i].second;
    if (ans[x] == -1 && ans[y] == -1) {
      for (auto q : v[x]) {
        for (auto r : v[y]) {
          if (conf[q] == -1 && conf[r] == -1) {
            conf[q] = rand() % 2;
            conf[r] = 1 - conf[q];
            ans[x] = q;
            ans[y] = r;
            goto ok;
          } else if (conf[q] == -1) {
            conf[q] = conf[r] ^ 1;
            ans[x] = q;
            ans[y] = r;
            goto ok;
          } else if (conf[r] == -1) {
            conf[r] = conf[q] ^ 1;
            ans[x] = q;
            ans[y] = r;
            goto ok;
          } else if (conf[q] + conf[r] == 1) {
            ans[x] = q;
            ans[y] = r;
            goto ok;
          }
        }
      }
    } else if (ans[x] == -1) {
      int r = ans[y];
      for (auto q : v[x]) {
        if (conf[q] == -1) {
          conf[q] = conf[r] ^ 1;
          ans[x] = q;
          goto ok;
        } else if (conf[q] == (conf[r] ^ 1)) {
          conf[q] = conf[r] ^ 1;
          ans[x] = q;
          goto ok;
        }
      }
    } else if (ans[y] == -1) {
      int r = ans[x];
      for (auto q : v[y]) {
        if (conf[q] == -1) {
          conf[q] = conf[r] ^ 1;
          ans[y] = q;
          goto ok;
        } else if (conf[q] == (conf[r] ^ 1)) {
          conf[q] = conf[r] ^ 1;
          ans[y] = q;
          goto ok;
        }
      }
    } else if (conf[ans[x]] == conf[ans[y]]) {
      cnt--;
    }
  ok:;
  }
  for (int i = 1; i <= n; i++) {
    if (ans[i] == -1) {
      ans[i] = v[i][rand() % v[i].size()];
    }
  }
  for (int i = 1; i <= T; i++) {
    if (conf[i] == -1) {
      conf[i] = rand() % 2;
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d%c", ans[i], " \n"[i == n]);
  }
  for (int i = 1; i <= T; i++) {
    printf("%d%c", conf[i] + 1, " \n"[i == T]);
  }
  return 0;
}
