#include <bits/stdc++.h>
using namespace std;
int color[300005];
vector<vector<int> > adj;
vector<vector<int> > actual_adj;
vector<int> visited;
void DFS(int i1) {
  visited[i1] = 1;
  vector<int> colors_present;
  for (int i = 0; i < adj[i1].size(); ++i) {
    int x = adj[i1][i];
    if (color[x] != 0) {
      colors_present.push_back(color[x]);
    }
  }
  sort(colors_present.begin(), colors_present.end());
  int count = 1;
  int k = 0;
  int l = 0;
  int ls = 0;
  while (k < colors_present.size() && l < adj[i1].size()) {
    if (count < colors_present[k]) {
      while (l < adj[i1].size() && color[adj[i1][l]] != 0) {
        l++;
      }
      if (l != adj[i1].size()) {
        color[adj[i1][l]] = count++;
      }
    } else {
      count = colors_present[k] + 1;
      k++;
    }
  }
  for (int o = l; o < adj[i1].size(); o++) {
    if (color[adj[i1][o]] == 0) {
      color[adj[i1][o]] = count++;
    }
  }
  for (int j = 0; j < actual_adj[i1].size(); ++j) {
    if (!visited[actual_adj[i1][j]]) {
      DFS(actual_adj[i1][j]);
    }
  }
}
int main(int argc, char const *argv[]) {
  long long int n, m;
  cin >> n >> m;
  adj.resize(n + 1);
  actual_adj.resize(n + 1);
  visited.resize(n + 1, 0);
  for (int j = 1; j <= n; ++j) {
    long long int si;
    cin >> si;
    for (int i = 0; i < si; ++i) {
      int x;
      cin >> x;
      adj[j].push_back(x);
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    actual_adj[u].push_back(v);
    actual_adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) DFS(i);
  }
  int maxi = 0;
  for (int i = 1; i <= m; ++i) {
    if (color[i] == 0) {
      color[i] = 1;
    }
    maxi = max(color[i], maxi);
  }
  cout << maxi << endl;
  for (int i = 1; i <= m; ++i) {
    cout << color[i] << " ";
  }
  cout << endl;
  return 0;
}
