#include <bits/stdc++.h>
using namespace std;
int n, m, s, si, tot, head = 1, ans[300033];
vector<set<int> > st(300033), used(300033);
vector<vector<int> > G(300033);
void dfs(int u, int pre) {
  int cnt = 1;
  if (pre != -1) {
    for (auto d : st[u]) {
      if (st[pre].count(d)) used[u].insert(ans[d]);
    }
  }
  for (auto x : st[u]) {
    if (ans[x]) continue;
    while (used[u].count(cnt)) cnt++;
    ans[x] = cnt;
    used[u].insert(cnt);
  }
  for (auto x : G[u]) {
    if (x == pre) continue;
    dfs(x, u);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &s);
    if (s > tot) {
      tot = s;
      head = i;
    }
    while (s--) {
      scanf("%d", &si);
      st[i].insert(si);
    }
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(head, -1);
  printf("%d\n", tot == 0 ? 1 : tot);
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i] == 0 ? 1 : ans[i]);
  return 0;
}
