#include <bits/stdc++.h>
using namespace std;
int color[300005], banned[300005];
vector<int> v[300005], store[300005];
set<int> s;
void dfs(int cur, int par) {
  vector<int> tocolor;
  for (auto it : store[cur]) {
    if (color[it])
      banned[color[it]] = 1;
    else
      tocolor.push_back(it);
  }
  for (int i = 1;; i++) {
    if (!tocolor.size()) break;
    if (banned[i]) continue;
    color[tocolor.back()] = i;
    tocolor.pop_back();
  }
  for (auto it : store[cur]) banned[color[it]] = 0;
  for (int i = 0; i < v[cur].size(); i++) {
    int x = v[cur][i];
    if (x == par) continue;
    dfs(x, cur);
  }
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin.tie(0);
  ;
  int i, j, n, m, x, y;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> x;
    while (x--) {
      cin >> y;
      store[i].push_back(y);
    }
  }
  for (i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, -1);
  for (i = 1; i <= m; i++) {
    if (!color[i]) color[i] = 1;
  }
  for (i = 1; i <= m; i++) s.insert(color[i]);
  cout << s.size() << "\n";
  for (i = 1; i <= m; i++) cout << color[i] << " ";
  return 0;
}
