import sys r = sys.stdin N,M = map(int,r.readline().split()) G = [[] for _ in range(N + 1 + M)] for i in range(M): l = list(map(int,r.readline().split())) for k in range(2,len(l)): G[l[k]].append(N + i + 1) G[N + i + 1].append((l[k],l[1])) import heapq q = [] heapq.heapify(q) heapq.heappush(q,(0,1)) C = 10 ** 16 dist = [C] * (N + 1) dist[1] = 0 while len(q): d,i = heapq.heappop(q) if i == N: print(d) exit() if d > dist[i]: continue for m in G[i]: for s,c in G[m]: u = (i + s + 1) // 2 + c + dist[i] if u < dist[s]: dist[s] = u heapq.heappush(q,(u,s)) print(-1)