#include "bits/stdc++.h" //#include "atcoder/all" using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" struct Edge { int to, cost; Edge(int _to, int _cost) :to(_to), cost(_cost) {} }; vectorg[300005]; typedef pair P; long long dikstra(int st, int ed) { vector D(300005, INF); priority_queue, greater

> q;//小さいもの順 q.emplace(0, st); D[st] = 0; while (!q.empty()) { auto tmp = q.top(); q.pop(); long long cost = tmp.first; long long pos = tmp.second; if (cost > D[pos]) continue; for (Edge e : g[pos]) { if (cost + e.cost < D[e.to]) { D[e.to] = e.cost + cost; q.emplace(D[e.to], e.to); } } } if (INF == D[ed]) D[ed] = -1; else D[ed] /= 2; return D[ed]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; rep(i, m) { int k; cin >> k; int c; cin >> c; int u = n + 2 * i; int v = n + 2 * i + 1; rep(j, k) { int s; cin >> s; s--; if (0 == s % 2) { g[u].emplace_back(s, c + s + 1); g[s].emplace_back(u, c + s + 1); } else { g[v].emplace_back(s, c + s + 1); g[s].emplace_back(v, c + s + 1); } } g[u].emplace_back(v, 1); g[v].emplace_back(u, 1); } cout << dikstra(0, n - 1) << endl; return 0; }