#include <bits/stdc++.h>
using namespace std;
vector<int> G[(int)(3e5 + 50)], col[(int)(3e5 + 50)];
vector<int> cur_color;
int ans[(int)(3e5 + 50)];
bool done[(int)(3e5 + 50)], vis[(int)(3e5 + 50)];
set<int> color;
void dfs(int vtx) {
  vis[vtx] = 1;
  for (int i = 0; i < col[vtx].size(); i++) {
    int color = col[vtx][i];
    if (ans[color])
      done[ans[color]] = true;
    else
      cur_color.push_back(color);
  }
  int a = 1;
  for (int i = 0; i < cur_color.size(); i++) {
    while (done[a]) a++;
    ans[cur_color[i]] = a++;
  }
  for (int i = 0; i < col[vtx].size(); i++) {
    int color = col[vtx][i];
    done[ans[color]] = false;
  }
  cur_color.clear();
  for (int i = 0; i < G[vtx].size(); i++) {
    if (vis[G[vtx][i]]) continue;
    dfs(G[vtx][i]);
  }
}
void solve() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++) {
    int sz;
    scanf("%d", &sz);
    while (sz--) {
      int color;
      scanf("%d", &color);
      col[i].push_back(color);
    }
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1);
  int mx = 0;
  for (int i = 1; i <= m; i++) {
    if (!ans[i]) ans[i] = 1;
    mx = max(mx, ans[i]);
  }
  cout << mx << endl;
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << " ";
  }
}
int main() {
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
