#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<vector<int> > col;
vector<vector<int> > adj;
int color[N];
void dfs(int v, int p) {
  set<int> st;
  int toapply = 1;
  for (int i = 0; i < col[v].size(); i++) {
    int c = col[v][i];
    if (color[c] != -1) st.insert(color[c]);
  }
  for (int i = 0; i < col[v].size(); i++) {
    int c = col[v][i];
    if (color[c] != -1) continue;
    while (st.find(toapply) != st.end()) toapply++;
    color[c] = toapply;
    toapply++;
  }
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i];
    if (u == p) continue;
    dfs(u, v);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  col.resize(n + 5);
  adj.resize(n + 5);
  memset(color, -1, sizeof(color));
  int mx = 1;
  for (int i = 1; i <= n; i++) {
    int s;
    scanf("%d", &s);
    mx = max(mx, s);
    while (s--) {
      int t;
      scanf("%d", &t);
      col[i].push_back(t);
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  printf("%d\n", mx);
  for (int i = 1; i <= m; i++)
    if (color[i] == -1) color[i] = 1;
  for (int i = 1; i <= m; i++) printf("%d ", color[i]);
  printf("\n");
  return 0;
}
