#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 110, M = 10010;
// n 点数 m 等级限制
int n, m;
int h[N], e[M], ne[M], w[M], idx;
int leval[N];
int dist[N];
bool st[N];
void add(int a,int b,int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void build(){
memset(h, -1, sizeof h);
int ver = 0;
for(int i = 1;i <= n;i ++ ){
int price, cnt;
scanf("%d%d%d", &price, &leval[i], &cnt);
add(ver, i, price);
while(cnt -- ){
int id, cost;
scanf("%d%d", &id, &cost);
add(id, i, cost);
}
}
}
int dijkstra(int start,int end){
memset(dist, 0x3f, sizeof dist);
memset(st, false, sizeof st);
dist[0] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({dist[0], 0});
while(heap.size()){
auto t = heap.top(); heap.pop();
int ver = t.y, d = t.x;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver];~i;i = ne[i]){
int j = e[i];
int lev = leval[j];
if(lev > end || lev < start) continue;
if(dist[j] > dist[ver] + w[i]){
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
if(dist[1] == INF) return INF;
else return dist[1];
}
int main(){
scanf("%d%d", &m, &n);
build();
int res = INF;
// 把 leval[1] 包含进去
for(int i = leval[1] - m;i <= leval[1];i ++ )
res = min(res, dijkstra(i, i + m));
printf("%d\n", res);
return 0;
}