#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1);
const long long INFF = (long long)1e18;
const int INF = (int)1e9;
const int mod = (int)1e9 + 7;
const int MAX = (int)3e5 + 7;
vector<int> edge[MAX];
vector<int> c[MAX];
int rt, mxn;
int n, m;
void init() {
  scanf("%d %d", &n, &m);
  for (int i = (1); i < (n + 1); ++i) {
    int num;
    scanf("%d", &num);
    ;
    for (int j = 0; j < (num); ++j) {
      int u;
      scanf("%d", &u);
      ;
      c[i].push_back(u);
    }
    if (num > mxn) {
      mxn = num;
      rt = i;
    }
  }
  for (int i = 0; i < (n - 1); ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    ;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  return;
}
int ans[MAX];
int pre[MAX];
int _t = 1;
void dfs(int u, int pa) {
  _t++;
  for (int v : c[u])
    if (ans[v]) pre[ans[v]] = _t;
  int p = 1;
  for (int v : c[u])
    if (!ans[v]) {
      while (pre[p] == _t) p++;
      ans[v] = p++;
    }
  for (int v : edge[u]) {
    if (v == pa) continue;
    dfs(v, u);
  }
  return;
}
void sol() {
  if (mxn == 0) {
    printf("1\n");
    for (int i = (1); i < (m + 1); ++i) printf("1 ");
  } else {
    for (int i = 0; i < (((int)c[rt].size())); ++i) ans[c[rt][i]] = i + 1;
    dfs(rt, 0);
    for (int i = (1); i < (m + 1); ++i)
      if (!ans[i]) ans[i] = 1;
    printf("%d\n", mxn);
    for (int i = (1); i < (m + 1); ++i) printf("%d ", ans[i]);
  }
  return;
}
int main(void) {
  init();
  sol();
  return 0;
}
