#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max() / 32 * 15 + 208; template struct edge { int from, to; T cost; edge(int to, T cost) : from(-1), to(to), cost(cost) {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} }; template vector dijkstra(int s,vector>> &G){ auto n = G.size(); vector d(n, INF); priority_queue,vector>,greater<>> Q; d[s] = 0; Q.emplace(0, s); while(!Q.empty()){ T cost; int i; tie(cost, i) = Q.top(); Q.pop(); if(d[i] < cost) continue; for (auto &&e : G[i]) { auto cost2 = cost + e.cost; if(d[e.to] <= cost2) continue; d[e.to] = cost2; Q.emplace(d[e.to], e.to); } } return d; } template ostream& operator<<(ostream& os, vector v) { os << "{"; for (int i = 0; i < v.size(); ++i) { if(i) os << ", "; os << v[i]; } return os << "}"; } template ostream& operator<<(ostream& os, pair p) { return os << "{" << p.first << ", " << p.second << "}"; } int main() { int n, m; cin >> n >> m; vector>> G(n+2*m); int id = n; for (int i = 0; i < m; ++i) { int k, c; cin >> k >> c; for (int j = 0; j < k; ++j) { int x; scanf("%d", &x); if(x&1) { G[x-1].emplace_back(id, x/2+1); G[id].emplace_back(x-1, x/2+c); G[id+1].emplace_back(x-1, x/2+c+1); } else { G[x-1].emplace_back(id+1, x/2); G[id].emplace_back(x-1, x/2+c); G[id+1].emplace_back(x-1, x/2+c); } } id += 2; } ll ans = dijkstra(0, G)[n-1]; if(ans == INF) puts("-1"); else cout << ans << "\n"; return 0; }