#include <bits/stdc++.h>
using namespace std;
const int mo = 1e9 + 7;
const int mx = 500010;
int n, m, k, a[1010], c[1010], ans, sum;
int jud[mx];
set<int> s[mx];
set<int>::iterator it;
vector<int> vc[mx];
void dfs(int y, int pre) {
  vector<int> v;
  if (pre == -1) {
    int t = 1;
    for (it = s[y].begin(); it != s[y].end(); it++) {
      jud[*it] = t++;
      sum = max(sum, t - 1);
    }
  } else {
    for (it = s[y].begin(); it != s[y].end(); it++) {
      if (jud[(*it)]) v.push_back(jud[(*it)]);
    }
    int vn = v.size();
    sort(v.begin(), v.end());
    int f = 0, t = 1;
    for (it = s[y].begin(); it != s[y].end(); it++) {
      if (!jud[(*it)]) {
        while (f < vn && v[f] == t) {
          f++;
          t++;
        }
        jud[(*it)] = t++;
      }
      sum = max(sum, t - 1);
    }
  }
  int nm = vc[y].size();
  for (int i = 0; i < nm; i++) {
    int vyi = vc[y][i];
    if (vyi != pre) dfs(vyi, y);
  }
}
int dxy[8] = {1, 0, -1, 0, 0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(false);
  int t, i, j, l, q, x, y, ss;
  int cas = 1, flag;
  cin >> n >> m;
  {
    l = max(n, m);
    for (i = 0; i <= l; i++) {
      vc[i].clear();
      jud[i] = 0;
    }
    sum = 1;
    for (i = 1; i <= n; i++) {
      cin >> ss;
      for (j = 0; j < ss; j++) {
        cin >> x;
        s[i].insert(x);
      }
    }
    for (i = 0; i < n - 1; i++) {
      cin >> x >> y;
      vc[x].push_back(y);
      vc[y].push_back(x);
    }
    dfs(1, -1);
    cout << sum << endl;
    for (i = 1; i <= m; i++) {
      if (jud[i])
        cout << jud[i];
      else
        cout << "1";
      if (i != m)
        cout << " ";
      else
        cout << endl;
    }
  }
  return 0;
}
