#include <bits/stdc++.h>
using namespace std;
const int M = 3e5 + 10;
set<int> s[M];
set<int>::iterator it;
vector<int> vc[M];
int ans[M], cnt;
void dfs(int pos, int pre) {
  vector<int> used;
  if (pre == -1)
    for (it = s[pos].begin(); it != s[pos].end(); it++) ans[(*it)] = ++cnt;
  else {
    for (it = s[pos].begin(); it != s[pos].end(); it++)
      if (ans[(*it)]) used.push_back(ans[(*it)]);
    int L = used.size();
    sort(used.begin(), used.end());
    int sta = 0, now = 1;
    for (it = s[pos].begin(); it != s[pos].end(); it++) {
      if (!ans[(*it)]) {
        while (sta < L)
          if (now == used[sta])
            now++, sta++;
          else
            break;
        ans[(*it)] = now++;
      }
      cnt = max(cnt, now - 1);
    }
  }
  int len = vc[pos].size();
  for (int i = 0; i < len; i++) {
    int v = vc[pos][i];
    if (v != pre) dfs(v, pos);
  }
}
int main() {
  int n, m, num, x;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num);
    for (int j = 0; j < num; j++) {
      scanf("%d", &x);
      s[i].insert(x);
    }
  }
  int u, v;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    vc[u].push_back(v);
    vc[v].push_back(u);
  }
  memset(ans, 0, sizeof(ans));
  cnt = 0;
  dfs(1, -1);
  cnt = max(1, cnt);
  printf("%d\n", cnt);
  for (int i = 1; i <= m; i++)
    if (ans[i] == 0)
      printf("1 ");
    else
      printf("%d ", ans[i]);
  printf("\n");
}
