#include <bits/stdc++.h>
using namespace std;
const int MAX = 5 * (1e5 + 5);
vector<int> e[MAX];
vector<int> ice[MAX];
int n, m;
int ans[MAX];
bool vis[MAX];
int cnt;
void dfs(int now, int pre) {
  int len = ice[now].size();
  for (int i = 0; i <= len; i++) vis[i] = 0;
  for (int i = 0; i < len; i++)
    if (ans[ice[now][i]]) vis[ans[ice[now][i]]] = 1;
  int f = 1;
  for (int i = 0; i < len; i++)
    if (!ans[ice[now][i]]) {
      int t = ice[now][i];
      while (vis[f]) f++;
      ans[t] = f++;
    }
  for (int i = 0; i < e[now].size(); i++)
    if (e[now][i] != pre) dfs(e[now][i], now);
}
int main() {
  cin >> n >> m;
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    int u, g;
    scanf("%d", &u);
    mx = max(mx, u);
    for (int j = 1; j <= u; j++) {
      scanf("%d", &g);
      ice[i].push_back(g);
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs(1, -1);
  if (mx)
    printf("%d\n", mx);
  else
    printf("1\n");
  if (ans[1])
    printf("%d", ans[1]);
  else
    printf("1");
  for (int i = 2; i <= m; i++) {
    if (ans[i])
      printf(" %d", ans[i]);
    else
      printf(" 1");
  }
  puts("");
  return 0;
}
