This page is still under construction.

Parts of this page are still being built. What you see may change.

Clique Festival

Time limit2sMemory limit512 MB

Summary
Given up to 18 weighted cliques added to a graph on n vertices, compute the sum of shortest-path distances over all vertex pairs.
Level

Hard9 of 10

Topics
Graph, Shortest path, Bit manipulation, Dynamic programming
Solved
No attempts yet

Problem

John has a graph with nn vertices labeled with integers 1,…,n1, \ldots, n. Initially, there are no edges in the graph. Then John modifies the graph kk times, each time adding a clique to the graph. He chooses an integer aa and a set SS which is a non-empty subset of the set of integers 1,2,…,n1, 2, \ldots, n. For each unordered pair (i,j)(i, j) such that i,j∈Si, j \in S and i≠ji \neq j, John adds an undirected edge between the vertices ii and jj with weight aa. It is possible that parallel edges appear in John's graph.

The distance between vertices uu and vv is defined as follows. Denote du,vd_{u,v} as the minimum weight of the edge between vertices uu and vv, or ∞\infty if there is no such edge. Then dist(u,v)=min⁡i1,…,ip(du,i1+di1,i2+…+dip−1,ip+dip,v)\mathrm{dist}(u,v) = \min\limits_{i_1, \ldots, i_p} \left(d_{u,i_1} + d_{i_1,i_2} + \ldots + d_{i_{p-1},i_p} + d_{i_p,v}\right). In other words, the distance is the length of the shortest path between uu and vv.

Your task is to calculate ∑i=1n−1∑j=i+1ndist(i,j)\sum\limits_{i=1}^{n-1} \sum\limits_{j = i+1}^n dist(i,j). It is guaranteed that all summands are finite.

Input

The first line contains two integers nn and kk (1≤n≤100 0001 \le n \le 100\,000, 1≤k≤181 \le k \le 18). The next kk lines contain the descriptions of the added cliques. Each of these lines contains integers aa (1≤a≤1071 \le a \le 10^7), ∣S∣|S| (1≤∣S∣≤n1 \le |S| \le n), and then ∣S∣|S| integers s1,…,s∣S∣s_1, \ldots, s_{|S|} (1≤si≤n1 \le s_i \le n, all sis_i are distinct). These are the weight of edges in the clique, the number of vertices in the clique, and the labels of these vertices, respectively.

The sum of all ∣S∣|S| in the input does not exceed 300 000300\,000.

Output

Output a single integer: the answer to the problem.

Examples2

  1. Example 1

    Input
    10 3
    10 5 1 2 3 4 5
    10 5 6 7 8 9 10
    1 2 5 6
    
    Expected output
    625
    
  2. Example 2

    Input
    3 2
    1 2 1 2
    1 2 2 3
    
    Expected output
    4