#include <bits/stdc++.h>
using namespace std;
template <class T>
int size(const T &x) {
  return x.size();
}
const int INF = 2147483647;
int n, m;
set<int> ice[300100];
vector<int> adj[300100];
int col[500100];
void dfs(int at, int par) {
  set<int> here;
  for (__typeof((ice[at]).begin()) it = (ice[at]).begin();
       it != (ice[at]).end(); ++it) {
    if (col[*it] != -1) {
      here.insert(col[*it]);
    }
  }
  int check = 1;
  for (__typeof((ice[at]).begin()) it = (ice[at]).begin();
       it != (ice[at]).end(); ++it) {
    if (col[*it] == -1) {
      while (here.find(check) != here.end()) check++;
      here.insert(col[*it] = check++);
    }
  }
  for (__typeof((adj[at]).begin()) it = (adj[at]).begin();
       it != (adj[at]).end(); ++it) {
    if (*it == par) continue;
    dfs(*it, at);
  }
}
int main() {
  memset(col, -1, sizeof(col));
  cin.sync_with_stdio(false);
  cin >> n >> m;
  for (__typeof(0) i = (0); i < (n); ++i) {
    int cnt;
    cin >> cnt;
    for (__typeof(0) j = (0); j < (cnt); ++j) {
      int x;
      cin >> x;
      ice[i].insert(x);
    }
  }
  for (__typeof(0) i = (0); i < (n - 1); ++i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(0, -1);
  int mx = 1;
  for (__typeof(1) i = (1); i < (m + 1); ++i) {
    mx = max(mx, col[i]);
  }
  cout << mx << endl;
  for (__typeof(1) i = (1); i < (m + 1); ++i) {
    if (i != 1) {
      cout << " ";
    }
    if (col[i] == -1)
      cout << 1;
    else
      cout << col[i];
  }
  cout << endl;
  return 0;
}
