#include <bits/stdc++.h>
using namespace std;
vector<int> node[300005];
vector<int> edge[300005];
int color[300005];
bool vis[300005];
int ice[300005];
int ans = 0;
void dfs(int now, int par) {
  int col = 1, cnt = 0;
  for (auto x : node[now]) {
    if (color[x]) ice[++cnt] = color[x];
  }
  sort(ice + 1, ice + 1 + cnt);
  if (!cnt) ice[1] = 0;
  int num = cnt;
  cnt = 1;
  for (auto x : node[now]) {
    if (color[x]) continue;
    while (col == ice[cnt] && cnt <= num) {
      ++col;
      ++cnt;
    }
    color[x] = col;
    ++col;
  }
  ans = max(ans, col - 1);
  for (auto x : edge[now]) {
    if (x != par) dfs(x, now);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    for (int j = 1; j <= t; j++) {
      int a;
      scanf("%d", &a);
      node[i].push_back(a);
    }
  }
  int a, b;
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(1, 0);
  if (!ans) ans = 1;
  cout << ans << endl;
  for (int i = 1; i <= m; i++) printf("%d ", max(color[i], 1));
  return 0;
}
