#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 1000;
vector<int> in[MAXN], g[MAXN];
int n, m, ans;
bool vis[MAXN], used[MAXN];
int col[MAXN];
void dfs(int u) {
  int tp = 1;
  vis[u] = 1;
  for (int i = 0; i < in[u].size(); i++) {
    int v = in[u][i];
    if (col[v]) used[col[v]] = true, tp = max(tp, col[v]);
  }
  int st = 1;
  for (int i = 0; i < in[u].size(); i++) {
    int v = in[u][i];
    if (!col[v]) {
      while (used[st]) st++;
      col[v] = st++;
    }
  }
  tp = max(tp, st);
  for (int i = 0; i <= tp; i++) used[i] = false;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v]) dfs(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int s;
    scanf("%d", &s);
    for (int j = 1; j <= s; j++) {
      int num;
      scanf("%d", &num);
      in[i].push_back(num);
    }
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1);
  for (int i = 1; i <= m; i++)
    if (col[i] == 0) col[i] = 1;
  for (int i = 1; i <= m; i++) ans = max(ans, col[i]);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf(i == m ? "%d\n" : "%d ", col[i]);
  return 0;
}
