#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
vector<int> ic[MAX];
vector<int> adj[MAX];
int used[MAX];
int cn;
vector<int> tr;
int ans[MAX];
void dfs(int s, int p) {
  for (int i = 0; i < ic[s].size(); i++) {
    int u = ic[s][i];
    if (!ans[u] || used[ans[u]]) {
      tr.push_back(u);
    } else {
      used[ans[u]] = 1;
    }
  }
  int cur = 1;
  for (int i = 0; i < tr.size(); i++) {
    int u = tr[i];
    while (used[cur]) {
      cur++;
    }
    ans[u] = cur;
    cur++;
  }
  cn = max(cn, cur);
  tr.clear();
  for (int i = 0; i < ic[s].size(); i++) {
    int u = ic[s][i];
    used[ans[u]] = 0;
  }
  for (int i = 0; i < adj[s].size(); i++) {
    int u = adj[s][i];
    if (u != p) {
      dfs(u, s);
    }
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int cnt;
    scanf("%d", &cnt);
    for (int j = 1; j <= cnt; j++) {
      int a;
      scanf("%d", &a);
      ic[i].push_back(a);
    }
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, 0);
  if ((cn - 1) == 0) {
    printf("1\n");
    for (int i = 1; i <= m; i++) {
      printf("1 ");
    }
  } else {
    printf("%d\n", cn - 1);
    for (int i = 1; i <= m; i++) {
      if (ans[i] == 0) {
        ans[i] = 1;
      }
      printf("%d ", ans[i]);
    }
  }
  return 0;
}
