#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const int MAXN = 3e5 + 5;
const long long INF = 1LL << 30;
const double eps = 1e-9;
const double PI = 3.1415926535897932;
vector<int> s[300009];
vector<int> adj[300009];
int c[300009];
void dfs(int x, int p = -1) {
  unordered_set<int> st;
  for (int i : s[x]) {
    if (c[i] != -1) {
      st.insert(c[i]);
    }
  }
  int cc = 0;
  for (int i : s[x]) {
    if (c[i] == -1) {
      while (1) {
        if (st.find(cc) != st.end())
          cc++;
        else {
          c[i] = cc;
          st.insert(cc);
          break;
        }
      }
    }
  }
  for (int i : adj[x]) {
    if (i == p) continue;
    dfs(i, x);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (n); ++i) {
    int(x);
    scanf("%d", &x);
    for (int j = 0; j < (x); ++j) {
      int(y);
      scanf("%d", &y);
      y--;
      s[i].push_back(y);
    }
  }
  memset((c), -1, sizeof((c)));
  for (int i = 0; i < (n - 1); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(0, -1);
  for (int i = 0; i < (m); ++i) {
    if (c[i] == -1) c[i] = 0;
    c[i]++;
  }
  int maxc = 0;
  for (int i = 0; i < (m); ++i) maxc = max(maxc, c[i]);
  printf("%d\n", maxc);
  for (int i = 0; i < (m); ++i) printf("%d ", c[i]);
}
