import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class partitions_as implements Runnable {

    boolean[][] e;
    boolean[] u;

    void dfs(int x) {
        u[x] = true;
        for (int i = 0; i < e.length; i++) {
            if (e[x][i] && !u[i]) {
                dfs(i);
            }
        }
    }

    public void solve() throws IOException {
        Scanner in = new Scanner(new File("partitions.in"));
        PrintWriter out = new PrintWriter(new File("partitions.out"));

        int n = in.nextInt();
        int m = in.nextInt();
        int[] l = new int[m];
        int[][] q = new int[m][n];
        for (int i = 0; i < m; i++) {
            l[i] = in.nextInt();
            for (int j = 0; j < l[i]; j++) {
                q[i][j] = in.nextInt() - 1;
            }
        }

        BigInteger[][] a = new BigInteger[n + 1][n + 1];
        BigInteger[] b = new BigInteger[n + 1];
        b[0] = BigInteger.ONE;
        Arrays.fill(a[0], BigInteger.ZERO);
        a[0][0] = BigInteger.ONE;
        for (int i = 1; i <= n; i++) {
            a[i][0] = BigInteger.ZERO;
            b[i] = BigInteger.ZERO;
            for (int j = 1; j <= n; j++) {
                a[i][j] = a[i - 1][j].multiply(BigInteger.valueOf(j)).add(a[i - 1][j - 1]);
                b[i] = b[i].add(a[i][j]);
            }
        }

        e = new boolean[n][n];
        u = new boolean[n];
        BigInteger answer = BigInteger.ZERO;
        for (int i = 0; i < (1 << m); i++) {
            for (int j = 0; j < n; j++) {
                Arrays.fill(e[j], false);
            }

            int s = 1;
            for (int j = 0; j < m; j++) {
                if ((i & (1 << j)) != 0) {
                    s = -s;
                    for (int k = 1; k < l[j]; k++) {
                        e[q[j][0]][q[j][k]] = e[q[j][k]][q[j][0]] = true;
                    }
                }
            }

            int nc = 0;
            Arrays.fill(u, false);
            for (int j = 0; j < n; j++) {
                if (!u[j]) {
                    dfs(j);
                    nc++;
                }
            }

            if (s == 1) {
                answer = answer.add(b[nc]);
            } else {
                answer = answer.subtract(b[nc]);
            }
        }

        out.println(answer);

        in.close();
        out.close();
    }

    public void run() {
        try {
            solve();
        } catch (IOException e) {
            System.exit(64);
        }
    }

    public static void main(String[] arg) throws IOException {
        new Thread(new partitions_as()).start();
    }
}
