#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
const int N = 300002;
int n, m, sz[N], color[N], ans;
vector<int> arr[N], adj[N];
bool banned[N];
void dfs(int v, int par) {
  for (auto it : arr[v])
    if (color[it] != 0) banned[color[it]] = true;
  int temp = 1;
  for (auto it : arr[v])
    if (color[it] == 0) {
      while (temp <= ans && banned[temp]) temp++;
      color[it] = temp;
      banned[temp] = true;
    }
  for (auto it : arr[v]) banned[color[it]] = false;
  for (auto vv : adj[v])
    if (vv != par) dfs(vv, v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &sz[i]);
    arr[i].resize(sz[i]);
    for (int j = 0; j < sz[i]; j++) scanf("%d", &arr[i][j]);
  }
  int e = n - 1;
  while (e--) {
    int a, b;
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int root = 1;
  for (int i = 1; i <= n; i++)
    if (sz[i] > sz[root]) root = i;
  ans = 0;
  for (auto it : arr[root]) color[it] = ++ans;
  if (ans == 0) ans = 1;
  dfs(root, -1);
  for (int i = 1; i <= m; i++)
    if (color[i] == 0) color[i] = 1;
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d ", color[i]);
  return 0;
}
