#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<int> col[N];
int n, m;
vector<int> adj[N];
int ans[N];
int val;
void dfs(int u, int par) {
  int cnt = 1;
  set<int> s;
  for (auto x : col[u]) {
    s.insert(ans[x]);
  }
  for (auto x : col[u]) {
    if (ans[x]) continue;
    while (s.find(cnt) != s.end()) {
      cnt++;
    }
    ans[x] = cnt;
    val = max(val, cnt);
    s.insert(cnt);
  }
  for (auto x : adj[u]) {
    if (x != par) dfs(x, u);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n + 1; i++) {
    int sz;
    scanf("%d", &sz);
    while (sz--) {
      int x;
      scanf("%d", &x);
      col[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  val = 1;
  dfs(1, -1);
  cout << val << endl;
  for (int i = 1; i < m + 1; i++) {
    printf("%d ", max(1, ans[i]));
  }
  cout << endl;
}
