#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i = (a);i<=(b);i++)
#define ROF(i,a,b) for(int i = (a);i>=(b);i--)
#define MST(a,x) memset(a,x,sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 105
#define M 100005
#define INF 1000000007
#define MOD 1000000007
#define MOD2 1000000009
#define eps 1e-8
using namespace std;

vector<int>e[N];
ll dp[N][55][55];
ll f[55][55];
int dep[N];
int n,k;
void dfs(int u,int fa)
{
    int max_dep = 0;
    MST(dp[u],0);
    dp[u][1][0] = dp[u][2*k+2][1] = 1;
    for(auto v:e[u])
    {
        if(v == fa)continue;
        dfs(v,u);
        max_dep = max(max_dep,dep[v]);

        MST(f,0);
        for(int i = 0;i<=2*k+1;i++)
        {
            for(int j = -1;j<=2*k;j++)
            {
                for(int ii = 0;ii<=2*k+1;ii++)
                {
                    for(int jj = -1;jj<=2*k;jj++)
                    {
                        int new_i = min(i,ii+1);

                        int dep1 = jj;// == -1?dep[v]:jj;
                        int new_j1 = jj == -1?-1:(k-i>=dep1+1?-1:dep1+1);

                        int dep2 = j;// == -1?dep[v]:j;
                        int new_j2 = j == -1?-1:(k-ii-1>=dep2?-1:dep2);
                        int new_j = max(new_j1,new_j2);

                        //cout<<new_i<<" "<<new_j<<endl;
                        //cout<<i<<" "<<j<<" "<<ii<<" "<<jj<<" "<<dp[u][i+1][j+1]<<" "<<dp[v][ii+1][jj+1]<<endl;
                        //cout<<"***************"<<endl;

                        f[new_i+1][new_j+1] = (f[new_i+1][new_j+1]+dp[u][i+1][j+1]*dp[v][ii+1][jj+1]%MOD)%MOD;
                    }
                }
            }
        }
        for(int i = 0;i<=2*k+1;i++)
        {
            for(int j = -1;j<=2*k;j++)
            {
                //cout<<u<<" "<<i<<" "<<j<<" "<<f[i+1][j+1]<<endl;
                dp[u][i+1][j+1] = f[i+1][j+1];
            }
        }
    }
    dep[u] = max_dep+1;
}
int main()
{
    while(cin>>n>>k)
    {
        FOR(i,1,n)e[i].clear();
        FOR(i,1,n-1)
        {
            int x,y;
            cin>>x>>y;
            e[x].PB(y);
            e[y].PB(x);
        }
        dfs(1,-1);
        ll ans = 0;
        for(int i = 0;i<=k;i++)for(int j = -1;j<k;j++)/*cout<<i<<" "<<j<<" "<<dp[1][i+1][j+1]<<endl,*/ans = (ans+dp[1][i+1][j+1])%MOD;
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
using namespace std;
int n,k,u,v;
long long ans,fl[25],fw[25],dp[2][105][25];
vector<int> G[105];
void dfs(int u,int fa)
{
	for(int v : G[u])
	 if(v != fa) dfs(v,u);
	dp[0][u][0] = dp[1][u][0] = 1;             //����������ǳ�Ͳ�������������
	for(int v : G[u])
	 if(v != fa)
	 {
	 	memset(fl,0,sizeof(fl));
		memset(fw,0,sizeof(fw));
	 	for(int i = 0;i <= k;i++)
		{
	 		for(int j = 0;j <= k;j++)           //win win
	 	  	 fw[min(i,j+1)] += dp[0][u][i] * dp[0][v][j];
	 	  	for(int j = 0;j <= k;j++)           //win lose
	 	  	 if(k - i >= j+1) fw[i] += dp[0][u][i] * dp[1][v][j];
			 else fl[j+1] += dp[0][u][i] * dp[1][v][j];
		}
		for(int i = 0;i <= k;i++)
		{
			for(int j = 0;j <= k;j++)         //lose win
			 if(k - j - 1 >= i) fw[j+1] += dp[1][u][i] * dp[0][v][j];
			 else fl[i] += dp[1][u][i] * dp[0][v][j];
			for(int j = 0;j <= k;j++)          // lose lose
			 fl[max(i,j+1)] += dp[1][u][i] * dp[1][v][j];
		}
		for(int i = 0;i <= k;i++)
		{
			dp[0][u][i] = fw[i] % MOD;
			dp[1][u][i] = fl[i] % MOD;
		}
	 }
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>k;
	for(int i = 1;i < n;i++)
	{
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1,-1);
	for(int i = 0;i <= k;i++) ans = (ans + dp[0][1][i]) % MOD;
	cout<<ans<<endl;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
using namespace std;
int n,k,u,v;
long long ans,fl[25],fw[25],dp[2][105][25];
vector<int> G[105];
void dfs(int u,int fa)
{
	for(int v : G[u])
	 if(v != fa) dfs(v,u);
	dp[0][u][0] = dp[1][u][0] = 1;             //����������ǳ�Ͳ�������������
	for(int v : G[u])
	 if(v != fa)
	 {
	 	memset(fl,0,sizeof(fl));
		memset(fw,0,sizeof(fw));
	 	for(int i = 0;i <= k;i++)
		{
	 		for(int j = 0;j <= k;j++)           //win win
	 	  	 fw[min(i,j+1)] += dp[0][u][i] * dp[0][v][j] % MOD;
	 	  	for(int j = 0;j <= k;j++)           //win lose
	 	  	 if(k - i >= j+1) fw[i] += dp[0][u][i] * dp[1][v][j] % MOD;
			 else fl[j+1] += dp[0][u][i] * dp[1][v][j] % MOD;
		}
		for(int i = 0;i <= k;i++)
		{
			for(int j = 0;j <= k;j++)         //lose win
			 if(k - j - 1 >= i) fw[j+1] += dp[1][u][i] * dp[0][v][j] % MOD;
			 else fl[i] += dp[1][u][i] * dp[0][v][j] % MOD;
			for(int j = 0;j <= k;j++)          // lose lose
			 fl[max(i,j+1)] += dp[1][u][i] * dp[1][v][j] % MOD;
		}
		for(int i = 0;i <= k;i++)
		{
			dp[0][u][i] = fw[i] % MOD;
			dp[1][u][i] = fl[i] % MOD;
		}
	 }
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>k;
	for(int i = 1;i < n;i++)
	{
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1,-1);
	for(int i = 0;i <= k;i++) ans = (ans + dp[0][1][i]) % MOD;
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
using namespace std;
ll dp[101][44],f[44],md=1e9+7,ans;
int n,k;
vector<int>g[101];
inline void add(ll&x,ll y){x=(x+y)%md;}
void dfs(int u,int par){
	dp[u][0]=dp[u][k+1]=1;
	for(int v:g[u])if(v!=par){
		dfs(v,u);
		memset(f,0,sizeof(f));
		for(int i=0;i<=k<<1;++i)for(int j=0;j<=k<<1;++j)if(i+j<=k<<1)add(f[min(i,j+1)],dp[u][i]*dp[v][j]);else add(f[max(i,j+1)],dp[u][i]*dp[v][j]);
		for(int i=0;i<=k<<1;++i)dp[u][i]=f[i];
	}
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<n;++i){
		int u,v;scanf("%d%d",&u,&v);
		g[u].push_back(v),g[v].push_back(u);
	}
	dfs(1,0);
	for(int i=0;i<=k;++i)add(ans,dp[1][i]);
	printf("%lld\n",ans);
}//hellobig_tq


#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

const int MOD = 1e9 + 7;

inline void ModSum(int& x, int y) {
    x += y;
    if (x >= MOD) {
        x -= MOD;
    }
    if (x < 0) {
        x += MOD;
    }
}

inline int GetModSum(int x, int y) {
    ModSum(x, y);
    return x;
}

inline void ModMul(int& x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int GetModMul(int x, int y) {
    ModMul(x, y);
    return x;
}

const int MAX_N = 100;
const int MAX_K = 20;

int n, k;
vector<vector<int>> g;

vector<int> height;

void BuildHeight(int v, int pr) {
    height[v] = 1;
    for (int u : g[v]) {
        if (u != pr) {
            BuildHeight(u, v);
            height[v] = max(height[v], height[u] + 1);
        }
    }
}

int dp[MAX_N][MAX_K + 2][MAX_N + 1];

int dp2[MAX_N + 1][MAX_N + 1];

void BuildDp(int v, int pr) {
    vector<int> childs;
    for (int u : g[v]) {
        if (u == pr) {
            continue;
        }
        childs.push_back(u);
        BuildDp(u, v);
    }
    const int m = childs.size();
    vector<int> childHeight(m + 1);
    childHeight[0] = 1;
    for (int i = 1; i <= m; ++i) {
        childHeight[i] = max(childHeight[i - 1], height[childs[i - 1]] + 1);
    }
    for (int distUp = 0; distUp <= k + 1; ++distUp) {
        for (int distDown = 0; distDown <= height[v]; ++distDown) {
            for (int black = 0; black < 2; ++black) {
                if (black && distDown != 0) {
                    continue;
                }

                int newDistUp = k + 1;
                if (distUp != k + 1) {
                    newDistUp = min(newDistUp, distUp + 1);
                }
                if (distDown != height[v]) {
                    newDistUp = min(newDistUp, distDown + 1);
                }
                newDistUp = min(newDistUp, k + 1);

                for (int i = 0; i <= m; ++i) {
                    for (int j = 0; j <= childHeight[i]; ++j) {
                        dp2[i][j] = 0;
                    }
                }
                dp2[0][childHeight[0]] = 1;
                for (int i = 0; i < m; ++i) {
                    for (int prevDistDown = 0; prevDistDown <= childHeight[i]; ++prevDistDown) {
                        for (int curDistDown = 0; curDistDown <= height[childs[i]]; ++curDistDown) {
                            int newDistDown;
                            if (prevDistDown == childHeight[i]) {
                                if (curDistDown == height[childs[i]]) {
                                    newDistDown = childHeight[i + 1];
                                } else {
                                    newDistDown = curDistDown + 1;
                                }
                            } else {
                                if (curDistDown == height[childs[i]]) {
                                    newDistDown = prevDistDown;
                                } else {
                                    newDistDown = min(prevDistDown, curDistDown + 1);
                                }
                            }

                            ModSum(dp2[i + 1][newDistDown], GetModMul(dp2[i][prevDistDown], dp[childs[i]][newDistUp][curDistDown]));
                        }
                    }
                }

                if (black) {
                    for (int dist = 0; dist <= height[v]; ++dist) {
                        ModSum(dp[v][distUp][distDown], dp2[m][dist]);
                    }
                } else {
                    if (distUp <= k || (distDown != height[v] && distDown <= k)) {
                        ModSum(dp[v][distUp][distDown], dp2[m][distDown]);
                    }
                }
            }
        }
    }
}

void Solve() {
    height.resize(n);
    BuildHeight(0, -1);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k + 1; ++j) {
            for (int t = 0; t <= height[i]; ++t) {
                dp[i][j][t] = 0;
            }
        }
    }
    BuildDp(0, -1);
    int result = 0;
    for (int d = 0; d <= height[0]; ++d) {
        ModSum(result, dp[0][k + 1][d]);
    }
    cout << result << endl;
}

bool Read() {
    if (!(cin >> n >> k)) {
        return false;
    }
    g.clear();
    g.resize(n);
    for (int i = 0; i + 1 < n; ++i) {
        int v1, v2;
        cin >> v1 >> v2;
        --v1;
        --v2;
        g[v1].push_back(v2);
        g[v2].push_back(v1);
    }
    return true;
}

int main() {
    // freopen("input.txt", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    while (Read()) {
        Solve();
    }

    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

const int MOD = 1e9 + 7;

inline void ModSum(int& x, int y) {
    x += y;
    if (x >= MOD) {
        x -= MOD;
    }
    if (x < 0) {
        x += MOD;
    }
}

inline int GetModSum(int x, int y) {
    ModSum(x, y);
    return x;
}

inline void ModMul(int& x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int GetModMul(int x, int y) {
    ModMul(x, y);
    return x;
}

const int MAX_N = 100;
const int MAX_K = 20;

int n, k;
vector<vector<int>> g;

vector<int> height;

void BuildHeight(int v, int pr) {
    height[v] = 1;
    for (int u : g[v]) {
        if (u != pr) {
            BuildHeight(u, v);
            height[v] = max(height[v], height[u] + 1);
        }
    }
}

int dp[MAX_N][MAX_K + 2][MAX_N + 1];

int dp2[MAX_N + 1][MAX_N + 1];

void BuildDp(int v, int pr) {
    vector<int> childs;
    for (int u : g[v]) {
        if (u == pr) {
            continue;
        }
        childs.push_back(u);
        BuildDp(u, v);
    }
    const int m = childs.size();
    vector<int> childHeight(m + 1);
    childHeight[0] = 1;
    for (int i = 1; i <= m; ++i) {
        childHeight[i] = max(childHeight[i - 1], height[childs[i - 1]] + 1);
    }
    for (int distUp = 0; distUp <= k + 1; ++distUp) {
        for (int distDown = 0; distDown <= height[v]; ++distDown) {
            for (int black = 0; black < 2; ++black) {
                if (black && distDown != 0) {
                    continue;
                }

                int newDistUp = min(distUp + 1, distDown + 1);
                newDistUp = min(newDistUp, k + 1);

                for (int i = 0; i <= m; ++i) {
                    for (int j = 0; j <= childHeight[i]; ++j) {
                        dp2[i][j] = 0;
                    }
                }
                dp2[0][childHeight[0]] = 1;
                for (int i = 0; i < m; ++i) {
                    for (int prevDistDown = 0; prevDistDown <= childHeight[i]; ++prevDistDown) {
                        for (int curDistDown = 0; curDistDown <= height[childs[i]]; ++curDistDown) {
                            int newDistDown;
                            if (prevDistDown == childHeight[i]) {
                                if (curDistDown == height[childs[i]]) {
                                    newDistDown = childHeight[i + 1];
                                } else {
                                    newDistDown = curDistDown + 1;
                                }
                            } else {
                                if (curDistDown == height[childs[i]]) {
                                    newDistDown = prevDistDown;
                                } else {
                                    newDistDown = min(prevDistDown, curDistDown + 1);
                                }
                            }

                            ModSum(dp2[i + 1][newDistDown], GetModMul(dp2[i][prevDistDown], dp[childs[i]][newDistUp][curDistDown]));
                        }
                    }
                }

                if (black) {
                    for (int dist = 0; dist <= height[v]; ++dist) {
                        ModSum(dp[v][distUp][distDown], dp2[m][dist]);
                    }
                } else {
                    if (distUp <= k || (distDown != height[v] && distDown <= k)) {
                        ModSum(dp[v][distUp][distDown], dp2[m][distDown]);
                    }
                }
            }
        }
    }
}

void Solve() {
    height.resize(n);
    BuildHeight(0, -1);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k + 1; ++j) {
            for (int t = 0; t <= height[i]; ++t) {
                dp[i][j][t] = 0;
            }
        }
    }
    BuildDp(0, -1);
    int result = 0;
    for (int d = 0; d <= height[0]; ++d) {
        ModSum(result, dp[0][k + 1][d]);
    }
    cout << result << endl;
}

bool Read() {
    if (!(cin >> n >> k)) {
        return false;
    }
    g.clear();
    g.resize(n);
    for (int i = 0; i + 1 < n; ++i) {
        int v1, v2;
        cin >> v1 >> v2;
        --v1;
        --v2;
        g[v1].push_back(v2);
        g[v2].push_back(v1);
    }
    return true;
}

int main() {
    // freopen("input.txt", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    while (Read()) {
        Solve();
        break;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

int n, K;
vector<int>g[105];
int dp[105][55], tmp[55], ma[105];

inline void add(int &x, int y) {
	x += y;
	if(x >= mod) x -= mod;
}

void go(int u, int fa) {
	dp[u][0] = 1, dp[u][K + 1] = 1;
	for(int i = 0; i < (int)g[u].size(); ++ i) {
		int v = g[u][i];
		if(v == fa) continue;
		go(v, u);
		memset(tmp, 0, sizeof tmp);
		for(int j = 0; j <= 2 * K + 1; ++ j) {
			for(int k = 0; k <= 2 * K; ++ k) {
				if(j + k <= 2 * K) add(tmp[min(j, k + 1)], 1LL * dp[u][j] * dp[v][k] % mod);
				else add(tmp[max(j, k + 1)], 1LL * dp[u][j] * dp[v][k] % mod);
			}
		}
		for(int j = 0; j <= 2 * K; ++ j) dp[u][j] = tmp[j];
	}
}

int main () {
	scanf("%d%d", &n, &K);
	for(int i = 1; i < n; ++ i) {
		int u, v; scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	if(K == 0) return printf("1\n"), 0;
	go(1, -1);
	int ans = 0;
	for(int i = 0; i <= K; ++ i) add(ans, dp[1][i]);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

int n, K;
vector<int>g[105];
int dp[105][55], tmp[55], ma[105];

inline void add(int &x, int y) {
	x += y;
	if(x >= mod) x -= mod;
}

void go(int u, int fa) {
	dp[u][0] = 1, dp[u][K + 1] = 1;
	for(int i = 0; i < (int)g[u].size(); ++ i) {
		int v = g[u][i];
		if(v == fa) continue;
		go(v, u);
		memset(tmp, 0, sizeof tmp);
		for(int j = 0; j <= 2 * K; ++ j) {
			for(int k = 0; k <= 2 * K; ++ k) {
				if(j + k <= 2 * K) add(tmp[min(j, k + 1)], 1LL * dp[u][j] * dp[v][k] % mod);
				else add(tmp[max(j, k + 1)], 1LL * dp[u][j] * dp[v][k] % mod);
			}
		}
		for(int j = 0; j <= 2 * K; ++ j) dp[u][j] = tmp[j];
	}
}

int main () {
	scanf("%d%d", &n, &K);
	for(int i = 1; i < n; ++ i) {
		int u, v; scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	if(K == 0) return printf("1\n"), 0;
	go(1, -1);
	int ans = 0;
	for(int i = 0; i <= K; ++ i) add(ans, dp[1][i]);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 109;
const ll mod = 1000000007;

int n, d;
vector<int> neigh[maxn];

ll f[maxn][maxn], g[maxn][maxn];
ll tmp_f[maxn];
ll tmp_g[maxn];

void dfs(int v, int p)
{
    f[v][0] = 1;
    g[v][0] = 1;

    for (int u : neigh[v]) {
        if (u == p) continue;
        dfs(u, v);

        memset(tmp_f, 0, sizeof tmp_f);
        memset(tmp_g, 0, sizeof tmp_g);

        for (int k = 0; k <= d; k++) {
            for (int kk = 0; kk <= d; kk++) {
                ll mult;

                // f f
                mult = (f[v][k] * f[u][kk]) % mod;
                tmp_f[min(k, kk + 1)] += mult;

                // f g
                mult = (f[v][k] * g[u][kk]) % mod;
                if (k + kk + 1 <= d) {
                    tmp_f[k] += mult;
                }
                else {
                    tmp_g[kk + 1] += mult;
                }

                // g f
                mult = (g[v][k] * f[u][kk]) % mod;
                if (k + kk + 1 <= d) {
                    tmp_f[kk + 1] += mult;
                }
                else {
                    tmp_g[k] += mult;
                }

                // g g
                mult = (g[v][k] * g[u][kk]) % mod;
                tmp_g[max(k, kk + 1)] += mult;
            }
        }

        for (int k = 0; k <= d; k++) {
            f[v][k] = tmp_f[k] % mod;
            g[v][k] = tmp_g[k] % mod;
        }
    }
}

int main()
{
    scanf("%d %d", &n, &d);

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        neigh[u].push_back(v);
        neigh[v].push_back(u);
    }

    dfs(1, 0);

    ll ans = 0;
    for (int k = 0; k <= d; k++) ans += f[1][k];
    cout << ans % mod;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 109;
const ll mod = 1000000007;

int n, d;
vector<int> neigh[maxn];

ll f[maxn][maxn], g[maxn][maxn];
ll tmp_f[maxn];
ll tmp_g[maxn];

void dfs(int v, int p)
{
    f[v][0] = 1;
    g[v][0] = 1;

    for (int u : neigh[v]) {
        if (u == p) continue;
        dfs(u, v);

        memset(tmp_f, 0, sizeof tmp_f);
        memset(tmp_g, 0, sizeof tmp_g);

        for (int k = 0; k <= d; k++) {
            for (int kk = 0; kk <= d; kk++) {
                ll mult;

                // f f
                mult = (f[v][k] * f[u][kk]) % mod;
                tmp_f[min(k, kk + 1)] = (tmp_f[min(k, kk + 1)] + mult) % mod;

                // f g
                mult = (f[v][k] * g[u][kk]) % mod;
                if (k + kk + 1 <= d) {
                    tmp_f[k] = (tmp_f[k] + mult) % mod;
                }
                else {
                    tmp_g[kk + 1] = (tmp_g[kk + 1] + mult) % mod;
                }

                // g f
                mult = (g[v][k] * f[u][kk]) % mod;
                if (k + kk + 1 <= d) {
                    tmp_f[kk + 1] = (tmp_f[kk + 1] + mult) % mod;
                }
                else {
                    tmp_g[k] = (tmp_g[k] + mult) % mod;
                }

                // g g
                mult = (g[v][k] * g[u][kk]) % mod;
                tmp_g[max(k, kk + 1)] = (tmp_g[max(k, kk + 1)] + mult) % mod;

                if (v == 2 && tmp_g[1] > 0) {
                    //cout << f[v][k] << " " << f[u][kk] << endl;
                    //cout << k << " " << kk << endl; exit(0);
                }
            }
        }

        for (int k = 0; k <= d; k++) {
            f[v][k] = tmp_f[k];
            g[v][k] = tmp_g[k];
        }
    }
}

int main()
{
    scanf("%d %d", &n, &d);

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        neigh[u].push_back(v);
        neigh[v].push_back(u);
    }

    dfs(1, 0);
    ll ans = 0;
    for (int k = 0; k <= d; k++) {
        ans = (ans + f[1][k]) % mod;
    }

    cout << ans;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 110;
const int M = N + N;
const int Mod = 1e9 + 7;

int n, m;
int head[N], dest[M], last[M], etot;
int up[N][30], dn[N][30];
int tup[2][30], tdn[2][30];
int siz[N];

void adde( int u, int v ) {
	etot++;
	dest[etot] = v;
	last[etot] = head[u];
	head[u] = etot;
}
inline void upd( int &dpv, int val ) {
	dpv += val;
	if( dpv >= Mod ) dpv -= Mod;
}
void dfs( int u, int f ) {
	siz[u] = 1;
	for( int t = head[u]; t; t = last[t] ) {
		int v = dest[t];
		if( v == f ) continue;
		dfs( v, u );
		siz[u] += siz[v];
	}
	int cur = 0, nxt = 1;
	memset( tup, 0, sizeof(tup) );
	memset( tdn, 0, sizeof(tdn) );
	tup[cur][m] = 1;
	tdn[cur][1] = 1;
	for( int t = head[u]; t; t = last[t] ) {
		int v = dest[t];
		if( v == f ) continue;
		memset( tup[nxt], 0, sizeof(tup[nxt]) );
		memset( tdn[nxt], 0, sizeof(tdn[nxt]) );
		for( int i = 0; i <= m; i++ ) {
			for( int j = 0; j <= m; j++ )
				upd( tup[nxt][max(i,j-1)], 1LL * tup[cur][i] * up[v][j] % Mod );
			for( int j = 1; j <= m; j++ ) {
				if( i >= j )
					upd( tup[nxt][i], 1LL * tup[cur][i] * dn[v][j] % Mod );
				else
					upd( tdn[nxt][j+1], 1LL * tup[cur][i] * dn[v][j] % Mod );
			}
		}
		for( int i = 1; i <= m; i++ ) {
			for( int j = 1; j <= m; j++ )
				upd( tdn[nxt][max(i,j+1)], 1LL * tdn[cur][i] * dn[v][j] % Mod );
			for( int j = 0; j <= m; j++ ) {
				if( j >= i )
					upd( tup[nxt][j - 1], 1LL * tdn[cur][i] * up[v][j] % Mod );
				else
					upd( tdn[nxt][i], 1LL * tdn[cur][i] * up[v][j] % Mod );
			}
		}
		swap( cur, nxt );
	}
	for( int i = 0; i <= m; i++ ) {
		up[u][i] = tup[cur][i];
		//printf( "up[%d][%d] = %d\n", u, i, up[u][i] );
	}
	for( int i = 1; i <= m; i++ ) {
		dn[u][i] = tdn[cur][i];
		//printf( "dn[%d][%d] = %d\n", u, i, dn[u][i] );
	}
}
int main() {
	scanf( "%d%d", &n, &m );
	for( int i = 1; i < n; i++ ) {
		int u, v;
		scanf( "%d%d", &u, &v );
		adde( u, v );
		adde( v, u );
	}
	if( m == 0 ) {
		printf( "1\n" );
	} else {
		dfs( 1, 0 );
		int ans = 0;
		for( int i = 0; i <= m; i++ )
			upd( ans, up[1][i] );
		upd( ans, dn[1][0] );
		printf( "%d\n", ans );
	}
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 110;
const int M = N + N;
const int Mod = 1e9 + 7;

int n, m;
int head[N], dest[M], last[M], etot;
int dp[N][45][45], ep[2][45][45];
int siz[N];

void adde( int u, int v ) {
	etot++;
	dest[etot] = v;
	last[etot] = head[u];
	head[u] = etot;
}
inline void upd( int &dpv, int val ) {
	dpv += val;
	if( dpv >= Mod ) dpv -= Mod;
}
void dfs( int u, int f ) {
	siz[u] = 1;
	for( int t = head[u]; t; t = last[t] ) {
		int v = dest[t];
		if( v == f ) continue;
		dfs( v, u );
		siz[u] += siz[v];
	}
	if( siz[u] == 1 ) {
		dp[u][m+1][m+1] = 1;
		dp[u][0][0] = 1;
	} else {
		memset( ep, 0, sizeof(ep) );
		int cur = 0, nxt = 1;
		ep[cur][m + m + 1][0] = 1;
		for( int t = head[u]; t; t = last[t] ) {
			int v = dest[t];
			if( v == f ) continue;
			memset( ep[nxt], 0, sizeof(ep[nxt]) );
			for( int dmin = 0; dmin <= m + m + 1; dmin++ )
				for( int dmax = 0; dmax <= m + m + 1; dmax++ ) {
					if( ep[cur][dmin][dmax] == 0 ) continue;
					for( int cmin = 0; cmin <= m + m + 1; cmin++ )
						for( int cmax = cmin; cmax <= m + m + 1; cmax++ ) {
							upd( ep[nxt][min(dmin,cmin+1)][max(dmax,cmax+1)], 1LL * ep[cur][dmin][dmax] * dp[v][cmin][cmax] % Mod );
						}
				}
			swap( cur, nxt );
		}
		memset( dp[u], 0, sizeof(dp[u]) );
		for( int dmin = 0; dmin <= m + m + 1; dmin++ )
			for( int dmax = dmin; dmax <= m + m + 1; dmax++ ) {
				upd( dp[u][dmin][dmax], ep[cur][dmin][dmax] );
				upd( dp[u][0][0], ep[cur][dmin][dmax] );
			}
	}
}
int main() {
	scanf( "%d%d", &n, &m );
	for( int i = 1; i < n; i++ ) {
		int u, v;
		scanf( "%d%d", &u, &v );
		adde( u, v );
		adde( v, u );
	}
	dfs( 1, 0 );
	int ans = 0;
	for( int dmin = 0; dmin <= m + m + 1; dmin++ )
		for( int dmax = dmin; dmax <= m + m + 1 - dmin; dmax++ )
			upd( ans, dp[1][dmin][dmax] );
	printf( "%d\n", ans );
}


#include<cstdio>
#include<cctype>
#include<algorithm>
#include<forward_list>
typedef long long int64;
inline int getint() {
	register char ch;
	while(!isdigit(ch=getchar()));
	register int x=ch^'0';
	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
	return x;
}
const int N=101,K=41,mod=1e9+7;
int k,f[N][K],tmp[K];
std::forward_list<int> e[N];
inline void add_edge(const int &u,const int &v) {
	e[u].push_front(v);
	e[v].push_front(u);
}
void dfs(const int &x,const int &par) {
	f[x][0]=f[x][k+1]=1;
	for(int &y:e[x]) {
		if(y==par) continue;
		dfs(y,x);
		std::fill(&tmp[0],&tmp[k*2]+1,0);
		for(register int i=0;i<=k*2;i++) {
			for(register int j=0;j<=k*2;j++) {
				(tmp[i+j<=k*2?std::min(i,j+1):std::max(i,j+1)]+=(int64)f[x][i]*f[y][j]%mod)%=mod;
			}
		}
		std::copy(&tmp[0],&tmp[k*2]+1,f[x]);
	}
}
int main() {
	const int n=getint();k=getint();
	for(register int i=1;i<n;i++) {
		add_edge(getint(),getint());
	}
	dfs(1,0);
	int ans=0;
	for(register int i=0;i<=k;i++) {
		(ans+=f[1][i])%=mod;
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
#define PI 3.14159265358979323846
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

// Modular operations
const int MOD = 1E9 + 7;

int Add(int a, int b, int m = MOD){
  a += b;
  if(a >= m) a -= m;
  return a;
}

int Sub(int a, int b, int m = MOD){
  a -= b;
  if(a < 0) a += m;
  return a;
}

int AddTo(int& to, int w, int m = MOD){
  to += w;
  if(to >= m) to -= m;
  return to;
}

int SubTo(int& to, int w, int m = MOD){
  to -= w;
  if(to < 0) to += m;
  return to;
}

int Mult(int a, int b, int m = MOD){
  return (a * 1LL * b) % m;
}

int Pow(int a, ll deg, int m = MOD){
  int res = 1 % m;
  for(;deg > 0;deg >>= 1, a = Mult(a, a, m))
    if(deg & 1) res = Mult(res, a, m);
  return res;
}

int Oem(int to, int m = MOD){
  return Pow(to, m - 2, m);
}

int Div(int a, int b, int m = MOD){
  return Mult(a, Oem(b, m), m);
}

// Solution
const int MAXN = 110;
const int MAXD = 22;

//    vr    out    in
int dp[MAXN][MAXD][MAXD];
int n, k;
vi fo[MAXN];

void Dfs(int vr, int par = -1){
  if(par != -1)
    fo[vr].erase(find(all(fo[vr]), par));
  for(int nxt : fo[vr]) Dfs(nxt, vr);
}

int Eval(int vr, int out, int in){
  if(dp[vr][out][in] != -1)
    return dp[vr][out][in];

  int dp1 = 1;
  for(int nxt : fo[vr]){
    int tmp = 0;
    for(int h = 0;h <= k + 1;++h)
      AddTo(tmp, Mult(dp1, Eval(nxt, 1, h)));
    dp1 = tmp;
  }

  vi dp2(k + 2, 0);
  vi buf(k + 2, 0);
  dp2[k + 1] = 1;
  for(int nxt : fo[vr]){
    fill(all(buf), 0);
    for(int gh = 0;gh <= k + 1;++gh)
      for(int h = 0;h <= k + 1;++h){
        int go = min(gh, out) + 1;
        relaxMin(go, k + 1);
        AddTo(buf[min(gh, h + 1)],
              Mult(dp2[gh], Eval(nxt, go, h)));
      }
    dp2.swap(buf);
  }

  for(int i = 0;i <= k + 1;++i)
    if(i <= k || out <= k) dp[vr][out][i] = dp2[i];
    else dp[vr][out][i] = 0;
  AddTo(dp[vr][out][0], dp1);

  return dp[vr][out][in];
}

int main(){
  memset(dp, -1, sizeof dp);

  scanf("%d%d", &n, &k);
  for(int i = 1;i < n;++i){
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    fo[u].pb(v), fo[v].pb(u);
  }

  Dfs(0);

  int ans = 0;
  for(int i = 0;i <= k + 1;++i)
    AddTo(ans, Eval(0, k + 1, i));

  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
#define PI 3.14159265358979323846
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

// Modular operations
const int MOD = 1E9 + 7;

int Add(int a, int b, int m = MOD){
  a += b;
  if(a >= m) a -= m;
  return a;
}

int Sub(int a, int b, int m = MOD){
  a -= b;
  if(a < 0) a += m;
  return a;
}

int AddTo(int& to, int w, int m = MOD){
  to += w;
  if(to >= m) to -= m;
  return to;
}

int SubTo(int& to, int w, int m = MOD){
  to -= w;
  if(to < 0) to += m;
  return to;
}

int Mult(int a, int b, int m = MOD){
  return (a * 1LL * b) % m;
}

int MultTo(int& a, int b, int m = MOD){
  a = (a * 1LL * b) % m;
}

int Pow(int a, ll deg, int m = MOD){
  int res = 1 % m;
  for(;deg > 0;deg >>= 1, a = Mult(a, a, m))
    if(deg & 1) res = Mult(res, a, m);
  return res;
}

int Oem(int to, int m = MOD){
  return Pow(to, m - 2, m);
}

int Div(int a, int b, int m = MOD){
  return Mult(a, Oem(b, m), m);
}

// Solution
const int MAXN = 110;
const int MAXD = 22;

//    vr    out    in
int dp[MAXN][MAXD][MAXD];
int n, k;
vi fo[MAXN];

void Dfs(int vr, int par = -1){
  if(par != -1)
    fo[vr].erase(find(all(fo[vr]), par));
  for(int nxt : fo[vr]) Dfs(nxt, vr);
}

int Eval(int vr, int out, int in){
  if(dp[vr][out][in] != -1) return dp[vr][out][in];
  fill(dp[vr][out], dp[vr][out] + k + 2, 0);

  // full
  dp[vr][out][0] = 1;
  for(int nxt : fo[vr]){
    int sum = 0;
    for(int h = 0;h <= k + 1;++h)
      AddTo(sum, Eval(nxt, 1, h));
    MultTo(dp[vr][out][0], sum);
  }

  // empty
  for(int h = 0;h < k;++h)
    for(int m = 0;m < sz(fo[vr]);++m){
      int res = 1;
      MultTo(res,
             Eval(fo[vr][m], min(out + 1, k + 1), h));
      int dpt = min(k + 1, min(out + 1, h + 2));
      for(int i = 0;i < sz(fo[vr]);++i){
        if(i == m) continue;
        int sum = 0;
        for(int j = h + 1;j <= k + 1;++j)
          AddTo(sum, Eval(fo[vr][i], dpt, j));
        if(i > m)
          AddTo(sum, Eval(fo[vr][i], dpt, h));
        MultTo(res, sum);
      }
      AddTo(dp[vr][out][h + 1], res);
    }

  if(out <= k){
    int res = 1;
    for(int nxt : fo[vr])
      MultTo(res, Add(Eval(nxt, out + 1, k),
                      Eval(nxt, out + 1, k + 1)));
    AddTo(dp[vr][out][k + 1], res);
  }

  return dp[vr][out][in];
}

int main(){
  memset(dp, -1, sizeof dp);

  scanf("%d%d", &n, &k);
  for(int i = 1;i < n;++i){
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    fo[u].pb(v), fo[v].pb(u);
  }

  Dfs(0);

  int ans = 0;
  for(int h = 0;h <= k + 1;++h)
    AddTo(ans, Eval(0, k + 1, h));

  printf("%d\n", ans);
  return 0;
}


#pragma GCC optimize("O3")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define PB push_back
#define MP make_pair
#define INF 1e9+7
#define LLINF 4e18+7
#define ALL(a) a.begin(), a.end()
#define SIZE(a) (int)a.size()

typedef unsigned long long ULL;
typedef long long LL;
typedef double LD;

using namespace std;

//#define DEBUG

int M[3001][3001];

int main()
{
    #ifndef DEBUG
    BOOST;
    #endif

    int n, m;
    cin >> n >> m;

    vector <pair <int, int> > wins(n);

    FOR(i, 0, n)
    {
        if(i < m)
        {
            cin >> wins[i].first;

            if(wins[i].first > 2 * (n - 1 - i))
            {
                cout << "no";
                return 0;
            }
        }
        else
        {
            wins[i].first = min(wins[i - 1].first, 2 * (n - 1 - i));
        }

        wins[i].second = i;
    }

    cout << "yes\n";

    while(wins[0].first != -1)
    {
        for(int i = n - 1; i > 0; --i)
        {
            if(wins[i].first == -1)
                continue;

            if(wins[0].first >= 2)
            {
                M[wins[0].second][wins[i].second] = 2;
                wins[0].first -= 2;
            }
            else if(wins[0].first == 1)
            {
                M[wins[0].second][wins[i].second] = 1;
                M[wins[i].second][wins[0].second] = 1;

                --wins[i].first;
                --wins[0].first;
            }
            else
            {
                M[wins[i].second][wins[0].second] = 2;
                wins[i].first -= 2;
            }
        }

        wins[0].first = -1;
        sort(ALL(wins), greater<pair <int,int> >());
    }

    FOR(i, 0, n)
    {
        FOR(j, 0, n)
        {
            if(i == j)
                cout << "X";
            else if(M[i][j] == 2)
                cout << "W";
            else if(M[i][j] == 1)
                cout << "D";
            else
                cout << "L";
        }
        cout << "\n";
    }

    return 0;
}


#pragma GCC optimize("O3")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define PB push_back
#define MP make_pair
#define INF 1e9+7
#define LLINF 4e18+7
#define ALL(a) a.begin(), a.end()
#define SIZE(a) (int)a.size()

typedef unsigned long long ULL;
typedef long long LL;
typedef double LD;

using namespace std;

//#define DEBUG

int M[3001][3001];

int main()
{
    #ifndef DEBUG
    BOOST;
    #endif

    int n, m;
    cin >> n >> m;

    int sum = 0;
    int total = n * (n - 1);

    vector <pair <int, int> > wins(n);

    FOR(i, 0, n)
    {
        if(i < m)
        {
            cin >> wins[i].first;
        }
        else
        {
            wins[i].first = (total - sum + n - 1 - i) / (n - i);
        }

        sum += wins[i].first;
        wins[i].second = i;
    }

    if(sum < total)
    {
        cout << "no\n";
        return 0;
    }

    cout << "yes\n";

    while(wins[0].first != -1)
    {
        for(int i = n - 1; i > 0; --i)
        {
            if(wins[i].first == -1)
                continue;

            if(wins[0].first >= 2)
            {
                M[wins[0].second][wins[i].second] = 2;
                wins[0].first -= 2;
            }
            else if(wins[0].first == 1)
            {
                M[wins[0].second][wins[i].second] = 1;
                M[wins[i].second][wins[0].second] = 1;

                --wins[i].first;
                --wins[0].first;
            }
            else
            {
                M[wins[i].second][wins[0].second] = 2;
                wins[i].first -= 2;
            }
        }

        wins[0].first = -1;
        sort(ALL(wins), greater<pair <int,int> >());
    }

    FOR(i, 0, n)
    {
        FOR(j, 0, n)
        {
            if(i == j)
                cout << "X";
            else if(M[i][j] == 2)
                cout << "W";
            else if(M[i][j] == 1)
                cout << "D";
            else
                cout << "L";
        }
        cout << "\n";
    }

    return 0;
}


#pragma GCC optimize("O3")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define PB push_back
#define MP make_pair
#define INF 1e9+7
#define LLINF 4e18+7
#define ALL(a) a.begin(), a.end()
#define SIZE(a) (int)a.size()

typedef unsigned long long ULL;
typedef long long LL;
typedef double LD;

using namespace std;

//#define DEBUG

int M[3001][3001];

int main()
{
    #ifndef DEBUG
    BOOST;
    #endif

    int n, m;
    cin >> n >> m;
    int total = n * (n - 1);

    vector <pair <int, int> > wins(n);

    FOR(i, 0, m)
    {
        cin >> wins[i].first;
        wins[i].second = i;

        total -= wins[i].first;
    }

    if(total < (n - m) * (n - m - 1) || total > (n - m) * wins[m - 1].first)
    {
        cout << "no\n";
        return 0;
    }

    FOR(i, m, n)
    {
        wins[i].first = (total + n - 1 - i) / (n - i);
        total -= wins[i].first;
        wins[i].second = i;
    }

    cout << "yes\n";

    while(wins[0].first != -1)
    {
        for(int i = n - 1; i > 0; --i)
        {
            if(wins[i].first == -1)
                continue;

            if(wins[0].first >= 2)
            {
                M[wins[0].second][wins[i].second] = 2;
                wins[0].first -= 2;
            }
            else if(wins[0].first == 1)
            {
                M[wins[0].second][wins[i].second] = 1;
                M[wins[i].second][wins[0].second] = 1;

                --wins[i].first;
                --wins[0].first;
            }
            else
            {
                M[wins[i].second][wins[0].second] = 2;
                wins[i].first -= 2;
            }
        }

        wins[0].first = -1;
        sort(ALL(wins), greater<pair <int,int> >());
    }

    FOR(i, 0, n)
    {
        FOR(j, 0, n)
        {
            if(i == j)
                cout << "X";
            else if(M[i][j] == 2)
                cout << "W";
            else if(M[i][j] == 1)
                cout << "D";
            else
                cout << "L";
        }
        cout << "\n";
    }

    return 0;
}


#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 3030;
struct pr {
	int id, score;
	inline pr(int _id = 0, int _score = 0) : id(_id), score(_score) {}
	inline int operator < (const pr & b) const { return score < b.score; }
};
pr a[maxn];
char w[maxn][maxn];
int n, m;
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> m >> n;
	int score = m * (m - 1);
	rep(i, 1, n) {
		cin >> a[i].score;
		a[i].id = i;
		score -= a[i].score;
	}
	rep(i, 1, m) w[i][i] = 'X';
	if(n != m) {
		const int average = score / (m - n), remain = score % (m - n);
		rep(i, n + 1, m) {
			a[i] = pr(i, average + (i <= n + remain));
		}
	}
	n = m;
	std::reverse(a + 1, a + n + 1);
	if(!std::is_sorted(a + 1, a + n + 1)) {
		cout << "no" << '\n';
		return 0;
	}
	for(int i = n;i >= 1;--i) {
		std::sort(a + 1, a + i + 1);
		for(int j = 1;j < i;++j) {
			if(a[i].score > 1) {
				a[i].score -= 2;
				w[a[i].id][a[j].id] = 'W';
				w[a[j].id][a[i].id] = 'L';
			} else {
				if(a[i].score > 0) {
					a[i].score -= 1;
					a[j].score -= 1;
					w[a[i].id][a[j].id] = 'D';
					w[a[j].id][a[i].id] = 'D';
				} else {
					a[j].score -= 2;
					w[a[i].id][a[j].id] = 'L';
					w[a[j].id][a[i].id] = 'W';
				}
			}
		}
		if(a[i].score != 0) {
			cout << "no" << '\n';
			return 0;
		}
	}
	cout << "yes" << '\n';
	rep(i, 1, n) {
		rep(j, 1, n) {
			cout.put(w[i][j]);
		}
		cout.put(10);

	}
}


#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 3030;
struct pr {
	int id, score;
	inline pr(int _id = 0, int _score = 0) : id(_id), score(_score) {}
	inline int operator < (const pr & b) const { return score < b.score; }
};
pr a[maxn];
char w[maxn][maxn];
int n, m;
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> m >> n;
	int score = m * (m - 1);
	rep(i, 1, n) {
		cin >> a[i].score;
		a[i].id = i;
		score -= a[i].score;
	}
	rep(i, 1, m) w[i][i] = 'X';
	if(n != m) {
		const int average = score / (m - n), remain = score % (m - n);
		rep(i, n + 1, m) {
			a[i] = pr(i, average + (i <= n + remain));
		}
	}
	std::reverse(a + 1, a + n + 1);
	if(!std::is_sorted(a + 1, a + n + 1)) {
		cout << "no" << '\n';
		return 0;
	}
	for(int i = n;i >= 1;--i) {
		std::sort(a + 1, a + i + 1);
		for(int j = 1;j < i;++j) {
			if(a[i].score > 1) {
				a[i].score -= 2;
				w[a[i].id][a[j].id] = 'W';
				w[a[j].id][a[i].id] = 'L';
			} else {
				if(a[i].score > 0) {
					a[i].score -= 1;
					a[j].score -= 1;
					w[a[i].id][a[j].id] = 'D';
					w[a[j].id][a[i].id] = 'D';
				} else {
					a[j].score -= 2;
					w[a[i].id][a[j].id] = 'L';
					w[a[j].id][a[i].id] = 'W';
				}
			}
		}
		if(a[i].score != 0) {
			cout << "no" << '\n';
			return 0;
		}
	}
	cout << "yes" << '\n';
	rep(i, 1, n) {
		rep(j, 1, n) {
			cout.put(w[i][j]);
		}
		cout.put(10);

	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned int uint;
#define MAX 3005
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"yes" : "no");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et() {
    puts("-1"); exit(0);
}

int  a[MAX],b[MAX];
pii c[MAX];
char s[MAX][MAX];
bool cmp(int x, int y) {
    return x>y;
}
bool pcmp(pii a, pii b) {
    return a.first>b.first;
}
void pp() {
    REQ(i,m) {
        puts(s[i]+1);
    }
}
void fmain() {
    scanf("%d%d",&m,&n);
    REQ(i,n) scanf("%d", a+i);
    sort(a+1,a+1+n, cmp);
    int ss = m*(m-1);
    for(int i=1,s=0,t=0,tt=m+m-2;i<=m;i++,tt-=2) {
        if (i<=n) {
            s += a[i]; t+=tt;
            if (s>t)  etp();
        } else {
            t += tt;
            int z=min(t-s, ss-s); z=min(a[i-1], z);
            a[i]=z; s+=z;
        }
    }

    REQ(i,m) {
        c[i].first=a[i]; c[i].second=i;
    }
    for(int i=1;i<m;i++) {
        sort(c+i,c+1+m,pcmp);
        int pt = c[i].first;
        int j = m;
        while(pt>0) {
            if (pt>1) {
                pt-=2;
                s[c[i].second][c[j].second]='W';
                s[c[j].second][c[i].second]='L';
            } else {
                assert(pt==1); pt--; c[j].first--;
                s[c[i].second][c[j].second]='D';
                s[c[j].second][c[i].second]='D';
            }
            j--;
        }
        for(int k=i+1;k<=j;k++) {
            s[c[i].second][c[k].second]='L';
            s[c[k].second][c[i].second]='W';
            c[k].first-=2;
        }

    }

    REQ(i,m) for(int j=i+1;j<=m;j++) {
        if (s[i][j]=='D') {
            b[i]++; b[j]++;
        } else if (s[i][j]=='W') {
            b[i]+=2;
        } else if (s[i][j]=='L') {
            b[j]+=2;
        }
    }
    sort(b+1,b+1+m,cmp);
    REQ(i,n) if (a[i]!=b[i]) etp();
    REQ(i,m) s[i][i]='X';
    puts("yes");
    REQ(i,m) {
        puts(s[i]+1);
    }
}
int main() {
    int t=1; //scanf("%d", &t);
    REQ(i,t) {
        fmain();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int MAX_N = 100000 + 5;

int n, k, key[MAX_N], mark;
int vis[MAX_N];
vector <int> g[MAX_N], v;
set <int> ans;
stack <int> stk;
bool ins[MAX_N];

void dfs(int x) {
    if (ans.find(x) != ans.end()) return;
    stk.push(x); ins[x] = 1;
    vis[x] = mark;
    ans.insert(x);
    for (int i = 0; i < g[x].size(); ++i) {
        int e = g[x][i];
        if (ins[e]) {
            puts("-1");
            exit(0);
        }
        if (ans.find(e) == ans.end()) dfs(e);
    }
    v.push_back(x);
    stk.push(x); ins[x] = 0;
}

int main() {
    scanf("%d%d", &n, &k);

    for (int i = 1; i <= k; ++i) scanf("%d", key + i);

    for (int i = 1; i <= n; ++i) {
        int t, u;
        scanf("%d", &t);
        for (int j = 1; j <= t; ++j) scanf("%d", &u), g[i].push_back(u);

    }

    for (int i = 1; i <= k; ++i) {
        ++mark;
        dfs(key[i]);
    }

    printf("%d\n", (int)v.size());
    int num = 0;
    for (auto const &x: v) {
        printf("%d%c", x, (++num) == v.size() ? '\n' : ' ');
    }

    return 0;
}


//#include "pch.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 10;
int head[maxn], Next[maxn], vis[maxn], to[maxn], a[maxn],ord[maxn];
int tot = 0,n,k,m,f=1,cnt=0,ans=0;
void add(int x, int y) {
	to[++tot] = y;
	Next[tot] = head[x];
	head[x] = tot;
}
void dfs(int now) {
	vis[now] = -1;
	for (int i = head[now]; i; i = Next[i]) {
		int y = to[i];
		if (vis[y] == -1) {
			f = 0; return;
		}
		else if (vis[y] == 1)
			continue;
		else {
			 dfs(y);
		}
	}
	vis[now] = 1;
	ord[++cnt] = now;
}
int main() {
	cin >> n >> k;
	for (int i = 1; i <= k; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> m;
		for (int j= 1; j <= m; j++) {
			int x;
			cin >> x;
			add(i, x);
		}
	}
	for (int i = 1; i <= k; i++) {
		if (!vis[a[i]]) {
			dfs(a[i]);
		}
	}
	if (f) {
		cout << cnt << endl;
		for (int i = 1; i <= cnt; i++)
			cout << ord[i] << " ";
	}
	else cout << -1 << endl;
}


#include <vector>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;

const int MAX = 1e5 + 10;
bool learned[MAX], vis[MAX],flag;
int cur, head[MAX], deepth[MAX];
vector<int> ans;

struct edge{
	int v, next;
}edges[MAX];
void add(int u, int v){
	edges[cur].v = v;
	edges[cur].next = head[u];
	head[u] = cur++;
}
void dfs(int s){
	if(learned[s]) return;
	ans.push_back(s);
	learned[s] = true;
	vis[s] = true;
	for(int i = head[s]; i != -1; i = edges[i].next){
		int tv = edges[i].v;
		if(vis[tv]){
			flag = true;
			break;
		}else if(!learned[tv] && tv){
			dfs(tv);
			deepth[s] = deepth[tv]+1;
		}
	}
}
bool cmp(int x, int y){
	return deepth[x] < deepth[y];
}
int main(){
	int n, k, t;
	while(cin >> n >> k){
		cur = 0;
		memset(head, -1, sizeof(head));
		memset(learned, 0, sizeof(learned));
		memset(deepth, 0, sizeof(deepth));
		vector<int>().swap(ans);
		vector<int> c;
		for(int i = 0; i < k; i++){
			int temp;
			cin >> temp;
			c.push_back(temp);
		}
		for(int i = 1; i <= n; i++){
			cin >> t;
			for(int j = 0; j < t; j++){
				int tv;
				cin >> tv;
				add(i, tv);
			}
		}
		for(int i = 0; i < c.size(); i++){
			memset(vis, false, sizeof(vis));
			flag = false;;
			dfs(c[i]);
			if(flag){
				cout << "-1\n";
				break;
			}
		}
		if(flag) continue;
		cout << ans.size() << endl;
		sort(ans.begin(), ans.end(), cmp);
		for(int i = 0; i < ans.size(); i++){
			if(i) cout << " ";
			cout << ans[i];
		}
		cout << endl;

	}

}


#include<iostream>
#include<cstdio>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f

const int maxn = 100005;

struct edge{
	int to,next;
};

edge E[maxn];
int head[maxn];
int vis[maxn];//-1,0,1
int n,m,k;
int a[maxn];
int order[maxn];
int num=0;

bool bfs(int u);
bool topo();

int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=k;i++)
		scanf("%d",&a[i]);
	int t1,t2;
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&t1);
		for(int j=1;j<=t1;j++)
		{
			scanf("%d",&t2);
			E[++cnt].to=t2;
			E[cnt].next=head[i];
			head[i]=cnt;
		}
	}
	if(topo())
	{
		printf("%d\n",num);
		printf("%d",order[1]);
		for(int i=2;i<=num;i++)
			printf(" %d",order[i]);
		printf("\n");
	}
	else
		printf("-1\n");
	return 0;
}

bool dfs(int u)
{
	vis[u]=-1;
	for(int i=head[u];i!=0;i=E[i].next)
	{
		int temp=E[i].to;
		if(vis[temp]==-1)
			return false;
		else if(!vis[temp]&&!dfs(temp))
			return false;
	}
	vis[u]=1;
	order[++num]=u;
	return true;
}

bool topo()
{
	for(int i=1;i<=k;i++)
		if(!vis[a[i]]&&!dfs(a[i]))
			return false;
	return true;
}


// PUSH YOUR LIMITS.!!

#include<bits/stdc++.h>
using namespace std;

typedef long double ld;
#define int long long

#define RAGE ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,n) for(i=0; i <n; i++)
#define repv(i,k,n) for(i=k; i<=n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define endl '\n'
#define fill(x , a) x.assign(a)
#define deb(x) cout<<#x<<" = "<<x<<endl
#define gcd __gcd
#define inf LLONG_MAX
const int mod = (int)(1e9+7);
int power(int x,int n)
{   if(n==0)    return 1;
    if(n==1)    return x%mod;
    if(n%2==0)  {   int y = power(x,n/2)%mod;return (y*y)%mod;}
    if(n&1)     {   int y = power(x,n-1);return (x%mod * y%mod)%mod;}
    return 0;
}
int dx[]    =  {-1, 0, 1,  0, -1, -1, 1,  1};
int dy[]    =  { 0, 1, 0, -1, -1,  1, 1, -1};
const int maxn = 100005;
//  ------------------------------------------------------------------

vector<int> vis(maxn);
vector<int> done(maxn);
vector<int> adj[maxn];
int flag=0;
vector<int> ar(maxn);
vector<int> ans;

void dfs(int n)
{
    if(vis[n])
    {   flag=1; return;}
    vis[n]=1;
    for(int x:adj[n])
    {
        if(!done[x])
        {
            dfs(x);
            done[x] = 1;
        }
    }
    done[n] =1;
    ans.pb(n);
}
void solve()
{
    int n,i,j,k,m;
    cin>>n>>k;
    rep(i,k)
        cin>>ar[i];

    repv(i,1,n)
    {   int temp;
        cin>>temp;
        while(temp--)
        {   int x;
            cin>>x;
            adj[i].pb(x);
        }
    }
    rep(i,k)
    {
        if(!done[ar[i]])
            dfs(ar[i]);
    }
    if(flag)
        cout<<-1<<endl;
    else
    {
        cout<<sz(ans)<<endl;
        for(int x:ans)
            cout<<x<<" ";
    }
}
signed main()
{
    RAGE;
        #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t=1;
    //  cin>>t;
    while(t--)
        solve();
    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif

    return 0;
}


// PUSH YOUR LIMITS.!!

#include<bits/stdc++.h>
using namespace std;

typedef long double ld;
#define int long long

#define RAGE ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,n) for(i=0; i <n; i++)
#define repv(i,k,n) for(i=k; i<=n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define endl '\n'
#define fill(x , a) x.assign(a)
#define deb(x) cout<<#x<<" = "<<x<<endl
#define gcd __gcd
#define inf LLONG_MAX
const int mod = (int)(1e9+7);
int power(int x,int n)
{   if(n==0)    return 1;
    if(n==1)    return x%mod;
    if(n%2==0)  {   int y = power(x,n/2)%mod;return (y*y)%mod;}
    if(n&1)     {   int y = power(x,n-1);return (x%mod * y%mod)%mod;}
    return 0;
}
int dx[]    =  {-1, 0, 1,  0, -1, -1, 1,  1};
int dy[]    =  { 0, 1, 0, -1, -1,  1, 1, -1};
const int maxn = 100005;
//  ------------------------------------------------------------------

vector<int> vis(maxn);
vector<int> done(maxn);
vector<int> adj[maxn];
int flag=0;
vector<int> ar(maxn);
vector<int> ans;

void dfs(int n)
{
    if(vis[n])
    {   flag=1; return;}
    vis[n]=1;
    for(int x:adj[n])
    {
        if(!done[x])
        {
            dfs(x);
        }
    }
    done[n] =1;
    ans.pb(n);
}
void solve()
{
    int n,i,j,k,m;
    cin>>n>>k;
    rep(i,k)
        cin>>ar[i];

    repv(i,1,n)
    {   int temp;
        cin>>temp;
        while(temp--)
        {   int x;
            cin>>x;
            adj[i].pb(x);
        }
    }
    rep(i,k)
    {
        if(!done[ar[i]])
            dfs(ar[i]);
    }
    if(flag)
        cout<<-1<<endl;
    else
    {
        cout<<sz(ans)<<endl;
        for(int x:ans)
            cout<<x<<" ";
    }
}
signed main()
{
    RAGE;
        #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t=1;
    //  cin>>t;
    while(t--)
        solve();
    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<long long> adj[1000005];
long long a[1000005];
long long visited[1000005]={0};
vector<long long> course;
void dfs(long long x)
{
    visited[x]=1;
    for(int i=0;i<adj[x].size();i++)
    {
        if(visited[adj[x][i]]==1)
		{
			cout<<-1;
			exit(0);
		}
        if(visited[adj[x][i]]==0)
        {
            dfs(adj[x][i]);
        }
    }
    visited[x]=2;
    course.push_back(x);
}
int main()
{
    long long n,k,p=0,q=1,l=0,m,r,ans=INT_MIN;
	long long x,y,ya,f,num,den,sum=0;
	cin>>n>>k;
    for(int j=0;j<k;j++)
        {
            cin>>a[j];
            a[j]--;
        }
    for(int i=0;i<n;i++)
    {
        cin>>m;
        for(int j=0;j<m;j++)
        {
        cin>>x;
        adj[i].push_back(x-1);
        }
    }
    for(long long i=0;i<k;i++)
    {
        if(visited[a[i]]==0)
        {
            dfs(a[i]);
        }
    }
    cout<<course.size()<<"\n";
    for(int i=0;i<course.size();i++)
    {
        cout<<course[i]+1<<' ';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<long long> adj[1000005];
long long a[1000005];
long long visited[1000005]={0};
vector<long long> course;
void dfs(long long x)
{
    visited[x]=1;
    for(int i=0;i<adj[x].size();i++)
    {
        if(visited[adj[x][i]]==1)
		{
			cout<<-1;
			exit(0);
		}
        if(visited[adj[x][i]]==0)
        {
            course.push_back(adj[x][i]);
            dfs(adj[x][i]);
        }
    }
}
int main()
{
    long long n,k,p=0,q=1,l=0,m,r,ans=INT_MIN;
	long long x,y,ya,f,num,den,sum=0;
	cin>>n>>k;
    for(int j=0;j<k;j++)
        {
            cin>>a[j];
            a[j]--;
        }
    for(int i=0;i<n;i++)
    {
        cin>>m;
        for(int j=0;j<m;j++)
        {
        cin>>x;
        adj[i].push_back(x-1);
        }
    }
    for(long long i=0;i<k;i++)
    {
        if(visited[a[i]]==0)
        {
            course.push_back(a[i]);
            dfs(a[i]);
        }
    }
    reverse(course.begin(),course.end());
    cout<<course.size()<<"\n";
    for(int i=0;i<course.size();i++)
    {
        cout<<course[i]+1<<' ';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<long long> adj[1000005];
long long a[1000005];
long long visited[1000005]={0};
vector<long long> course;
void dfs(long long x)
{
    visited[x]=1;
    for(int i=0;i<adj[x].size();i++)
    {
        if(visited[adj[x][i]]==1)
		{
			cout<<-1;
			exit(0);
		}
        if(visited[adj[x][i]]==0)
        {
            course.push_back(adj[x][i]);
            dfs(adj[x][i]);
        }
    }
    visited[x]=2;
}
int main()
{
    long long n,k,p=0,q=1,l=0,m,r,ans=INT_MIN;
	long long x,y,ya,f,num,den,sum=0;
	cin>>n>>k;
    for(int j=0;j<k;j++)
        {
            cin>>a[j];
            a[j]--;
        }
    for(int i=0;i<n;i++)
    {
        cin>>m;
        for(int j=0;j<m;j++)
        {
        cin>>x;
        adj[i].push_back(x-1);
        }
    }
    for(long long i=0;i<k;i++)
    {
        if(visited[a[i]]==0)
        {
            course.push_back(a[i]);
            dfs(a[i]);
        }
    }
    reverse(course.begin(),course.end());
    cout<<course.size()<<"\n";
    for(int i=0;i<course.size();i++)
    {
        cout<<course[i]+1<<' ';
    }
    return 0;
}


/*{{{*/
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int d[SIZE];
VI e[SIZE];
void f(int x){
    for(int y:e[x]){
        if(!d[y]){
            d[y]=1;
            f(y);
        }
    }
}
int main(){
    int n,k;
    R(n,k);
    FOR(i,1,k){
        int x;
        R(x);
        d[x]=1;
    }
    FOR(i,1,n){
        int t;
        R(t);
        REP(j,t){
            int y;
            R(y);
            e[i].PB(y);
        }
    }
    FOR(i,1,n){
        if(d[i])f(i);
    }
    VI an;
    FOR(i,1,n){
        if(d[i])an.PB(i);
    }
    W(SZ(an));
    W(an);
    return 0;
}


/*{{{*/
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int d[SIZE],u[SIZE];
VI e[SIZE];
VI an;
void f(int x){
    if(u[x])return;
    u[x]=1;
    for(int y:e[x]){
        f(y);
    }
    an.PB(x);
}
int main(){
    int n,k;
    R(n,k);
    FOR(i,1,k){
        int x;
        R(x);
        d[x]=1;
    }
    FOR(i,1,n){
        int t;
        R(t);
        REP(j,t){
            int y;
            R(y);
            e[i].PB(y);
        }
    }
    FOR(i,1,n){
        if(d[i])f(i);
    }
    W(SZ(an));
    W(an);
    return 0;
}


/*{{{*/
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int d[SIZE],u[SIZE];
VI e[SIZE];
VI an;
bool in[SIZE];
void f(int x){
    if(in[x]){
        W(-1);
        exit(0);
    }
    if(u[x])return;
    in[x]=1;
    u[x]=1;
    for(int y:e[x]){
        f(y);
    }
    an.PB(x);
    in[x]=0;
}
int main(){
    int n,k;
    R(n,k);
    FOR(i,1,k){
        int x;
        R(x);
        d[x]=1;
    }
    FOR(i,1,n){
        int t;
        R(t);
        REP(j,t){
            int y;
            R(y);
            e[i].PB(y);
        }
    }
    FOR(i,1,n){
        if(d[i])f(i);
    }
    W(SZ(an));
    W(an);
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define fl(i,n) for(int i=0;i<n;i++)
#define v(i) vector<int>
#define um unordered_map<int,int>
#define pb push_back
#define pr pair<int,int>
#define pq priority_queue<int>
#define st stack<int>
#define mp make_pair
/* lcm(a,b) , _gcd(a,b) , swap(a,b) , sort(a.begin(),a.end()) , *max_element(a.begin(),a.end()) , *min_element(a.begin(),a.end()) */

/* _builtin_popcount(x): This function is used to count the number of one’s */

/*__builtin_clz(x): This function is used to count the leading zeros of the integer. */

/*__builtin_ctz(x): This function is used to count the trailing zeros of the given integer */

/* binary_search(a, a + 10, 10) -> true/false */

/* this creates a sorted set with func in log time *p.find_by_order(x)  , p.order_of_key(x)   if not present than its pos in sorted array*/

/* cout<<fixed<<setprecision(8)   ,  round(1.589) -> 2 , trunc(1.590) -> 1 , ceil(x) , floor(x) */

// int mult(int x,v(i) &arr,int s){
//     int c=0;
//     fl(i,s){
//         int p=arr[i]*x+c;
//         arr[i]=p%10;
//         c=p/10;
//     }
//     while(c){
//         arr[s]=c%10;
//         c=c/10;
//         s++;
//     }
//     return s;
// }

// string fact(int k){
//      v(i) arr(50000);
//     arr[0]=1;
//     int size=1;
//     for(int x=2;x<=k;x++){
//         size=mult(x,arr,size);
//     }
//     string s="";
//     int j=0;
//     for(int i=size-1;i>=0;i--){
//         char c= arr[i] ;
//       // cout<<arr[i];
//       s+=(c+'0');
//     }
//     return (s);
// }

// int binsearch(v(i) a,int x){
//     int l=0,r=a.size()-1;
//     while(l<=r){
//         int m=(l+r)/2;
//         if(a[m]==x) return m;
//         else if(a[m]>x) r=m-1;
//         else l=m+1;
//     }
//     return -1;
// }

void print(v(i) a){
fl(i,a.size())
cout<<a[i]<<" ";
cout<<"\n";
}

bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool cmp(int a,int b){ return a>b;}

void dfs(vector<v(i)> &adj,v(i) &used,int x,v(i) &ans){
    used[x]=1;
    for(auto i:adj[x]){
        if(used[i]==0) dfs(adj,used,i,ans);
    }
    ans.pb(x+1);
}
bool explore(vector<v(i)> &adj,int x,v(i) &vis, bool *res){
    if(vis[x]==0) vis[x]=1;
    res[x]=true;
    for(auto i:adj[x]){
        if(vis[i]==0 && explore(adj,i,vis,res)) return true;
        if(res[i]==true) return true;

    }
     res[x]=false;
        return false;
}

bool checkcy(vector<v(i)> &adj,v(i) &mainc){
    v(i) vis(adj.size(),0);
    bool *res=new bool[adj.size()];
    fl(i,adj.size()) res[i]=false;
    fl(i,mainc.size()){
        if(explore(adj,mainc[i]-1,vis,res)) return true;
    }
return false;
}

int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k; cin>>n>>k;
    v(i) mainc(k);
    fl(i,k) cin>>mainc[i];
    vector<v(i)> adj(n);
    fl(i,n){
        int t;
        cin>>t;
        fl(j,t){
            int x; cin>>x;
            adj[i].pb(x-1);
        }
    }
    v(i) ans,used(n,0);
    fl(i,k){
        if(used[mainc[i]-1]==0){
            dfs(adj,used,mainc[i]-1,ans);
        }
    }
    if(checkcy(adj,mainc)==true){
        cout<<"-1";
    }
    else{cout<<ans.size()<<"\n"; print(ans);}

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define fl(i,n) for(int i=0;i<n;i++)
#define v(i) vector<int>
#define um unordered_map<int,int>
#define pb push_back
#define pr pair<int,int>
#define pq priority_queue<int>
#define st stack<int>
#define mp make_pair
/* lcm(a,b) , _gcd(a,b) , swap(a,b) , sort(a.begin(),a.end()) , *max_element(a.begin(),a.end()) , *min_element(a.begin(),a.end()) */

/* _builtin_popcount(x): This function is used to count the number of one’s */

/*__builtin_clz(x): This function is used to count the leading zeros of the integer. */

/*__builtin_ctz(x): This function is used to count the trailing zeros of the given integer */

/* binary_search(a, a + 10, 10) -> true/false */

/* this creates a sorted set with func in log time *p.find_by_order(x)  , p.order_of_key(x)   if not present than its pos in sorted array*/

/* cout<<fixed<<setprecision(8)   ,  round(1.589) -> 2 , trunc(1.590) -> 1 , ceil(x) , floor(x) */

// int mult(int x,v(i) &arr,int s){
//     int c=0;
//     fl(i,s){
//         int p=arr[i]*x+c;
//         arr[i]=p%10;
//         c=p/10;
//     }
//     while(c){
//         arr[s]=c%10;
//         c=c/10;
//         s++;
//     }
//     return s;
// }

// string fact(int k){
//      v(i) arr(50000);
//     arr[0]=1;
//     int size=1;
//     for(int x=2;x<=k;x++){
//         size=mult(x,arr,size);
//     }
//     string s="";
//     int j=0;
//     for(int i=size-1;i>=0;i--){
//         char c= arr[i] ;
//       // cout<<arr[i];
//       s+=(c+'0');
//     }
//     return (s);
// }

// int binsearch(v(i) a,int x){
//     int l=0,r=a.size()-1;
//     while(l<=r){
//         int m=(l+r)/2;
//         if(a[m]==x) return m;
//         else if(a[m]>x) r=m-1;
//         else l=m+1;
//     }
//     return -1;
// }

void print(v(i) a){
fl(i,a.size())
cout<<a[i]<<" ";
cout<<"\n";
}

bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool cmp(int a,int b){ return a>b;}

void dfs(vector<v(i)> &adj,v(i) &used,int x,v(i) &ans){
    used[x]=1;
    for(auto i:adj[x]){
        if(used[i]==0) dfs(adj,used,i,ans);
    }
    ans.pb(x+1);
}
bool explore(vector<v(i)> &adj,int x,v(i) &vis, bool *res){
    if(vis[x]==0) vis[x]=1;
    res[x]=true;
    for(auto i:adj[x]){
        if(vis[i]==0 && explore(adj,i,vis,res)) return true;
        if(res[i]==true) return true;

    }
     res[x]=false;
        return false;
}

bool checkcy(vector<v(i)> &adj){
    v(i) vis(adj.size(),0);
    bool *res=new bool[adj.size()];
    fl(i,adj.size()) res[i]=false;
    fl(i,adj.size()){
        if(explore(adj,i,vis,res)) return true;
    }
return false;
}

int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k; cin>>n>>k;
    v(i) mainc(k);
    fl(i,k) cin>>mainc[i];
    vector<v(i)> adj(n);
    fl(i,n){
        int t;
        cin>>t;
        fl(j,t){
            int x; cin>>x;
            adj[i].pb(x-1);
        }
    }
    v(i) ans,used(n,0);
    fl(i,k){
        if(used[mainc[i]-1]==0){
            dfs(adj,used,mainc[i]-1,ans);
        }
    }
    if(checkcy(adj)==true){
        cout<<"-1";
    }
    else{cout<<ans.size()<<"\n"; print(ans);}

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define fl(i,n) for(int i=0;i<n;i++)
#define v(i) vector<int>
#define um unordered_map<int,int>
#define pb push_back
#define pr pair<int,int>
#define pq priority_queue<int>
#define st stack<int>
#define mp make_pair
/* lcm(a,b) , _gcd(a,b) , swap(a,b) , sort(a.begin(),a.end()) , *max_element(a.begin(),a.end()) , *min_element(a.begin(),a.end()) */

/* _builtin_popcount(x): This function is used to count the number of one’s */

/*__builtin_clz(x): This function is used to count the leading zeros of the integer. */

/*__builtin_ctz(x): This function is used to count the trailing zeros of the given integer */

/* binary_search(a, a + 10, 10) -> true/false */

/* this creates a sorted set with func in log time *p.find_by_order(x)  , p.order_of_key(x)   if not present than its pos in sorted array*/

/* cout<<fixed<<setprecision(8)   ,  round(1.589) -> 2 , trunc(1.590) -> 1 , ceil(x) , floor(x) */

// int mult(int x,v(i) &arr,int s){
//     int c=0;
//     fl(i,s){
//         int p=arr[i]*x+c;
//         arr[i]=p%10;
//         c=p/10;
//     }
//     while(c){
//         arr[s]=c%10;
//         c=c/10;
//         s++;
//     }
//     return s;
// }

// string fact(int k){
//      v(i) arr(50000);
//     arr[0]=1;
//     int size=1;
//     for(int x=2;x<=k;x++){
//         size=mult(x,arr,size);
//     }
//     string s="";
//     int j=0;
//     for(int i=size-1;i>=0;i--){
//         char c= arr[i] ;
//       // cout<<arr[i];
//       s+=(c+'0');
//     }
//     return (s);
// }

// int binsearch(v(i) a,int x){
//     int l=0,r=a.size()-1;
//     while(l<=r){
//         int m=(l+r)/2;
//         if(a[m]==x) return m;
//         else if(a[m]>x) r=m-1;
//         else l=m+1;
//     }
//     return -1;
// }

void print(v(i) a){
fl(i,a.size())
cout<<a[i]<<" ";
cout<<"\n";
}

bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool cmp(int a,int b){ return a>b;}

void dfs(vector<v(i)> &adj,v(i) &used,int x,v(i) &ans){
    used[x]=1;
    for(auto i:adj[x]){
        if(used[i]==0) dfs(adj,used,i,ans);
    }
    ans.pb(x+1);
}
bool explore(vector<v(i)> &adj,int x,v(i) &vis, bool *res){
    if(vis[x]==0) vis[x]=1;
    res[x]=true;
    for(auto i:adj[x]){
        if(vis[i]==0 && explore(adj,i,vis,res)) return false;
        if(res[i]==true) return false;

    }
     res[x]=false;
        return true;
}

bool checkcy(vector<v(i)> &adj){
    v(i) vis(adj.size(),0);
    bool *res=new bool[adj.size()];
    fl(i,adj.size()) res[i]=false;
    fl(i,adj.size()){
        if(explore(adj,i,vis,res)) return false;
    }
return true;
}

int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k; cin>>n>>k;
    v(i) mainc(k);
    fl(i,k) cin>>mainc[i];
    vector<v(i)> adj(n);
    fl(i,n){
        int t;
        cin>>t;
        fl(j,t){
            int x; cin>>x;
            adj[i].pb(x-1);
        }
    }
    v(i) ans,used(n,0);
    fl(i,k){
        if(used[mainc[i]-1]==0){
            dfs(adj,used,mainc[i]-1,ans);
        }
    }
    if(checkcy(adj)){
        cout<<"-1";
    }
    else{cout<<ans.size()<<"\n"; print(ans);}

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define fl(i,n) for(int i=0;i<n;i++)
#define v(i) vector<int>
#define um unordered_map<int,int>
#define pb push_back
#define pr pair<int,int>
#define pq priority_queue<int>
#define st stack<int>
#define mp make_pair
/* lcm(a,b) , _gcd(a,b) , swap(a,b) , sort(a.begin(),a.end()) , *max_element(a.begin(),a.end()) , *min_element(a.begin(),a.end()) */

/* _builtin_popcount(x): This function is used to count the number of one’s */

/*__builtin_clz(x): This function is used to count the leading zeros of the integer. */

/*__builtin_ctz(x): This function is used to count the trailing zeros of the given integer */

/* binary_search(a, a + 10, 10) -> true/false */

/* this creates a sorted set with func in log time *p.find_by_order(x)  , p.order_of_key(x)   if not present than its pos in sorted array*/

/* cout<<fixed<<setprecision(8)   ,  round(1.589) -> 2 , trunc(1.590) -> 1 , ceil(x) , floor(x) */

// int mult(int x,v(i) &arr,int s){
//     int c=0;
//     fl(i,s){
//         int p=arr[i]*x+c;
//         arr[i]=p%10;
//         c=p/10;
//     }
//     while(c){
//         arr[s]=c%10;
//         c=c/10;
//         s++;
//     }
//     return s;
// }

// string fact(int k){
//      v(i) arr(50000);
//     arr[0]=1;
//     int size=1;
//     for(int x=2;x<=k;x++){
//         size=mult(x,arr,size);
//     }
//     string s="";
//     int j=0;
//     for(int i=size-1;i>=0;i--){
//         char c= arr[i] ;
//       // cout<<arr[i];
//       s+=(c+'0');
//     }
//     return (s);
// }

// int binsearch(v(i) a,int x){
//     int l=0,r=a.size()-1;
//     while(l<=r){
//         int m=(l+r)/2;
//         if(a[m]==x) return m;
//         else if(a[m]>x) r=m-1;
//         else l=m+1;
//     }
//     return -1;
// }

void print(v(i) a){
fl(i,a.size())
cout<<a[i]<<" ";
cout<<"\n";
}

bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool cmp(int a,int b){ return a>b;}

void dfs(vector<v(i)> adj,v(i) &used,int x,v(i) &ans){
    used[x]=1;
    for(auto i:adj[x]){
        if(used[i]==0) dfs(adj,used,i,ans);
    }
    ans.pb(x+1);
}
bool explore(vector<v(i)> adj,int x,v(i) &vis, v(i) &res){
    if(vis[x]==0) vis[x]=1;
    res[x]=1;
    for(auto i:adj[x]){
        if(vis[i]==0 && explore(adj,i,vis,res)) return false;
        if(res[i]==1) return false;

    }
     res[x]=0;
        return true;
}

bool checkcy(vector<v(i)> adj){
    v(i) vis(adj.size(),0);
    v(i) res(adj.size(),0);
    fl(i,adj.size()){
        if(explore(adj,i,vis,res)) return false;
    }
return true;
}

int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k; cin>>n>>k;
    v(i) mainc(k);
    fl(i,k) cin>>mainc[i];
    vector<v(i)> adj(n);
    fl(i,n){
        int t;
        cin>>t;
        fl(j,t){
            int x; cin>>x;
            adj[i].pb(x-1);
        }
    }
    v(i) ans,used(n,0);
    fl(i,k){
        if(used[mainc[i]-1]==0){
            dfs(adj,used,mainc[i]-1,ans);
        }
    }
    fill(used.begin(),used.end(),0);

    if(checkcy(adj)){
        cout<<"-1";
    }
    else{cout<<ans.size()<<"\n"; print(ans);}

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define fl(i,n) for(int i=0;i<n;i++)
#define v(i) vector<int>
#define um unordered_map<int,int>
#define pb push_back
#define pr pair<int,int>
#define pq priority_queue<int>
#define st stack<int>
#define mp make_pair
/* lcm(a,b) , _gcd(a,b) , swap(a,b) , sort(a.begin(),a.end()) , *max_element(a.begin(),a.end()) , *min_element(a.begin(),a.end()) */

/* _builtin_popcount(x): This function is used to count the number of one’s */

/*__builtin_clz(x): This function is used to count the leading zeros of the integer. */

/*__builtin_ctz(x): This function is used to count the trailing zeros of the given integer */

/* binary_search(a, a + 10, 10) -> true/false */

/* this creates a sorted set with func in log time *p.find_by_order(x)  , p.order_of_key(x)   if not present than its pos in sorted array*/

/* cout<<fixed<<setprecision(8)   ,  round(1.589) -> 2 , trunc(1.590) -> 1 , ceil(x) , floor(x) */

// int mult(int x,v(i) &arr,int s){
//     int c=0;
//     fl(i,s){
//         int p=arr[i]*x+c;
//         arr[i]=p%10;
//         c=p/10;
//     }
//     while(c){
//         arr[s]=c%10;
//         c=c/10;
//         s++;
//     }
//     return s;
// }

// string fact(int k){
//      v(i) arr(50000);
//     arr[0]=1;
//     int size=1;
//     for(int x=2;x<=k;x++){
//         size=mult(x,arr,size);
//     }
//     string s="";
//     int j=0;
//     for(int i=size-1;i>=0;i--){
//         char c= arr[i] ;
//       // cout<<arr[i];
//       s+=(c+'0');
//     }
//     return (s);
// }

// int binsearch(v(i) a,int x){
//     int l=0,r=a.size()-1;
//     while(l<=r){
//         int m=(l+r)/2;
//         if(a[m]==x) return m;
//         else if(a[m]>x) r=m-1;
//         else l=m+1;
//     }
//     return -1;
// }

void print(v(i) a){
fl(i,a.size())
cout<<a[i]<<" ";
cout<<"\n";
}

bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool cmp(int a,int b){ return a>b;}

void dfs(vector<v(i)> adj,v(i) &used,int x,v(i) &ans){
    used[x]=1;
    for(auto i:adj[x]){
        if(used[i]==0) dfs(adj,used,i,ans);
    }
    ans.pb(x+1);
}
bool explore(vector<v(i)> adj,int x,v(i) &vis, bool *res){
    if(vis[x]==0) vis[x]=1;
    res[x]=true;
    for(auto i:adj[x]){
        if(vis[i]==0 && explore(adj,i,vis,res)) return false;
        if(res[i]==true) return false;

    }
     res[x]=false;
        return true;
}

bool checkcy(vector<v(i)> adj){
    v(i) vis(adj.size(),0);
    bool *res=new bool[adj.size()];
    fl(i,adj.size()) res[i]=false;
    fl(i,adj.size()){
        if(explore(adj,i,vis,res)) return false;
    }
return true;
}

int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k; cin>>n>>k;
    v(i) mainc(k);
    fl(i,k) cin>>mainc[i];
    vector<v(i)> adj(n);
    fl(i,n){
        int t;
        cin>>t;
        fl(j,t){
            int x; cin>>x;
            adj[i].pb(x-1);
        }
    }
    v(i) ans,used(n,0);
    fl(i,k){
        if(used[mainc[i]-1]==0){
            dfs(adj,used,mainc[i]-1,ans);
        }
    }
    if(checkcy(adj)){
        cout<<"-1";
    }
    else{cout<<ans.size()<<"\n"; print(ans);}

}


#include <bits/stdc++.h>
using namespace std;

#define MX 110000
vector<int> g[MX];
vector<int> res;
int vis[MX];
bool vis1[MX];
int sp[MX];
int cur;

bool dfs(int u) {
	vis[u] = cur;
	for(auto v : g[u]) {
		if(!vis[v] && !dfs(v)) return false;
		else if(vis[v] == cur && !vis1[v]) return false;
	}
	res.push_back(u);
	vis1[u] = true;
	return true;
}
int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for(int i = 0; i < k; i++) scanf("%d", sp + i);
	for(int i = 1; i <= n; i++) {
		int tc;
		scanf("%d", &tc);
		while(tc--) {
			int p; scanf("%d", &p);
			g[i].push_back(p);
		}
	}
	for(int i = 0; i < k; i++)
		if(!vis[sp[i]]) {
			++ cur;
			if(!dfs(sp[i])) {
				puts("-1");
				return 0;
			}
		}
	for(int i = 0; i < res.size(); i++) printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MX 110000
vector<int> g[MX];
vector<int> res;
int vis[MX];
bool vis1[MX];
int sp[MX];
int cur;

bool dfs(int u) {
	vis[u] = cur;
	for(auto v : g[u]) {
		if(!vis[v] && !dfs(v)) return false;
		else if(vis[v] == cur && !vis1[v]) return false;
	}
	res.push_back(u);
	vis1[u] = true;
	return true;
}
int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for(int i = 0; i < k; i++) scanf("%d", sp + i);
	for(int i = 1; i <= n; i++) {
		int tc;
		scanf("%d", &tc);
		while(tc--) {
			int p; scanf("%d", &p);
			g[i].push_back(p);
		}
	}
	for(int i = 0; i < k; i++)
		if(!vis[sp[i]]) {
			++ cur;
			if(!dfs(sp[i])) {
				puts("-1");
				return 0;
			}
		}
	printf("%d\n", res.size());
	for(int i = 0; i < res.size(); i++) printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 110000;

int a[N], que[N], vis[N], deg[N];
vector<int> con[N], rcon[N];

int main() {

	ios_base::sync_with_stdio(0);

	int n, k; cin >> n >> k;
	for (int i = 1; i <= k; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		int m; cin >> m;
		while (m--) {
			int u; cin >> u;
			con[u].push_back(i), rcon[i].push_back(u);
		}
	}

	int hd = 0, tl = 0;
	for (int i = 1; i <= k; i++) vis[a[i]] = 1, que[tl++] = a[i];
	while (hd < tl) {
		int cur = que[hd++];
		for (int v : rcon[cur]) if (!vis[v]) {
			vis[v] = 1;
			que[tl++] = v;
		}
	}

	int cnt = 0;
	for (int i = 1; i <= n; i++) cnt += vis[i];

	for (int i = 1; i <= n; i++) if (vis[i]) for (int v : con[i]) if (vis[v]) deg[v]++;
	hd = tl = 0;
	for (int i = 1; i <= n; i++) if (vis[i] && !deg[i]) que[tl++] = i;
	while (hd < tl) {
		int cur = que[hd++];
		for (int v : con[cur]) if (vis[v]) {
			deg[v]--;
			if (!deg[v]) que[tl++] = v;
		}
	}
	if (tl < cnt) {
		cout << -1 << endl;
		return 0;
	}
	cout << tl << endl;
	for (int i = 0; i < tl; i++) cout << que[i] << " ";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<long long int>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define pl pair<ll,ll>
#define pb push_back
#define PI 3.14159265
#define mod 998244353
#define pb push_back
#define mp make_pair
#define fri(s,n) for(int i=s;i<n;i++)
#define frj(s,n) for(int j=s;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define vsi vector<set<int> >
#define pii pair<int,int>
#define inf 1e18
#define vpii vector<pair<int,int> >
vvi graph;
stack<int> ans;
bool cycle=false;
void dfs(int node,vb &visited,int parent){
    visited[node]=true;
    ans.push(node);
    for(auto nbr:graph[node]){
        if(!visited[nbr])
            dfs(nbr,visited,node);
        else if(parent!=nbr)
            cycle=true;
    }
    }

int main(){
    int n,k;
    cin>>n>>k;
    vi special(k);
    fri(0,k)
        cin>>special[i];
    graph.resize(n+1);
    fri(1,n+1){
        int dependencies;
        cin>>dependencies;
        while(dependencies--){
            int x;
            cin>>x;
            graph[i].pb(x);
        }
        }
    vb visited(n+1);
    for(auto node:special){
        if(!visited[node])
            dfs(node,visited,node);
    }
    if(cycle)
        cout<<-1;
    else{
        cout<<ans.size()<<"\n";
        while(!ans.empty()){
            cout<<ans.top()<<" ";
            ans.pop();
        }
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<long long int>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define pl pair<ll,ll>
#define pb push_back
#define PI 3.14159265
#define mod 998244353
#define pb push_back
#define mp make_pair
#define fri(s,n) for(int i=s;i<n;i++)
#define frj(s,n) for(int j=s;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define vsi vector<set<int> >
#define pii pair<int,int>
#define inf 1e18
#define vpii vector<pair<int,int> >
vvi graph;
stack<int> ans;
bool cycle=false;
vb current;
void dfs(int node,vb &visited){
    current[node]=true;
    visited[node]=true;
    ans.push(node);
    for(auto nbr:graph[node]){
        if(!visited[nbr])
            dfs(nbr,visited);
        else if(current[nbr])
            cycle=true;
    }
    current[node]=false;
    return;
    }

int main(){
    int n,k;
    cin>>n>>k;
    vi special(k);
    fri(0,k)
        cin>>special[i];
    graph.resize(n+1);
    fri(1,n+1){
        int dependencies;
        cin>>dependencies;
        while(dependencies--){
            int x;
            cin>>x;
            graph[i].pb(x);
        }
        }
    vb visited(n+1);
    current.resize(n+1);
    for(auto node:special){
        if(!visited[node])
            dfs(node,visited);
    }
    if(cycle)
        cout<<-1;
    else{
        cout<<ans.size()<<"\n";
        while(!ans.empty()){
            cout<<ans.top()<<" ";
            ans.pop();
        }
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<long long int>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define pl pair<ll,ll>
#define pb push_back
#define PI 3.14159265
#define mod 998244353
#define pb push_back
#define mp make_pair
#define fri(s,n) for(int i=s;i<n;i++)
#define frj(s,n) for(int j=s;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define vsi vector<set<int> >
#define pii pair<int,int>
#define inf 1e18
#define vpii vector<pair<int,int> >
vvi graph;
vi ans;
bool cycle=false;
vb current;
void dfs(int node,vb &visited){
    current[node]=true;
    visited[node]=true;

    for(auto nbr:graph[node]){
        if(!visited[nbr])
            dfs(nbr,visited);
        else if(current[nbr])
            cycle=true;
    }
    current[node]=false;
    ans.pb(node);
    return;
    }

int main(){
    int n,k;
    cin>>n>>k;
    vi special(k);
    fri(0,k)
        cin>>special[i];
    graph.resize(n+1);
    fri(1,n+1){
        int dependencies;
        cin>>dependencies;
        while(dependencies--){
            int x;
            cin>>x;
            graph[i].pb(x);
        }
        }
    vb visited(n+1);
    current.resize(n+1);
    for(auto node:special){
        if(!visited[node])
            dfs(node,visited);
    }
    if(cycle)
        cout<<-1;
    else{
        cout<<ans.size()<<"\n";
        for(auto x:ans){
            cout<<x<<" ";
        }
    }

}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn<<1],to[maxn<<1],be[maxn],e;
int deg[maxn];
bool sign[maxn];
int vis[maxn];
vector<int>G[maxn];
queue<int>q;

void add(int x,int y){
	to[++e]=y,ne[e]=be[x],be[x]=e;
}

void dfs(int node){
	if(vis[node]==-1){
		flag=1;
		return ;
	}
	vis[node]=-1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		dfs(u);
		if(flag) return ;
		add(u,node),++deg[node];
	}
	vis[node]=1;
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		for(int i=be[u];i;i=ne[i]){
			int v=to[i];
			--deg[v];
			if(!deg[v]) q.push(v);
		} }
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn],to[maxn],be[maxn],e;
int deg[maxn];
bool sign[maxn];
int vis[maxn];
vector<int>G[maxn];
queue<int>q;

void add(int x,int y){
	to[++e]=y,ne[e]=be[x],be[x]=e;
}

void dfs(int node){
	if(vis[node]==-1){
		flag=1;
		return ;
	}
	vis[node]=-1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		dfs(u);
		add(u,node),++deg[node];
	}
	vis[node]=1;
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		for(int i=be[u];i;i=ne[i]){
			int v=to[i];
			--deg[v];
			if(!deg[v]) q.push(v);
		} }
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn],to[maxn],be[maxn],e;
int deg[maxn];
bool sign[maxn],vis[maxn];
vector<int>G[maxn];
queue<int>q;

void add(int x,int y){
	to[++e]=y,ne[e]=be[x],be[x]=e;
}

void dfs(int node){
	if(vis[node]){
		flag=1;
		return ;
	}
	vis[node]=1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		dfs(u);
		add(u,node),++deg[node];
	}
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		for(int i=be[u];i;i=ne[i]){
			int v=to[i];
			--deg[v];
			if(!deg[v]) q.push(v);
		}
	}
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn<<1],to[maxn<<1],be[maxn],e;
int deg[maxn];
bool sign[maxn];
int vis[maxn];
vector<int>G[maxn];
queue<int>q;

void add(int x,int y){
	to[++e]=y,ne[e]=be[x],be[x]=e;
}

void dfs(int node){
	if(flag) return ;
	if(vis[node]==-1){
		flag=1;
		return ;
	}
	vis[node]=-1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		if(!vis[u]) dfs(u);
		if(flag) return ;
		add(u,node),++deg[node];
	}
	vis[node]=1;
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		for(int i=be[u];i;i=ne[i]){
			int v=to[i];
			--deg[v];
			if(!deg[v]) q.push(v);
		} }
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn<<1],to[maxn<<1],be[maxn],e;
int deg[maxn];
bool sign[maxn];
int vis[maxn];
vector<int>G[maxn];
queue<int>q;

void add(int x,int y){
	to[++e]=y,ne[e]=be[x],be[x]=e;
}

void dfs(int node){
	if(vis[node]==-1){
		flag=1;
		return ;
	}
	vis[node]=-1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		dfs(u);
		add(u,node),++deg[node];
	}
	vis[node]=1;
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		for(int i=be[u];i;i=ne[i]){
			int v=to[i];
			--deg[v];
			if(!deg[v]) q.push(v);
		} }
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn<<1],to[maxn<<1],be[maxn],e;
int deg[maxn];
bool sign[maxn];
int vis[maxn];
vector<int>G[maxn];
queue<int>q;

void add(int x,int y){
	to[++e]=y,ne[e]=be[x],be[x]=e;
}

void dfs(int node){
	if(flag) return ;
	if(vis[node]==-1){
		flag=1;
		return ;
	}
	vis[node]=-1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		if(vis[u]==1) continue;
		dfs(u);
		if(flag) return ;
		add(u,node),++deg[node];
	}
	vis[node]=1;
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		for(int i=be[u];i;i=ne[i]){
			int v=to[i];
			--deg[v];
			if(!deg[v]) q.push(v);
		} }
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}


#include<bits/stdc++.h>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
#define Forr(aa,bb,cc) for(int aa=(bb);aa>=(int)(cc);--aa)
using namespace std;
const int maxn=1e5+10;
int n,m,flag;
int ne[maxn<<1],to[maxn<<1],be[maxn],e;
int deg[maxn];
bool sign[maxn];
int vis[maxn];
vector<int>G[maxn],V[maxn];
queue<int>q;

void add(int x,int y){
	V[x].push_back(y);
}

void dfs(int node){
	if(vis[node]==-1){
		flag=1;
		return ;
	}
	vis[node]=-1;
	For(i,0,G[node].size()-1){
		int u=G[node][i];
		dfs(u);
		add(u,node),++deg[node];
	}
	vis[node]=1;
}

void topo(){
	int ans[maxn],tot=0;
	For(i,1,n) if(vis[i] && !deg[i]) q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		ans[++tot]=u;
		For(i,0,V[u].size()-1){
			int v=V[u][i];
			--deg[v];
			if(!deg[v]) q.push(v);
		} }
	printf("%d\n",tot);
	For(i,1,tot) printf("%d ",ans[i]);puts("");
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int x,y;
	scanf("%d%d",&n,&m);
	For(i,1,m) scanf("%d",&x),sign[x]=1;
	For(i,1,n){
		scanf("%d",&x);
		For(j,1,x) scanf("%d",&y),G[i].push_back(y);
	}
	//For(i,1,n) For(j,0,G[i].size()-1) cout<<i<<" "<<G[i][j]<<endl;
	For(i,1,n) if(sign[i] && !vis[i]) dfs(i);
	if(flag){
		puts("-1");
		return 0;
	}
	topo();
	return 0;
}