#include <bits/stdc++.h>
const int MAXN = 5 * 100000 + 10;
const int MOD = 1000000000 + 7;
const int INF = 0x7fffffff;
using namespace std;
vector<int> G[MAXN];
vector<int> S[MAXN];
vector<int> Lay[MAXN];
int n, m, Ans = 0;
int vis[MAXN], col[MAXN];
void dfs(int fa, int x) {
  int pos = 1;
  priority_queue<int> Q;
  while (!Q.empty()) Q.pop();
  for (int i = 0; i < S[x].size(); i++)
    if (col[S[x][i]] != 0) Q.push(-col[S[x][i]]);
  for (int i = 0; i < S[x].size(); i++) {
    if (col[S[x][i]] != 0) continue;
    while (!Q.empty() && (pos == (-Q.top()))) {
      pos++;
      Q.pop();
    }
    col[S[x][i]] = pos;
    pos++;
  }
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v == fa) continue;
    dfs(x, v);
  }
}
int main() {
  memset(col, 0, sizeof(col));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) S[i].clear();
  for (int i = 1; i <= m; i++) Lay[i].clear();
  for (int i = 1; i <= n; i++) {
    int s, x;
    scanf("%d", &s);
    Ans = max(Ans, s);
    for (int j = 1; j <= s; j++) {
      scanf("%d", &x);
      S[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0, 1);
  if (Ans == 0) Ans = 1;
  printf("%d\n", Ans);
  for (int i = 1; i <= m; i++) {
    if (col[i] == 0)
      printf("1 ");
    else
      printf("%d ", col[i]);
  }
  return 0;
}
