#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3;
int n, m, head[N], len, col[N];
vector<int> s[N];
int vis[N];
bool o[N];
int ans = 1;
struct edge {
  int v, fail;
  edge() {}
  edge(int _v, int _fail) {
    v = _v;
    fail = _fail;
  }
} e[N << 1];
void init() {
  memset(head, 0xff, sizeof head);
  len = 0;
}
void add(int u, int v) {
  e[len] = edge(v, head[u]);
  head[u] = len++;
}
void add2(int u, int v) {
  add(u, v);
  add(v, u);
}
bool mark[N];
void dfs(int u, int pre) {
  o[u] = 1;
  for (int i = 0; i < s[u].size(); i++) {
    if (col[s[u][i]]) vis[col[s[u][i]]] = u;
  }
  int j = 1;
  for (int i = 0; i < s[u].size(); i++) {
    if (!col[s[u][i]]) {
      while (vis[j] == u) j++;
      col[s[u][i]] = j;
      vis[j] = u;
    }
  }
  for (int i = head[u]; ~i; i = e[i].fail) {
    int v = e[i].v;
    if (v != pre) dfs(v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  init();
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d", &x);
    ans = max(ans, x);
    while (x--) {
      scanf("%d", &y);
      s[i].push_back(y);
    }
  }
  int u, v;
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    add2(u, v);
  }
  for (int i = 1; i <= n; i++) {
    if (!o[i]) {
      dfs(i, -1);
    }
  }
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d ", max(col[i], 1));
  return 0;
}
