#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long INFL = 9223372036854775807LL;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;
vector<int> ice_creams[300000 + 5];
int colours[300000 + 5];
vector<int> adj[300000 + 5];
vector<int> order;
bool visited[300000 + 5];
void bfs(int start) {
  memset(visited, false, sizeof(visited));
  ;
  queue<int> q;
  q.push(start);
  visited[start] = true;
  while (!q.empty()) {
    int top = q.front();
    q.pop();
    order.push_back(top);
    for (int i = 0; i < (int)(adj[top]).size(); i++) {
      int next = adj[top][i];
      if (!visited[next]) {
        visited[next] = true;
        q.push(next);
      }
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  ;
  int m;
  scanf("%d", &m);
  ;
  int max_colours = 1;
  for (int i = 0; i < n; i++) {
    int c;
    scanf("%d", &c);
    ;
    for (int j = 0; j < c; j++) {
      int x;
      scanf("%d", &x);
      ;
      ice_creams[i].push_back(x - 1);
    }
    max_colours = ((max_colours) > (c) ? (max_colours) : (c));
  }
  for (int i = 0; i < n - 1; i++) {
    int a;
    scanf("%d", &a);
    ;
    int b;
    scanf("%d", &b);
    ;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  bfs(0);
  memset(colours, -1, sizeof(colours));
  ;
  int new_colour = 0;
  set<int> avail;
  for (int ii = 0; ii < n; ii++) {
    int i = order[ii];
    for (int j = 0; j < (int)(ice_creams[i]).size(); j++) {
      int ic = ice_creams[i][j];
      if (colours[ic] != -1) {
        avail.erase(colours[ic]);
      }
    }
    for (int j = 0; j < (int)(ice_creams[i]).size(); j++) {
      int ic = ice_creams[i][j];
      if (colours[ic] == -1) {
        if (avail.empty()) {
          colours[ic] = new_colour++;
        } else {
          set<int>::iterator it = avail.begin();
          colours[ic] = *it;
          avail.erase(it);
        }
      }
    }
    for (int j = 0; j < (int)(ice_creams[i]).size(); j++) {
      int ic = ice_creams[i][j];
      avail.insert(colours[ic]);
    }
  }
  cout << max_colours << endl;
  for (int i = 0; i < m; i++) {
    if (colours[i] != -1)
      printf("%d ", colours[i] + 1);
    else
      printf("1 ");
  }
  printf("\n");
  return 0;
}
