#include <bits/stdc++.h>
using namespace std;
const int INF = (int)(1e9 + 1337);
const long long LINF = (long long)(4e18);
const double EPS = (double)(1e-11);
int n;
int m;
int ans;
vector<vector<int> > g;
vector<set<int> > s;
vector<set<int> > fbd;
int a[1000010];
void dfs(int v, int p) {
  int cur = 1;
  for (auto &it : s[v]) {
    if (a[it] != -1) fbd[v].insert(a[it]);
  }
  for (auto &it : s[v]) {
    if (a[it] != -1) continue;
    while (fbd[v].count(cur)) cur++;
    ans = max(ans, cur);
    a[it] = cur;
    cur++;
  }
  for (int i = 0; i < (int)g[v].size(); i++) {
    int to = g[v][i];
    if (p == to) continue;
    dfs(to, v);
  }
}
void solve() {
  cin >> n >> m;
  g.resize(n + 1);
  s.resize(n + 1);
  fbd.resize(n + 1);
  int tk = 0;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    tk = max(k, tk);
    for (int j = 0; j < k; j++) {
      int tt;
      cin >> tt;
      s[i].insert(tt);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  memset(a, -1, sizeof a);
  dfs(1, 0);
  if (ans == 0) ans = 1;
  cout << ans << '\n';
  for (int i = 1; i <= m; i++) {
    if (a[i] == -1) a[i] = 1;
    cout << a[i] << ' ';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
}
