import sys import heapq def main(): input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 M = int(data[idx]) idx += 1 adj = [[] for _ in range(N+1)] # 1-based for _ in range(M): k_i = int(data[idx]) idx += 1 c_i = int(data[idx]) idx += 1 s = list(map(int, data[idx:idx + k_i])) idx += k_i # Add all pairs for x in range(k_i): for y in range(x+1, k_i): a = s[x] b = s[y] w = (a + b + 1) // 2 + c_i adj[a].append((b, w)) adj[b].append((a, w)) # 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 current_dist > dist[u]: continue if u == N: print(current_dist) return for v, w in adj[u]: if dist[v] > dist[u] + w: dist[v] = dist[u] + w heapq.heappush(heap, (dist[v], v)) print(-1) if __name__ == '__main__': main()