#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using ll = long long; using std::cin; using std::cout; using std::endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int inf = (int)1e9 + 7; const long long INF = 1LL << 60; namespace KKT89 { std::vector dijkstra(int start, std::vector>>& graph) { std::vector dist(graph.size(), INF); dist[start] = 0; std::priority_queue, std::vector>, std::greater>> pq; pq.emplace(0, start); while (!pq.empty()) { ll cost; int idx; std::tie(cost, idx) = pq.top(); pq.pop(); if (dist[idx] < cost) continue; for (auto next : graph[idx]) if (chmin(dist[next.first], cost + next.second)) pq.emplace(dist[next.first], next.first); } return dist; } } void solve() { int n, m; cin >> n >> m; std::vector>> g(n + m * 2); for (int i = 0; i < m; ++i) { int k; cin >> k; const int u = n + i * 2; const int v = u + 1; g[u].emplace_back(v, 1); g[v].emplace_back(u, 1); ll c; cin >> c; for (int i = 0; i < k; ++i) { int s; cin >> s; s -= 1; if(s & 1) { g[v].emplace_back(s, s + 1 + c); g[s].emplace_back(v, s + 1 + c); } else { g[u].emplace_back(s, s + 1 + c); g[s].emplace_back(u, s + 1 + c); } } } ll res = KKT89::dijkstra(0, g)[n - 1]; if(res == INF) { cout << -1 << "\n"; return; } res /= 2; cout << res << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int kkt = 1; //cin >> kkt; while(kkt--) solve(); return 0; }