#include <bits/stdc++.h>
const long long MOD = 1000000007;
using namespace std;
int n, m;
vector<int> tree[(300000 + 10)], ice[(300000 + 10)];
int vis[(300000 + 10)];
int color[(300000 + 10)];
int max_color = 1;
void dfs(int id, int fa) {
  for (int i = 0; i < ice[id].size(); i++) {
    vis[color[ice[id][i]]] = 1;
  }
  int now_color = 1;
  for (int i = 0; i < ice[id].size(); i++) {
    if (color[ice[id][i]]) continue;
    while (vis[now_color]) now_color++;
    max_color = max(max_color, now_color);
    color[ice[id][i]] = now_color++;
  }
  for (int i = 0; i < ice[id].size(); i++) {
    vis[color[ice[id][i]]] = 0;
  }
  for (int i = 0; i < tree[id].size(); i++) {
    if (tree[id][i] != fa) dfs(tree[id][i], id);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    while (k--) {
      int x;
      scanf("%d", &x);
      ice[i].push_back(x);
    }
    sort(ice[i].begin(), ice[i].end());
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  dfs(1, 0);
  printf("%d\n", (max_color));
  for (int i = 1; i <= m; i++) printf("%d ", color[i] == 0 ? 1 : color[i]);
  printf("\n");
  return 0;
}
