#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 3e5 + 5;
int col[maxn];
int a[maxn];
int v[maxn];
vector<int> tree[maxn];
vector<int> save[maxn];
int maxm = 1;
void dfs(int u, int fa) {
  int k;
  k = save[u].size();
  memset(col, 0, sizeof(int) * (k + 2));
  maxm = max(maxm, k);
  int temp = 1;
  for (int i = 0; i < k; ++i)
    if (a[save[u][i]]) col[a[save[u][i]]] = 1;
  for (int i = 0; i < k; ++i) {
    if (!a[save[u][i]]) {
      while (col[temp] == 1) temp++;
      col[temp] = 1;
      a[save[u][i]] = temp++;
    }
  }
  for (int i = 0; i < tree[u].size(); ++i) {
    int pt = tree[u][i];
    if (pt == fa) continue;
    dfs(pt, u);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int i, j, k, m, n, nn;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> k;
    for (int j = 0; j < k; ++j) {
      save[i].resize(k);
      cin >> save[i][j];
    }
  }
  int tempk, tempp;
  for (int i = 1; i <= n - 1; ++i) {
    cin >> tempk >> tempp;
    tree[tempk].push_back(tempp);
    tree[tempp].push_back(tempk);
  }
  dfs(1, -1);
  cout << maxm << endl;
  for (int i = 1; i <= m; ++i) {
    if (a[i])
      cout << a[i] << " ";
    else
      cout << "1 ";
  }
  return 0;
}
