public static void main(String[] args) throws Throwable{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		for(;;){
			StringTokenizer st = new StringTokenizer(in.readLine().trim()); 
			int M = parseInt(st.nextToken()), W = parseInt(st.nextToken());
			if(M==0&&W==0)break;
			double[][] mAdy = new double[2*M][2*M];
			ArrayList<Integer> lAdy[] = new ArrayList[2*M];
			for (int i = 0; i < lAdy.length; i++) lAdy[i] = new ArrayList<Integer>();
			for (int i = 0; i < M - 2; i++) {
				st = new StringTokenizer(in.readLine().trim());
				int d = parseInt(st.nextToken())-1, c = parseInt(st.nextToken());
				if(c>0){
					mAdy[2*d][2*d+1]=mAdy[2*d+1][2*d]=c;
					lAdy[2*d].add(2*d+1);lAdy[2*d+1].add(2*d);
				}
			}
			mAdy[0][1]=mAdy[1][0]=Integer.MAX_VALUE;
			mAdy[2*M-2][2*M-1]=mAdy[2*M-1][2*M-2]=Integer.MAX_VALUE;
			lAdy[2*M-1].add(2*M-2);lAdy[2*M-2].add(2*M-1);
			lAdy[0].add(1);lAdy[1].add(0);
			for (int i = 0; i < W; i++) {
				st = new StringTokenizer(in.readLine().trim());
				int desde = parseInt(st.nextToken()) - 1, hasta = parseInt(st.nextToken()) - 1, costo = parseInt(st.nextToken());
				if(costo>0){
					mAdy[2*desde][2*hasta+1]=costo;
					mAdy[2*hasta][2*desde+1]=costo;
					lAdy[2*desde].add(2*hasta+1);
					lAdy[2*hasta].add(2*desde+1);
				}
			}
			System.out.println(Math.round(maxFlow(mAdy, 0, 2*M-1)));
		}
	}