#include <bits/stdc++.h>
using namespace std;
int n, m;
int len[500020];
vector<int> s[500020];
vector<int> g[500020];
int col[500020];
set<int> FK;
int c;
int rt;
void dfs(int u, int p) {
  if (p) {
    vector<int> t;
    int k = c;
    for (auto v : s[u]) {
      if (!col[v]) continue;
      FK.erase(col[v]);
      t.push_back(col[v]);
    }
    k = 0;
    set<int>::iterator it = FK.begin();
    for (auto v : s[u]) {
      if (col[v]) continue;
      col[v] = *it;
      ++it;
    }
    for (auto v : t) {
      FK.insert(v);
    }
    t.clear();
  }
  for (auto v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  rt = 0;
  int tmp = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &len[i]);
    for (int j = 1; j <= len[i]; ++j) {
      int v;
      scanf("%d", &v);
      s[i].push_back(v);
    }
    if (len[i] > tmp) {
      tmp = len[i], rt = i;
    }
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int t = 0;
  for (auto v : s[rt]) {
    col[v] = ++t;
  }
  c = max(tmp, 1);
  for (int i = 1; i <= c; ++i) FK.insert(i);
  dfs(rt, 0);
  for (int i = 1; i <= m; ++i)
    if (!col[i]) col[i] = 1;
  printf("%d\n", c);
  for (int i = 1; i <= m; ++i) printf("%d%c", col[i], i == m ? '\n' : ' ');
  return 0;
}
