#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 7;
int n, m;
vector<int> E[maxn], G[maxn], H[maxn];
int vis[maxn];
int ans[maxn];
map<int, int> H2[maxn];
queue<int> Q;
int tot;
void solve(int i) {
  for (int j = 0; j < E[i].size(); j++) {
    int v = E[i][j];
    if (ans[v] != 0) continue;
    while (H2[i][tot] == 1) tot++;
    ans[v] = tot;
    H2[i][tot] = 1;
    for (int k = 0; k < H[v].size(); k++) {
      if (H2[H[v][k]][tot] == 0) {
        Q.push(H[v][k]);
      }
      H2[H[v][k]][tot] = 1;
    }
  }
  vis[i] = 1;
}
int main() {
  scanf("%d%d", &n, &m);
  int Ans = 0;
  for (int i = 1; i <= n; i++) {
    int p;
    scanf("%d", &p);
    for (int j = 0; j < p; j++) {
      int x;
      scanf("%d", &x);
      E[i].push_back(x);
      H[x].push_back(i);
    }
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    tot = 1;
    if (vis[i]) continue;
    solve(i);
    while (!Q.empty()) {
      int now = Q.front();
      tot = 1;
      Q.pop();
      if (vis[now]) continue;
      solve(now);
    }
  }
  for (int i = 1; i <= m; i++)
    if (ans[i] == 0) ans[i] = 1;
  for (int i = 1; i <= m; i++) Ans = max(Ans, ans[i]);
  cout << Ans << endl;
  for (int i = 1; i <= m; i++) cout << ans[i] << " ";
  cout << endl;
}
