#include <bits/stdc++.h>   using namespace std;   const int N = 100500;   const int INF = 2 * (1e9);   int n, m;   vector<pair<int, int> > edge[N];   vector<pair<int, int> > delay[N];   int dist[N];   int main() {     cin.sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int from, to, len;       cin >> from >> to >> len;       from--;       to--;       edge[from].push_back(make_pair(to, len));       edge[to].push_back(make_pair(from, len));     }     for (int i = 0; i < n; i++) {       int num;       cin >> num;       int cur;       for (int j = 0; j < num; j++) {         cin >> cur;         if (j == 0 or delay[i].back().second != cur)           delay[i].push_back(make_pair(cur, cur + 1));         else           delay[i].back().second++;       }     }     for (int i = 0; i < n; i++) dist[i] = INF;     dist[0] = 0;     if (delay[0].size() >= 1 and delay[0][0].first == 0)       dist[0] = delay[0][0].second;     set<pair<int, int> > dijkstra;     dijkstra.insert(make_pair(dist[0], 0));     for (int i = 0; i < n; i++) {       if (dijkstra.empty()) break;       pair<int, int> now = *dijkstra.begin();       dijkstra.erase(now);       for (const pair<int, int> &next : edge[now.second]) {         int arrival = next.first;         int tm = next.second + now.first;         int idx = lower_bound(delay[arrival].begin(), delay[arrival].end(),                               make_pair(tm, INF)) -                   delay[arrival].begin();         if (idx > 0 and arrival != n - 1) {           idx--;           if (delay[arrival][idx].first <= tm and tm < delay[arrival][idx].second)             tm = delay[arrival][idx].second;         }         if (dist[arrival] > tm) {           dijkstra.erase(make_pair(dist[arrival], arrival));           dist[arrival] = tm;           dijkstra.insert(make_pair(dist[arrival], arrival));         }       }     }     if (dist[n - 1] == INF) dist[n - 1] = -1;     cout << dist[n - 1] << endl;     return 0;   }   