/* Well figure the topic. The most short-circuit application, dijkstra algorithm */# include <iostream> using namespace std; const int INF = 100000; int a [105] [105], B [105], c [105], s [105], dist [105]; int n, m; int dj (int p, int q) {int I, j, r, t, k = 0; for (I = 0; I <n; I ++) if (B [I]> = p & B [I] <= q) dist [I] = a [k] [I], s [I] = 0; else dist [I] = INF, s [I] = 0; dist [0] = 0; s [0] = 1; for (I = 1; I <n; I ++) {t = INF; for (j = 0; j <n; j ++) if (! S [j] & dist [j] <t) t = dist [j], k = j; s [k] = 1; for (j = 0; j <n; j ++) if (! S [j] & a [k] [j] <INF & B [j]> = p & B [j] <= q) {r = dist [k] + a [k] [j]; if (dist [j]> r) dist [j] = r ;}} t = c [0]; for (I = 0; I <n; I ++) if (dist [I] + c [I] <t) t = dist [I] + c [I]; return t;} int main (int argc, char * argv []) {int I, j, k, p, q, l, r; while (cin> m> n & (m | n) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) a [I] [j] = INF; for (I = 0; I <n; I ++) {cin> c [I]> B [I]> k; for (j = 0; j <k; j ++) {cin> p> q; a [I] [P-1] = q ;}} r = c [0]; for (I = B [0]-m; I <= B [0]; I ++) {k = dj (I, I + m); if (k <r) r = k ;} cout <r <endl;} return 0 ;}