#include<stdio.h>
#include<vector>
#include<stack>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn = 10005;
const int maxm = 105;
const int oo = 0xfffffff;
struct node
{
int u, v, price, next;
}e[maxn];
int head[maxm], lv[maxm], p[maxm];
bool use[maxm];
void Add(int u, int v, int c, int k)
{
e[k].u = u;
e[k].v = v;
e[k].price = c;
e[k].next = head[u];
head[u] = k;
}
int Dfs(int MaxLv, int MinLv, int k, int M)
{
int MinP = oo;
for(int i=head[k]; i!=0; i=e[i].next)
{
int v = e[i].v;
int c = e[i].price;
if(lv[v] >= MinLv && lv[v] <= MaxLv && use[v] == false)
{
use[v] = true;
int L = min(MaxLv, lv[v]+M);
int R = max(MinLv, lv[v]-M);
MinP = min(MinP, Dfs(L, R, v, M)+c);
use[v] = false;
}
}
return min(p[k], MinP);
}
int main()
{
int N, M;
while(scanf("%d%d", &M, &N) != EOF)
{
int i, k=1, v, price, T;
memset(head, 0, sizeof(head));
for(i=1; i<=N; i++)
{
scanf("%d%d%d", &p[i], &lv[i], &T);
while(T--)
{
scanf("%d%d", &v, &price);
Add(i, v, price, k++);
}
}
use[1] = true;
int ans = Dfs(lv[1]+M, lv[1]-M, 1, M);
printf("%d\n", ans);
}
return 0;