#include typedef struct List { struct List *next; int v; long long cost; } list; typedef struct { long long key; int id; } data; typedef struct { data obj[800001]; int size; } min_heap; void push(data x, min_heap* h) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key < h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(min_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1; if (h->obj[j].key < h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j = i << 1; } else break; } return output; } int main() { int i, j, k, m, n, u, w, N, M; long long c; list *adj[500001] = {}, e[800001], *p; scanf("%d %d", &N, &M); for (i = 1, m = 0, n = N; i <= M; i++, n += 4) { scanf("%d %lld", &k, &c); for (j = 1; j <= k; j++) { scanf("%d", &u); if (u % 2 == 0) { w = n + 1; e[m].v = w; e[m].cost = c + u / 2; e[m].next = adj[u]; adj[u] = &(e[m++]); w = n + 2; e[m].v = w; e[m].cost = c + u / 2; e[m].next = adj[u]; adj[u] = &(e[m++]); w = n + 1; e[m].v = u; e[m].cost = u / 2; e[m].next = adj[w]; adj[w] = &(e[m++]); w = n + 3; e[m].v = u; e[m].cost = u / 2; e[m].next = adj[w]; adj[w] = &(e[m++]); } else { w = n + 3; e[m].v = w; e[m].cost = c + u / 2 + 1; e[m].next = adj[u]; adj[u] = &(e[m++]); w = n + 4; e[m].v = w; e[m].cost = c + u / 2 + 1; e[m].next = adj[u]; adj[u] = &(e[m++]); w = n + 2; e[m].v = u; e[m].cost = u / 2 + 1; e[m].next = adj[w]; adj[w] = &(e[m++]); w = n + 4; e[m].v = u; e[m].cost = u / 2; e[m].next = adj[w]; adj[w] = &(e[m++]); } } } const long long sup = 1LL << 60; long long dist[500001]; min_heap h; data d; for (u = 1; u <= n; u++) dist[u] = sup; dist[1] = 0; h.size = 0; d.key = 0; d.id = 1; push(d, &h); while (h.size > 0) { d = pop(&h); u = d.id; if (d.key != dist[u]) continue; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (dist[w] > dist[u] + p->cost) { dist[w] = dist[u] + p->cost; d.key = dist[w]; d.id = w; push(d, &h); } } } if (dist[N] == sup) printf("-1\n"); else printf("%lld\n", dist[N]); fflush(stdout); return 0; }