import heapq def main(): import sys input = sys.stdin.read data = input().split() ptr = 0 N = int(data[ptr]) ptr += 1 M = int(data[ptr]) ptr += 1 ops = [] for _ in range(M): k_i = int(data[ptr]) ptr += 1 c_i = int(data[ptr]) ptr += 1 s_i = list(map(int, data[ptr:ptr + k_i])) ptr += k_i ops.append((k_i, c_i, s_i)) INF = 1 << 60 distance = [INF] * (N + 1) distance[1] = 0 heap = [] heapq.heappush(heap, (0, 1)) # Process each operation once for op in ops: k_i, c_i, s_i = op min_val = INF # Find the minimal 2*d + u + 1 among nodes in s_i that have been reached for u in s_i: if distance[u] < INF: current_val = 2 * distance[u] + u + 1 if current_val < min_val: min_val = current_val if min_val == INF: continue # No nodes in this operation are reachable yet # Update distances for all nodes in s_i for v in s_i: new_dist = (min_val + v) // 2 + c_i if new_dist < distance[v]: # Only push to heap if this is a better distance if distance[v] == INF: distance[v] = new_dist heapq.heappush(heap, (new_dist, v)) else: if new_dist < distance[v]: distance[v] = new_dist heapq.heappush(heap, (new_dist, v)) # Continue with Dijkstra's algorithm to ensure all nodes are processed while heap: d, u = heapq.heappop(heap) if u == N: break if d > distance[u]: continue # No explicit edges to process here; all edges are handled via the operations print(distance[N] if distance[N] != INF else -1) if __name__ == "__main__": main()