#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int ans[N], vis[N];
vector<int> G[N], s[N], tmp;
void dfs(int u, int fa) {
  int sz = s[u].size();
  for (int i = 1; i <= sz; i++) vis[i] = 0;
  for (int j : s[u]) {
    if (ans[j] && ans[j] <= sz) vis[ans[j]] = 1;
  }
  for (int j = 1; j <= sz; j++) {
    if (!vis[j]) tmp.push_back(j);
  }
  int cur = 0;
  for (int j : s[u]) {
    if (!ans[j]) ans[j] = tmp[cur++];
  }
  tmp.clear();
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs(v, u);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int cnt;
    scanf("%d", &cnt);
    for (int j = 1; j <= cnt; j++) {
      int x;
      scanf("%d", &x);
      s[i].push_back(x);
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  int mx = 0;
  for (int i = 1; i <= m; i++) {
    if (!ans[i]) ans[i] = 1;
    mx = max(mx, ans[i]);
  }
  printf("%d\n", mx);
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}
