//#define _GLIBCXX_DEBUG #include using namespace std; #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater>; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} //head int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vvi G(n); vvi s(m); vi k(m); vi c(m); rep(i, m) { cin >> k[i]; s[i].resize(k[i]); cin >> c[i]; rep(j, k[i]) { cin >> s[i][j]; G[--s[i][j]].emplace_back(i); } } vector dist(n, LINF); dist[0] = 0; PQG> q; q.emplace(0, 0); while(not q.empty()) { auto [d, now] = q.top(); q.pop(); if(d != dist[now]) continue; for(auto to:G[now]) { if(k[to] == -1) continue; for(auto ne:s[to]) { if(chmin(dist[ne], d+c[to]+(ne+now+1)/2+1)) { q.emplace(dist[ne], ne); } } k[to] = -1; } } if(dist[n-1] == LINF) puts("-1"); else cout << dist[n-1] << endl; }