#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3;
vector<int> adj[N], v[N];
int col[N], ans;
bool vis[N];
void dfs(int x, int p) {
  ans = max(ans, (int)v[x].size());
  for (int it : v[x]) {
    if (col[it]) vis[col[it]] = 1;
  }
  int c = 1;
  for (int it : v[x]) {
    if (!col[it]) {
      while (vis[c]) c++;
      col[it] = c;
      vis[c] = 1;
    }
  }
  for (int it : v[x]) vis[col[it]] = 0;
  for (int to : adj[x])
    if (to != p) dfs(to, x);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int s;
    cin >> s;
    v[i].resize(s);
    int b;
    for (int j = 0; j < s; j++) cin >> v[i][j];
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b), adj[b].push_back(a);
  }
  dfs(1, 0);
  ans = max(1, ans);
  cout << ans << '\n';
  for (int i = 1; i <= m; i++) cout << max(1, col[i]) << ' ';
}
