#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int m, n, l, tot;
struct node { 
	int val, lev, to;
	void input() {
		scanf( "%d%d%d", &val, &lev, &to );
	}
}a[102];
int nxt[102][102],cheap[102][102];
int find( int i ) {
	if( a[i].to == 0 )
		return a[i].val;
	int mmin = a[i].val;
	for( int j = 1; j <= a[i].to; ++j ) {
		if( a[nxt[i][j]].lev >= m ) {
			mmin = min( mmin, cheap[i][j] + find( nxt[i][j] ) );
		}
	}
	return mmin;
}
int main() {
	while( scanf("%d%d", &m, &n ) != EOF ) {
		a[1].input();
		for( int i = 1; i<= a[1].to; ++i )
			scanf( "%d%d", &nxt[1][i], &cheap[1][i] );
		m = a[1].lev - m;
		for( int i = 2; i <= n; ++i ) {
			a[i].input();
			for( int j = 1; j <= a[i].to; ++j ) {
				scanf( "%d%d", &nxt[i][j], &cheap[i][j] );
			}
		}
		int ans = find( 1 );
		printf( "%d\n", ans );
	}
	return 0;
}