Cactus edge move

Count the pairs of deleting one edge and inserting a different edge that keep the given graph a cactus.

Hard9GraphCombinatoricsNo attempts yetTime limit1sMemory limit256 MB

Problem

A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. It generalizes a tree by allowing some cycles. A cactus has no multiple edges between the same pair of vertices and no loop from a vertex to itself.

You are given a cactus. To move an edge, delete one edge of the graph and insert an edge between a different pair of vertices, so that the graph is still a cactus afterwards. Count the ways to move an edge.

Two ways are different when they delete different edges or insert different edges. The inserted edge cannot join the same pair of vertices that the deleted edge joined.

The picture shows two examples of a cactus.

Input

The first line contains the number of vertices nn and the number of paths mm (1n500001 \le n \le 50000, 0m500000 \le m \le 50000). Vertices are numbered from 11 to nn. The edges of the graph are given as mm paths that never reuse an edge.

Each of the next mm lines contains one path. The line starts with an integer kik_i (2ki10002 \le k_i \le 1000), followed by kik_i integers between 11 and nn. These numbers are the vertices the path visits, in order. Adjacent vertices in a path are distinct. A path may visit the same vertex several times, but every edge of the graph appears exactly once in the whole input.

The given graph is a cactus.

Output

Print one integer, the number of ways to move an edge.