#include using namespace std; int main(){ int N, M; cin >> N >> M; vector>> E(N + M * 8); for (int i = 0; i < M; i++){ int k, c; cin >> k >> c; vector s(k); for (int j = 0; j < k; j++){ cin >> s[j]; } int id = N + i * 8; for (int j = 0; j < k; j++){ if (s[j] % 2 == 0){ E[s[j] - 1].push_back(make_pair(s[j] / 2, id)); E[s[j] - 1].push_back(make_pair(s[j] / 2, id + 1)); E[id + 4].push_back(make_pair(s[j] / 2, s[j] - 1)); E[id + 6].push_back(make_pair(s[j] / 2, s[j] - 1)); } if (s[j] % 2 == 1){ E[s[j] - 1].push_back(make_pair(s[j] / 2, id + 2)); E[s[j] - 1].push_back(make_pair(s[j] / 2, id + 3)); E[id + 5].push_back(make_pair(s[j] / 2, s[j] - 1)); E[id + 7].push_back(make_pair(s[j] / 2, s[j] - 1)); } } E[id].push_back(make_pair(c, id + 4)); E[id + 1].push_back(make_pair(c + 1, id + 5)); E[id + 2].push_back(make_pair(c + 1, id + 6)); E[id + 3].push_back(make_pair(c + 1, id + 7)); } int V = N + M * 8; vector d(V, -1); priority_queue, vector>, greater>> pq; pq.push(make_pair(0, 0)); while (!pq.empty()){ long long dd = pq.top().first; int v = pq.top().second; pq.pop(); if (d[v] == -1){ d[v] = dd; for (auto P : E[v]){ int w = P.second; if (d[w] == -1){ pq.push(make_pair(dd + P.first, w)); } } } } cout << d[N - 1] << endl; }