#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, MOD = 1e9 + 7;
vector<int> g[N], v[N];
int rang[N], ans = 1;
bool mark[N];
void dfs(int i, int p) {
  for (int j : v[i]) mark[rang[j]] = true;
  int cnt = 1;
  for (int j : v[i])
    if (!rang[j]) {
      while (mark[cnt]) cnt++;
      rang[j] = cnt;
      mark[cnt] = true;
      ans = max(ans, cnt);
    }
  for (int j : v[i]) mark[rang[j]] = false;
  for (int u : g[i])
    if (u != p) dfs(u, i);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, s, x, u, p;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < s; j++) {
      cin >> x;
      x--;
      v[i].push_back(x);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> p;
    u--;
    p--;
    g[u].push_back(p);
    g[p].push_back(u);
  }
  dfs(0, 0);
  cout << ans << endl;
  for (int i = 0; i < m; i++) {
    if (rang[i])
      cout << rang[i] << " ";
    else
      cout << 1 << " ";
  }
  return 0;
}
