#include <bits/stdc++.h>
const long long INF = 1e+18;
const double eps = 1e-10;
const int MOD = 1000000007;
using namespace std;
vector<int> edge[1000005];
vector<int> ver[1000005];
int co_max, vis[1000005], color[1000005];
void init(int n) {
  co_max = 1;
  for (int i = 0; i <= n - 1; i++) {
    edge[i].clear();
    ver[i].clear();
  }
  memset(vis, 0, sizeof(vis));
  memset(color, 0, sizeof(color));
}
void dfs(int v) {
  if (vis[v]) return;
  vis[v] = 1;
  int coMax = 1, n = ver[v].size(), m = edge[v].size();
  set<int> _set;
  for (int i = 0; i <= n - 1; i++) {
    int tp = ver[v][i];
    if (color[tp] != 0) _set.insert(color[tp]);
  }
  for (int i = 0; i <= n - 1; i++) {
    int tp = ver[v][i];
    if (color[tp] == 0) {
      while (_set.end() != _set.find(coMax)) coMax++;
      color[tp] = coMax++;
      co_max = color[tp] > co_max ? color[tp] : co_max;
    }
  }
  for (int i = 0; i <= m - 1; i++) {
    int tp = edge[v][i];
    dfs(tp);
  }
}
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    init(n > m ? n : m);
    for (int i = 0; i <= n - 1; i++) {
      int k;
      scanf("%d", &k);
      for (int j = 0; j <= k - 1; j++) {
        int ve;
        scanf("%d", &ve);
        ver[i].push_back(ve - 1);
      }
    }
    for (int i = 0; i <= n - 1 - 1; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      edge[x - 1].push_back(y - 1);
      edge[y - 1].push_back(x - 1);
    }
    dfs(0);
    printf("%d\n", co_max);
    for (int i = 1; i <= m; i++) {
      printf("%d ", color[i - 1] > 1 ? color[i - 1] : 1);
    }
    printf("\n");
  }
  return 0;
}
