import sys import heapq def main(): input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]); ptr += 1 M = int(input[ptr]); ptr += 1 adj = [[] for _ in range(N + 1)] for _ in range(M): k = int(input[ptr]); ptr += 1 c = int(input[ptr]); ptr += 1 s = list(map(int, input[ptr:ptr + k])) ptr += k s1 = s[0] s2 = s[1] # Add edge between s1 and s2 cost_s1_s2 = (s1 + s2 + 1) // 2 + c adj[s1].append((s2, cost_s1_s2)) adj[s2].append((s1, cost_s1_s2)) # Add edges from each node to s1 and s2 for node in s: cost_to_s1 = (node + s1 + 1) // 2 + c adj[node].append((s1, cost_to_s1)) adj[s1].append((node, cost_to_s1)) cost_to_s2 = (node + s2 + 1) // 2 + c adj[node].append((s2, cost_to_s2)) adj[s2].append((node, cost_to_s2)) # Dijkstra's algorithm INF = float('inf') dist = [INF] * (N + 1) dist[1] = 0 heap = [] heapq.heappush(heap, (0, 1)) while heap: current_dist, u = heapq.heappop(heap) if u == N: break if current_dist > dist[u]: continue for v, w in adj[u]: if dist[v] > current_dist + w: dist[v] = current_dist + w heapq.heappush(heap, (dist[v], v)) print(dist[N] if dist[N] != INF else -1) if __name__ == '__main__': main()