#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<vector<int> > tims;
vector<int> part;
vector<int> conf;
int n;
int e;
int T = 0;
void build_part() {
  bool ok = false;
  while (!ok) {
    for (int(i) = (1); (i) < (T + 1); ++(i)) {
      part[i] = rand() % 2;
    }
    for (int(i) = 0; (i) < (n); ++(i)) {
      ok = false;
      int cnt[] = {0, 0};
      for (int(j) = 0; (j) < (tims[i].size()); ++(j)) {
        cnt[part[tims[i][j]]] = 1;
        if (cnt[0] * cnt[1] == 1) {
          ok = true;
          break;
        }
      }
      if (!ok) {
        break;
      }
    }
  }
  for (int(i) = (1); (i) < (T + 1); ++(i)) {
    ++part[i];
  }
}
void build() {
  conf[0] = 1;
  for (int(i) = (1); (i) < (n); ++(i)) {
    int d[] = {0, 0};
    for (int(j) = 0; (j) < (g[i].size()); ++(j)) {
      if (conf[g[i][j]] > 0) {
        ++d[conf[g[i][j]] - 1];
      }
    }
    if (d[0] > d[1]) {
      conf[i] = 2;
    } else
      conf[i] = 1;
  }
}
void solve() {
  srand(22812323);
  cin >> n >> e;
  g.resize(n);
  tims.resize(n);
  conf.resize(n, 0);
  for (int(i) = 0; (i) < (e); ++(i)) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int(i) = 0; (i) < (n); ++(i)) {
    int l;
    cin >> l;
    tims[i].resize(l);
    for (int(j) = 0; (j) < (tims[i].size()); ++(j)) {
      cin >> tims[i][j];
      T = max(T, tims[i][j]);
    }
  }
  part.resize(T + 1);
  build_part();
  build();
  for (int(i) = 0; (i) < (n); ++(i)) {
    for (int(j) = 0; (j) < (tims[i].size()); ++(j)) {
      if (conf[i] == part[tims[i][j]]) {
        cout << tims[i][j] << " ";
        break;
      }
    }
  }
  cout << endl;
  for (int(i) = (1); (i) < (T + 1); ++(i)) {
    cout << part[i] << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
