#include <bits/stdc++.h>
using namespace std;
vector<int> sets[300010];
vector<int> adjList[300010];
int cols[300010];
int N, M;
int maxcol = 1;
void dfs(int u, int p) {
  set<int> alloted;
  for (int i = 0; i < sets[u].size(); i++)
    if (cols[sets[u][i]] != 0) alloted.insert(cols[sets[u][i]]);
  auto it = alloted.begin();
  int colidx = 1;
  for (int i = 0; i < sets[u].size(); i++) {
    if (cols[sets[u][i]] != 0) continue;
    while (it != alloted.end() && (*it) == colidx) {
      it++;
      colidx++;
    }
    cols[sets[u][i]] = colidx;
    maxcol = max(maxcol, colidx);
    colidx += 1;
  }
  for (int i = 0; i < adjList[u].size(); i++) {
    int v = adjList[u][i];
    if (v == p) continue;
    dfs(v, u);
  }
}
int main() {
  cin.tie(NULL);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    sets[i].resize(n);
    for (int j = 0; j < n; j++) {
      int x;
      cin >> x;
      x--;
      sets[i][j] = x;
    }
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
  }
  dfs(0, -1);
  cout << maxcol << endl;
  for (int i = 0; i < M; i++) cout << (cols[i] == 0 ? 1 : cols[i]) << " ";
  cout << endl;
  return 0;
}
