You are developing the file system for a new operating system. All disk space is divided into N equally sized clusters, numbered from 1 to N. Each file occupies one or more clusters located at arbitrary positions on the disk. Every cluster not occupied by a file is considered free.
A file can be read fastest when all of its clusters lie in consecutive clusters in their natural order. Because the disk rotates at a constant speed, clusters near the beginning are read faster than clusters near the end, so the files are numbered from 1 to K in order of decreasing access frequency. In the optimal layout, file 1 occupies clusters 1,2,…,S1, file 2 occupies clusters S1+1,…,S1+S2, and so on, where Si is the number of clusters that file i occupies.
To reach the optimal layout you perform cluster-moving operations. One cluster-moving operation reads the contents of one occupied cluster and writes them to a free cluster; afterwards the source cluster becomes free and the destination cluster becomes occupied.
Compute the minimum number of cluster-moving operations required to place all files in the optimal layout.
The first line contains two integers N and K separated by a space (1≤K<N≤10000). Each of the next K lines describes one file. The description of the i-th file starts with the integer Si, the number of clusters in file i (1≤Si<N), followed by Si integers giving the cluster numbers occupied by that file in natural order.
All cluster numbers in the input are distinct, and there is always at least one free cluster.
Print a single integer: the minimum number of cluster-moving operations needed to place all files in the optimal layout. If the files are already in the optimal layout, print 0.