#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > V;
vector<vector<int> > adj;
vector<int> color;
void dfs(int a, int p, int m) {
  int n = (int)V[a].size();
  set<int> unvalid;
  for (int i = 0; i < n; ++i) unvalid.insert(color[V[a][i]]);
  unvalid.insert(m + 2);
  int c = 0;
  auto it = unvalid.begin();
  for (int i = 0; i < n; ++i) {
    if (color[V[a][i]]) continue;
    while (c == *it) {
      c++;
      it++;
    }
    color[V[a][i]] = c++;
  }
  for (auto v : adj[a]) {
    if (v == p) continue;
    dfs(v, a, m);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  V = vector<vector<int> >(n, vector<int>());
  adj = vector<vector<int> >(n, vector<int>());
  color = vector<int>(m + 1, 0);
  int ans = 1;
  for (int i = 0; i < n; ++i) {
    int s;
    cin >> s;
    V[i] = vector<int>(s);
    ans = max(ans, s);
    vector<bool> B(m + 1, false);
    for (int j = 0; j < s; ++j) {
      cin >> V[i][j];
      --V[i][j];
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0, 0, m);
  cout << ans << '\n';
  for (int i = 0; i < m; ++i) cout << max(color[i], 1) << " ";
  cout << '\n';
  return 0;
}
