#include <bits/stdc++.h>
using namespace std;
int col[300005];
vector<int> v[300005], g[300005];
void dfs(int s, int pp) {
  set<int> st;
  vector<int> vv;
  int i, p = 0;
  for (i = 0; i < v[s].size(); ++i) {
    int x = v[s][i];
    if (col[x])
      st.insert(col[x]);
    else
      vv.push_back(x);
  }
  for (i = 0; p < vv.size() && i < v[s].size(); ++i) {
    if (st.find(i + 1) == st.end()) col[vv[p++]] = i + 1;
  }
  for (i = 0; i < g[s].size(); ++i)
    if (g[s][i] != pp) dfs(g[s][i], s);
}
int main() {
  int ans = 1, n, m, i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; ++i) {
    int sz;
    scanf("%d", &sz);
    ans = max(ans, sz);
    for (j = 0; j < sz; ++j) {
      int x;
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  printf("%d\n", ans);
  for (i = 1; i <= m; ++i) printf("%d ", col[i] == 0 ? 1 : col[i]);
  return 0;
}
