#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> c;
vector<bool> used;
vector<vector<int> > e;
vector<vector<int> > ices;
int colors = 1;
int cur = 1;
int used_colors[1000000];
void dfs(int v) {
  if (used[v]) return;
  used[v] = 1;
  if (ices[v].size() > colors) {
    colors = ices[v].size();
  }
  cur++;
  for (int j = 0; j < ices[v].size(); j++) {
    int t = ices[v][j];
    if (c[t]) {
      used_colors[c[t]] = cur;
    }
  }
  int cur_unused = 1;
  for (int j = 0; j < ices[v].size(); j++) {
    int t = ices[v][j];
    if (!c[t]) {
      while (used_colors[cur_unused] == cur) cur_unused++;
      c[t] = cur_unused++;
    }
  }
  for (int i = 0; i < e[v].size(); i++) {
    dfs(e[v][i]);
  }
}
int main() {
  for (int i = 0; i < 1e6; i++) {
    used_colors[i] = 0;
  }
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  c.resize(m + 1);
  e.resize(n);
  used.assign(n, 0);
  ices.resize(n);
  int cnt;
  for (int i = 0; i < n; i++) {
    cin >> cnt;
    if (cnt == 0) continue;
    ices[i].resize(cnt);
    for (int j = 0; j < cnt; j++) {
      cin >> ices[i][j];
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    cin >> v >> u;
    v--, u--;
    e[v].push_back(u);
    e[u].push_back(v);
  }
  dfs(0);
  cout << colors << '\n';
  for (int i = 0; i < m; i++) {
    cout << max(1, c[i + 1]) << ' ';
  }
  cout << '\n';
}
