#include <bits/stdc++.h>
#define s 0
#define t (m+n+1)
using namespace std;
const int maxn=1e2+2,maxm=5e3+2e2+1,inf=1e9;
int m,n,a,tot=1,head[maxn],dep[maxn],ans,sum;
struct edge
{
int to,next,w;
}e[maxm];
void addedge(int x,int y,int w)
{
e[++tot].to=y;e[tot].w=w;e[tot].next=head[x];head[x]=tot;
e[++tot].to=x;e[tot].w=0;e[tot].next=head[y];head[y]=tot;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
queue<int>q;
q.push(s);dep[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(w&&!dep[y])
{
dep[y]=dep[x]+1;
q.push(y);
}
}
}
return dep[t];
}
int dfs(int x,int flow)
{
if(x==t)return flow;
int sum=0;
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(w&&dep[y]==dep[x]+1)
{
int f=dfs(y,min(flow,w));
e[i].w-=f;e[i^1].w+=f;
flow-=f;sum+=f;
}
}
if(!sum)dep[x]=0;
return sum;
}
int main()
{
ios::sync_with_stdio(0);
scanf("%d %d",&m,&n);
for(int i=1;i<=m;i++)
{
scanf("%d",&a);sum+=a;
addedge(s,i,a);
while(getchar()==' ')
{
scanf("%d",&a);
addedge(i,m+a,inf);
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
addedge(m+i,t,a);
}
while(bfs())ans+=dfs(s,inf);
for(int i=1;i<=m;i++)if(dep[i])printf("%d ",i);printf("\n");
for(int i=m+1;i<=m+n;i++)if(dep[i])printf("%d ",i-m);printf("\n");
printf("%d\n",sum-ans);
return 0;
}