A few years ago, Arup started submitting problems to the UCF Local Contest regarding her daughter Anya’s CD requests. Unfortunately, his latest question wasn’t good enough to make the cut for the 2019 UCF Local Contest. Luckily, a couple coaches, nostalgic for solving Anya’s CD problems, suggested that Arup’s latest question about Anya’s CD requests be added to the practice local contest. Arup is so ecstatic that his question is being used in this contest that he will personally present the first solver with a copy of Ed Sheeran’s CD “Divide”.To this day, Arup drives Anya to school daily and Anya makes CD requests in the car. When Anya was four (in 2017), she would request a sequence of track numbers for Arup to play and Arup had to determine the fewest number of button presses to satisfy the requests. If you didn't compete in 2017, here is an explanation of how the CD player in Arup's car works: Arup can only change tracks by pressing a forward button or a backward button. If track number k has completed, when Arup presses the backward button, track number k will play again. Alternatively, if he presses the forward button when track k has completed, then track k+2 will play. The only exception to the latter is that if track k+2 doesn't exist. In this case, the tracks
just wrap back around to the beginning, starting with 1, then 2, etc. Similarly, if Arup presses the backward button twice right after track 1 completes, this will move the CD to track t, where t is the number of tracks on the CD. In the absence of a button press, after track k completes, track k+1 plays, except for when k = t, in which case, track 1 plays next. Now that Anya is six, her requests have become slightly more flexible. Instead of insisting on particular songs in a sequence, for each song, Anya gives Arup several choices1. For example, for the first song, Anya may tell Arup that she wants to hear one of tracks 1, 3, 8 or 9, and for the second song Anya may tell Arup she wants to hear one of tracks 2 or 12. Even though Arup has some choice in what songs he plays, he still gets caught pressing either the forward or backward button a great deal. Help him minimize the number of times he presses the buttons. In the example above, given that the CD is originally queued to start with track 1, no button presses are required because he can simply select to play track 1 followed by track 2.
The Problem: Given the number of tracks on Anya's favorite CD and the set of possible tracks for each song Anya wants played, determine the minimum number of button presses Arup must make to get a valid sequence of songs played. For the purposes of this problem, assume that at the very beginning the CD player is queued up to play track number 1, so that if the first song played is anything but track 1, some buttons will have to be pressed before the first song plays.
The first input line consists of two (space separated) positive integers: t (1 ≤ t ≤ 109), the number of tracks on Anya's favorite CD and s (1 ≤ s ≤ 1000), the number of songs Anya would like to listen to from the CD. The next s input lines contain the possible track numbers for each song she would like to listen to. The ith of these lines starts with a positive integer, ni (1 ≤ ni ≤ 10), representing the number of choices Anya has provided for the ith song she listens to. This is followed by ni distinct positive integers, each in between 1 and t, inclusive, indicating the track numbers of the possible ith song Anya will listen to.
Output a single integer on a line by itself indicating the minimum number of button presses Arup can use to play the desired sequence of songs from the CD.