#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 99;
int n, m, t, x, y, rt, mx, col[N], mark[N];
vector<int> g[N], v[N];
void dfs(int x, int par) {
  for (int i = 0; i < v[x].size(); i++) mark[col[v[x][i]]] = 1;
  int p = 1;
  for (int i = 0; i < v[x].size(); i++)
    if (col[v[x][i]] == 0) {
      while (mark[p]) p++;
      col[v[x][i]] = p;
      p++;
    }
  for (int i = 0; i < v[x].size(); i++) mark[col[v[x][i]]] = 0;
  for (int i = 0; i < g[x].size(); i++)
    if (g[x][i] != par) dfs(g[x][i], x);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n + 1; i++) {
    cin >> t;
    if (t > mx) mx = t, rt = i;
    for (int j = 0; j < t; j++) {
      scanf("%d", &x);
      ;
      v[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    ;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(rt, 0);
  cout << max(mx, 1) << endl;
  for (int i = 1; i < m + 1; i++) cout << max(col[i], 1) << " ";
}
