#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const long long INF = 1.01e9;
int n, m;
vector<int> e[N];
vector<int> who[N];
int answer[N];
int cuse[N];
void dfs(int v, int p) {
  set<int> usedColor;
  vector<int> needColor;
  for (auto ice : who[v]) {
    if (cuse[ice]) {
      usedColor.insert(answer[ice]);
    } else {
      needColor.push_back(ice);
      cuse[ice] = 1;
    }
  }
  int cur = 0;
  for (auto x : needColor) {
    for (; usedColor.count(cur) == 1; cur++)
      ;
    answer[x] = cur;
    cur++;
  }
  for (auto u : e[v])
    if (u != p) dfs(u, v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    who[i].resize(k);
    for (int j = 0; j < k; j++) {
      scanf("%d", &who[i][j]);
      who[i][j]--;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u--;
    e[v].push_back(u);
    e[u].push_back(v);
  }
  dfs(0, -1);
  int mx = 0;
  for (int i = 0; i < m; i++) mx = max(mx, answer[i]);
  printf("%d\n", mx + 1);
  for (int i = 0; i < m; i++) printf("%d ", answer[i] + 1);
  puts("");
  return 0;
}
