#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 500;
const int INF = 0x3f3f3f3f;
struct Edge {
int from, to, cap, flow;
};
struct Dinic {
int s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init() {
edges.clear();
for (int i = 0; i < maxn; i++) G[i].clear();
}
void addedge(int from, int to, int cap) {
edges.push_back(Edge{from, to, cap, 0});
edges.push_back(Edge{to, from, 0, 0});
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs() {
memset(vis, 0, sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == 0) return a;
int flow = 0, f;
for (int &i = cur[x]; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s, this->t = t;
int flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, INF);
}
return flow;
}
}ac;
int main()
{
int m, n;
scanf("%d%d", &m, &n);
ac.init();
int s = 0, t = n + m + 1;
int sum = 0;
int C;
char c;
for (int i = 1; i <= m; i++) {
scanf("%d", &C);
ac.addedge(s, i, C);
sum += C;
int j;
while ((c = getchar()) != '\n') {
scanf("%d", &j);
ac.addedge(i, j+m, INF);
}
}
for (int i = 1; i <= n; i++) {
scanf("%d", &C);
ac.addedge(i+m, t, C);
}
int maxf = ac.maxflow(s, t);
for (int i = 1; i <= m; i++) {
if (ac.vis[i]) printf("%d ", i);
}
printf("\n");
for (int i = m+1; i <= m+n; i++) {
if (ac.vis[i]) printf("%d ", i-m);
}
printf("\n%d\n", sum - maxf);
return 0;
}