#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
int res, col[maxn], vis[maxn];
vector<int> g[maxn], ice[maxn];
void dfs(int u, int p) {
  int rem = 1;
  for (auto i : ice[u]) vis[col[i]] = 1;
  for (auto i : ice[u]) {
    if (col[i]) continue;
    while (vis[rem]) rem++;
    res = max(res, rem);
    col[i] = rem++;
  }
  for (auto i : ice[u]) vis[col[i]] = 0;
  for (auto i : g[u])
    if (i != p) dfs(i, u);
}
int main() {
  int n, m, tmp, s;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    for (int j = 0; j < s; j++) {
      scanf("%d", &tmp);
      ice[i].push_back(tmp - 1);
    }
  }
  int a, b;
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, -1);
  printf("%d\n", max(res, 1));
  for (int i = 0; i < m; i++) printf("%d ", max(1, col[i]));
  return 0;
}
