#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005, maxe = maxn * 2;
int cnt[maxn], col[maxn];
vector<int> V[maxn];
set<int> S;
int fir[maxn], nxt[maxe], son[maxe], tot;
void add(int x, int y) {
  son[++tot] = y;
  nxt[tot] = fir[x];
  fir[x] = tot;
}
void dfs(int x, int pre) {
  S.clear();
  for (int i = 1; i <= cnt[x]; i++) {
    int t = V[x][i - 1];
    if (col[t]) S.insert(col[t]);
  }
  int now = 0;
  for (int i = 1; i <= cnt[x]; i++) {
    int t = V[x][i - 1];
    if (!col[t]) {
      now++;
      while (S.count(now)) now++;
      col[t] = now;
    }
  }
  for (int j = fir[x]; j; j = nxt[j])
    if (son[j] != pre) dfs(son[j], x);
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &cnt[i]);
    for (int j = 1; j <= cnt[i]; j++) {
      int x;
      scanf("%d", &x);
      V[i].push_back(x);
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 1);
  int _max = 1;
  for (int i = 1; i <= m; i++) {
    if (!col[i]) col[i] = 1;
    _max = max(_max, col[i]);
  }
  printf("%d\n", _max);
  for (int i = 1; i <= m; i++) printf("%d ", col[i]);
  return 0;
}
