#include <bits/stdc++.h>
using namespace std;
vector<int> v[300005];
int col[300005], cnt, m;
bool comp(vector<int> x, vector<int> y) {
  if (x.size() != y.size()) return x.size() > y.size();
  int mn = min(x.size(), y.size());
  for (int i = 0; i < mn; i++)
    if (x[i] < y[i])
      return 1;
    else
      return 0;
  return 1;
}
int tr[1500000];
void init(int nd, int b, int e) {
  if (b == e) {
    tr[nd] = b;
    return;
  }
  int md, lf, rt;
  md = (b + e) / 2;
  lf = nd * 2;
  rt = lf + 1;
  init(lf, b, md);
  init(rt, md + 1, e);
  tr[nd] = min(tr[lf], tr[rt]);
}
void update(int nd, int b, int e, int i, int x) {
  if (i > e || i < b) return;
  if (b == e) {
    tr[nd] = b + x;
    return;
  }
  int md, lf, rt;
  md = (b + e) / 2;
  lf = nd * 2;
  rt = lf + 1;
  update(lf, b, md, i, x);
  update(rt, md + 1, e, i, x);
  tr[nd] = min(tr[lf], tr[rt]);
}
vector<int> g[300000];
void dfs(int i, int par) {
  int k = v[i].size();
  for (int j = 0; j < k; j++) {
    if (col[v[i][j]] != 0) update(1, 1, m, col[v[i][j]], 300000);
  }
  for (int j = 0; j < k; j++) {
    if (col[v[i][j]] == 0) {
      col[v[i][j]] = tr[1];
      cnt = max(cnt, tr[1]);
      update(1, 1, m, tr[1], 300000);
    }
  }
  for (int j = 0; j < k; j++) {
    update(1, 1, m, col[v[i][j]], 0);
  }
  k = g[i].size();
  for (int j = 0; j < k; j++) {
    if (g[i][j] != par) {
      dfs(g[i][j], i);
    }
  }
}
int main() {
  int n;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int vl;
      scanf("%d", &vl);
      v[i].push_back(vl);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  init(1, 1, m);
  cnt = 1;
  dfs(0, -1);
  cout << cnt << endl;
  for (int i = 1; i <= m; i++) printf("%d ", col[i] == 0 ? 1 : col[i]);
  printf("\n");
}
